Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ public BeamFnApi.InstructionResponse.Builder trySplit(InstructionRequest request
/** Shutdown the bundles, running the tearDown() functions. */
public void shutdown() throws Exception {
bundleProcessorCache.shutdown();
beamFnDataClient.close();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BeamFnDataGrpcClient is created in FnHarness.main:

BeamFnDataGrpcClient beamFnDataMultiplexer =

and ownership passed to ProcessBundleHandler:

Therefore we should close it here.

When FnHarness.main is invoked multiple times (instead of as a standalone worker like on Dataflow runner), previously unclosed BeamFnDataGrpcClient leak gRPC streams

}
Comment on lines 768 to 771

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If bundleProcessorCache.shutdown() throws an exception, beamFnDataClient.close() will not be called, potentially leaking resources. Wrapping the shutdown calls in a try-finally block ensures that beamFnDataClient.close() is always executed.

  public void shutdown() throws Exception {
    try {
      bundleProcessorCache.shutdown();
    } finally {
      beamFnDataClient.close();
    }
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be overly defensive. close is always best effort. Currently bundleProcessorCache.shutdown() just invalidates a cache container


@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.beam.fn.harness.data;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements;
import org.apache.beam.model.pipeline.v1.Endpoints;
Expand All @@ -30,7 +32,7 @@
* provide a receiver of outbound elements. Callers can register themselves as receivers for inbound
* elements or can get a handle for a receiver of outbound elements.
*/
public interface BeamFnDataClient {
public interface BeamFnDataClient extends Closeable {
/**
* Registers a receiver for the provided instruction id.
*
Expand Down Expand Up @@ -72,4 +74,9 @@ void unregisterReceiver(
/** Get the outbound observer for the specified apiServiceDescriptor and dataStreamId. */
StreamObserver<Elements> getOutboundObserver(
Endpoints.ApiServiceDescriptor apiServiceDescriptor, String dataStreamId);

@Override
default void close() throws IOException {
// Default to no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public void poisonInstructionId(String instructionId) {
}
}

@Override
public void close() {
for (BeamFnDataGrpcMultiplexer client : multiplexerCache.values()) {
try {
client.close();
} catch (Exception e) {
LOG.warn("Failed to close multiplexer", e);
}
}
Comment thread
Abacn marked this conversation as resolved.
multiplexerCache.clear();
}

@Override
public StreamObserver<Elements> getOutboundObserver(
ApiServiceDescriptor apiServiceDescriptor, String dataStreamId) {
Expand Down
Loading