Skip to content
Draft
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 @@ -886,8 +886,8 @@ def test_compute_dependency_wait_false(self, mock_is_computing):

def test_async_computation_result_cancel(self):
p = beam.Pipeline(ir.InteractiveRunner())
# A stream that never finishes to test cancellation
pcoll = p | beam.Create([1]) | beam.Map(lambda x: time.sleep(100))
# A slow transform to test cancellation while the computation is in flight
pcoll = p | beam.Create([1]) | beam.Map(lambda x: time.sleep(3))
ib.watch(locals())
self.env.track_user_pipelines()

Expand All @@ -909,6 +909,7 @@ def test_async_computation_result_cancel(self):
# This part is hard to test without deeper runner integration
with self.assertRaises(TimeoutError):
async_result.result(timeout=1) # It should not complete successfully
async_result.wait_for_completion(timeout=30)

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.

Instead of waiting for completion, is there a way to actually force cancel the job? Then we could reraise the time.sleep to 100

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.

Under InteractiveRunner (which delegates to DirectRunner -> FnApiRunner), pipeline_to_run.run() executes the pipeline synchronously in-process on the RecordingManager._executor ThreadPoolExecutor worker thread before run() even returns a PipelineResult (recording_manager.py:491). Because Python's ThreadPoolExecutor / threading.Thread cannot forcibly interrupt a running thread that is already blocked inside a C-level time.sleep(100) (Future.cancel() only cancels futures that haven't started running yet), and because line 911 (with self.assertRaises(TimeoutError): async_result.result(timeout=1)) explicitly tests that the background thread is still running after async_result.cancel() returns, cancel() itself cannot kill time.sleep(100).

However, we can keep the 100s timeout AND immediately unblock the worker thread during cleanup by replacing time.sleep(100) with a threading.Event wait (InteractiveBeamComputeTest._cancel_event.wait(100), using a class attribute so the Map lambda isn't rejected by PipelineFragment runner-API pickling) and calling InteractiveBeamComputeTest._cancel_event.set() in a finally: block right before async_result.wait_for_completion(timeout=30). I have applied and verified this locally on the branch.


@patch(
'apache_beam.runners.interactive.recording_manager.RecordingManager.'
Expand Down
Loading