bowenli86 commented on code in PR #28716:
URL: https://github.com/apache/flink/pull/28716#discussion_r4350797729


##########
flink-python/src/main/java/org/apache/flink/streaming/api/runners/python/beam/BeamPythonFunctionRunner.java:
##########
@@ -442,29 +512,284 @@ public Tuple3<String, byte[], Integer> takeResult() 
throws Exception {
 
     @Override
     public void flush() throws Exception {
-        if (bundleStarted) {
-            try {
-                finishBundle();
-            } finally {
+        final BundleCloseOperation closeOperation;
+        final boolean ownsBundleClose;
+        synchronized (this) {
+            if (cancelRequested) {
+                return;
+            }
+
+            if (bundleStarted) {
+                closeOperation = new BundleCloseOperation(remoteBundle);
+                bundleCloseOperation = closeOperation;
                 bundleStarted = false;
+                clearBundleReferences();
+                ownsBundleClose = true;
+            } else {
+                closeOperation = bundleCloseOperation;
+                ownsBundleClose = false;
             }
         }
+
+        if (closeOperation == null) {
+            return;
+        }
+
+        if (ownsBundleClose) {
+            finishBundle(closeOperation);
+        } else {
+            closeOperation.await();
+        }
     }
 
     /** Interrupts the progress of takeResult. */
     public void notifyNoMoreResults() {
         resultBuffer.add(Tuple2.of(null, new byte[0]));
     }
 
-    private void finishBundle() {
+    private void finishBundle(BundleCloseOperation closeOperation) {
+        RuntimeException failure = null;
         try {
-            remoteBundle.close();
+            closeOperation.remoteBundle.close();
         } catch (Throwable t) {
-            throw new RuntimeException("Failed to close remote bundle", t);
+            failure = new RuntimeException("Failed to close remote bundle", t);
+            throw failure;
         } finally {
-            remoteBundle = null;
-            mainInputReceiver = null;
-            timerInputReceiver = null;
+            closeOperation.complete(failure);
+            if (isCancelRequested()) {
+                try {
+                    closeResources();

Review Comment:
   Agreed—fixed in `17f1fc31eb6`. Resource cleanup now has a shared completion 
future, and concurrent shutdown callers wait outside the runner monitor until 
disposal finishes or receive its failure. The wait also preserves an 
interrupted cancel caller's interrupt status, so cancellation cannot return 
while final-lease disposal still holds the slot's shared-resource lock.
   
   Added a deterministic regression using the real `SharedResources` registry 
that forces the bundle-close thread to claim cleanup first. It covers 
successful and failed disposal, with and without interruption, and verifies 
single disposal plus subsequent allocation in the same registry. All 16 focused 
shutdown tests, Spotless, and checkstyle pass. The new cases also fail when the 
completion wait is disabled.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to