From ede5bf701821e3eb696fc916c8c1a23fd8d1ffce Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Wed, 11 Nov 2026 12:47:55 +0000 Subject: [PATCH 1/4] Prevent background computation leak in test_async_computation_result_cancel --- .../interactive/interactive_beam_test.py | 53 ++++++++++++------- .../runners/interactive/recording_manager.py | 3 +- .../runners/interactive/testing/mock_env.py | 6 +-- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py index af3f6638fc91..4614b2acae7b 100644 --- a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py +++ b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py @@ -21,6 +21,7 @@ import dataclasses import importlib import sys +import threading import time import unittest from concurrent.futures import TimeoutError @@ -886,29 +887,41 @@ 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)) + pcoll = p | beam.Create([1]) ib.watch(locals()) self.env.track_user_pipelines() - async_result = ib.compute(pcoll, blocking=False) - self.assertIsInstance(async_result, AsyncComputationResult) - - # Give it a moment to start - time.sleep(0.1) - - # Mock the pipeline result's cancel method - mock_pipeline_result = MagicMock() - mock_pipeline_result.state = PipelineState.RUNNING - async_result.set_pipeline_result(mock_pipeline_result) - - self.assertTrue(async_result.cancel()) - mock_pipeline_result.cancel.assert_called_once() - - # The future should be cancelled eventually by the runner - # This part is hard to test without deeper runner integration - with self.assertRaises(TimeoutError): - async_result.result(timeout=1) # It should not complete successfully + rm = self.env.get_recording_manager(p) + started_event = threading.Event() + release_event = threading.Event() + + def fake_execute(pcolls_to_compute, async_res, runner=None, options=None): + mock_res = MagicMock() + mock_res.state = PipelineState.RUNNING + if async_res: + async_res.set_pipeline_result(mock_res) + started_event.set() + release_event.wait(timeout=10) + return mock_res + + with patch.object( + rm, '_execute_pipeline_fragment', side_effect=fake_execute): + async_result = None + try: + async_result = ib.compute(pcoll, blocking=False) + self.assertIsInstance(async_result, AsyncComputationResult) + self.assertTrue(started_event.wait(timeout=5)) + + mock_pipeline_result = async_result._pipeline_result + self.assertTrue(async_result.cancel()) + mock_pipeline_result.cancel.assert_called_once() + + with self.assertRaises(TimeoutError): + async_result.result(timeout=0.2) + finally: + release_event.set() + if async_result is not None: + async_result.wait_for_completion(timeout=5) @patch( 'apache_beam.runners.interactive.recording_manager.RecordingManager.' diff --git a/sdks/python/apache_beam/runners/interactive/recording_manager.py b/sdks/python/apache_beam/runners/interactive/recording_manager.py index bd861d9904e3..28c3e5c66a50 100644 --- a/sdks/python/apache_beam/runners/interactive/recording_manager.py +++ b/sdks/python/apache_beam/runners/interactive/recording_manager.py @@ -194,7 +194,8 @@ def cancel(self): return False self._cancel_requested = True - self._cancel_button.disabled = True + if self._cancel_button is not None: + self._cancel_button.disabled = True self.update_display('Attempting to cancel...') if self._pipeline_result: diff --git a/sdks/python/apache_beam/runners/interactive/testing/mock_env.py b/sdks/python/apache_beam/runners/interactive/testing/mock_env.py index d6f7f318a4f0..fc6571bc64da 100644 --- a/sdks/python/apache_beam/runners/interactive/testing/mock_env.py +++ b/sdks/python/apache_beam/runners/interactive/testing/mock_env.py @@ -49,11 +49,11 @@ def setUp(self): self.env_patchers.append(self.ipython_patcher) # self.current_env IS interactive_environment.current_env() in tests. + self.current_env = InteractiveEnvironment() self.ie_patcher = patch( - f'{interactive_path}.interactive_environment.current_env') + f'{interactive_path}.interactive_environment.current_env', + return_value=self.current_env) self.m_current_env = self.ie_patcher.start() - self.current_env = InteractiveEnvironment() - self.m_current_env.return_value = self.current_env self.env_patchers.append(self.ie_patcher) # Patches dataproc cluster creation and deletion. From a503c6cd32ca83ac486fc95ead81879174c74dbe Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Wed, 11 Nov 2026 13:05:09 +0000 Subject: [PATCH 2/4] Format interactive_beam_test.py with yapf --- .../apache_beam/runners/interactive/interactive_beam_test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py index 4614b2acae7b..ebe297f479b0 100644 --- a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py +++ b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py @@ -904,8 +904,9 @@ def fake_execute(pcolls_to_compute, async_res, runner=None, options=None): release_event.wait(timeout=10) return mock_res - with patch.object( - rm, '_execute_pipeline_fragment', side_effect=fake_execute): + with patch.object(rm, + '_execute_pipeline_fragment', + side_effect=fake_execute): async_result = None try: async_result = ib.compute(pcoll, blocking=False) From df61ac5241f324f6deba1cf9750a0da0b0b02599 Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Wed, 11 Nov 2026 19:50:42 +0000 Subject: [PATCH 3/4] Simplify test_async_computation_result_cancel fix to @patch and wait_for_completion --- .../interactive/interactive_beam_test.py | 59 ++++++++----------- .../runners/interactive/recording_manager.py | 3 +- .../runners/interactive/testing/mock_env.py | 6 +- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py index ebe297f479b0..8c0608b942ae 100644 --- a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py +++ b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py @@ -21,7 +21,6 @@ import dataclasses import importlib import sys -import threading import time import unittest from concurrent.futures import TimeoutError @@ -885,44 +884,36 @@ def test_compute_dependency_wait_false(self, mock_is_computing): spy_execute.assert_called_with({pcoll2}, async_res2, ANY, ANY) self.assertTrue(pcoll2 in self.env.computed_pcollections) - def test_async_computation_result_cancel(self): + @patch( + 'apache_beam.runners.interactive.recording_manager.RecordingManager.' + '_execute_pipeline_fragment', + side_effect=lambda *args: time.sleep(2)) + def test_async_computation_result_cancel(self, unused_mock_execute): p = beam.Pipeline(ir.InteractiveRunner()) - pcoll = p | beam.Create([1]) + # A stream that never finishes to test cancellation + pcoll = p | beam.Create([1]) | beam.Map(lambda x: time.sleep(100)) ib.watch(locals()) self.env.track_user_pipelines() - rm = self.env.get_recording_manager(p) - started_event = threading.Event() - release_event = threading.Event() - - def fake_execute(pcolls_to_compute, async_res, runner=None, options=None): - mock_res = MagicMock() - mock_res.state = PipelineState.RUNNING - if async_res: - async_res.set_pipeline_result(mock_res) - started_event.set() - release_event.wait(timeout=10) - return mock_res + async_result = ib.compute(pcoll, blocking=False) + self.assertIsInstance(async_result, AsyncComputationResult) - with patch.object(rm, - '_execute_pipeline_fragment', - side_effect=fake_execute): - async_result = None - try: - async_result = ib.compute(pcoll, blocking=False) - self.assertIsInstance(async_result, AsyncComputationResult) - self.assertTrue(started_event.wait(timeout=5)) - - mock_pipeline_result = async_result._pipeline_result - self.assertTrue(async_result.cancel()) - mock_pipeline_result.cancel.assert_called_once() - - with self.assertRaises(TimeoutError): - async_result.result(timeout=0.2) - finally: - release_event.set() - if async_result is not None: - async_result.wait_for_completion(timeout=5) + # Give it a moment to start + time.sleep(0.1) + + # Mock the pipeline result's cancel method + mock_pipeline_result = MagicMock() + mock_pipeline_result.state = PipelineState.RUNNING + async_result.set_pipeline_result(mock_pipeline_result) + + self.assertTrue(async_result.cancel()) + mock_pipeline_result.cancel.assert_called_once() + + # The future should be cancelled eventually by the runner + # 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=5) @patch( 'apache_beam.runners.interactive.recording_manager.RecordingManager.' diff --git a/sdks/python/apache_beam/runners/interactive/recording_manager.py b/sdks/python/apache_beam/runners/interactive/recording_manager.py index 28c3e5c66a50..bd861d9904e3 100644 --- a/sdks/python/apache_beam/runners/interactive/recording_manager.py +++ b/sdks/python/apache_beam/runners/interactive/recording_manager.py @@ -194,8 +194,7 @@ def cancel(self): return False self._cancel_requested = True - if self._cancel_button is not None: - self._cancel_button.disabled = True + self._cancel_button.disabled = True self.update_display('Attempting to cancel...') if self._pipeline_result: diff --git a/sdks/python/apache_beam/runners/interactive/testing/mock_env.py b/sdks/python/apache_beam/runners/interactive/testing/mock_env.py index fc6571bc64da..d6f7f318a4f0 100644 --- a/sdks/python/apache_beam/runners/interactive/testing/mock_env.py +++ b/sdks/python/apache_beam/runners/interactive/testing/mock_env.py @@ -49,11 +49,11 @@ def setUp(self): self.env_patchers.append(self.ipython_patcher) # self.current_env IS interactive_environment.current_env() in tests. - self.current_env = InteractiveEnvironment() self.ie_patcher = patch( - f'{interactive_path}.interactive_environment.current_env', - return_value=self.current_env) + f'{interactive_path}.interactive_environment.current_env') self.m_current_env = self.ie_patcher.start() + self.current_env = InteractiveEnvironment() + self.m_current_env.return_value = self.current_env self.env_patchers.append(self.ie_patcher) # Patches dataproc cluster creation and deletion. From b4f759486e626c1293b2046034f72f70f4cdfd22 Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Fri, 13 Nov 2026 14:11:00 +0000 Subject: [PATCH 4/4] Use 3s Map sleep instead of mocking _execute_pipeline_fragment in test_async_computation_result_cancel --- .../runners/interactive/interactive_beam_test.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py index 8c0608b942ae..2fa10fc4aa44 100644 --- a/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py +++ b/sdks/python/apache_beam/runners/interactive/interactive_beam_test.py @@ -884,14 +884,10 @@ def test_compute_dependency_wait_false(self, mock_is_computing): spy_execute.assert_called_with({pcoll2}, async_res2, ANY, ANY) self.assertTrue(pcoll2 in self.env.computed_pcollections) - @patch( - 'apache_beam.runners.interactive.recording_manager.RecordingManager.' - '_execute_pipeline_fragment', - side_effect=lambda *args: time.sleep(2)) - def test_async_computation_result_cancel(self, unused_mock_execute): + 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() @@ -913,7 +909,7 @@ def test_async_computation_result_cancel(self, unused_mock_execute): # 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=5) + async_result.wait_for_completion(timeout=30) @patch( 'apache_beam.runners.interactive.recording_manager.RecordingManager.'