[Interactive Beam] Fix caching deadlock, wait race conditions, and stale graph in notebooks#39161
[Interactive Beam] Fix caching deadlock, wait race conditions, and stale graph in notebooks#39161ian-Liaozy wants to merge 4 commits into
Conversation
…ph in Colab. - Resolve self-deadlock in background thread: Updated `_wait_for_dependencies` to exclude target PCollections from the wait list when called from the background thread (i.e. when `async_result` is provided), allowing it to only wait on upstream dependencies. - Fix duplicate execution race condition: Replaced waiting on `future.result()` with a `threading.Event` (`_completed_event`) set at the very end of `_on_done`. This ensures `collect()` only resumes after the background job has fully marked the PCollection as computed. - Recalculate uncomputed PCollections: Added a check in `record()` to re-evaluate computed PCollections after waiting, preventing the launch of duplicate pipeline fragments for PCollections that completed during the wait. - Fix stale pipeline graph: Removed caching of the `PipelineGraph` in `RecordingManager`. Re-creating the graph dynamically ensures that new transforms added in subsequent Colab cells are correctly detected and computed.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses critical stability issues in Interactive Beam when running within notebook environments. By refining how background threads handle dependencies and ensuring that the pipeline graph is dynamically refreshed, the changes prevent common race conditions and deadlocks that previously led to stale state or duplicate execution of pipeline fragments. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request improves the reliability and correctness of asynchronous computations in the RecordingManager by introducing a threading.Event to wait for completion, ensuring cleanup in _on_done via a finally block, and dynamically recreating the pipeline graph to avoid stale caches. It also updates dependency waiting logic and recalculates uncomputed PCollections after waiting to prevent redundant runs. The reviewer feedback is highly valuable, pointing out a critical thread-safety issue where concurrent modification of _async_computations could raise a RuntimeError, and suggesting a lock-based solution. Additionally, the reviewer suggests a cleaner, more idiomatic way to compute the set difference of uncomputed PCollections.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for dep in pcolls_to_check: | ||
| is_computing = self._env.is_pcollection_computing(dep) | ||
| if is_computing: |
There was a problem hiding this comment.
Iterating over self._async_computations.values() (line 811) while another thread concurrently modifies the dictionary (e.g., in _on_done via pop()) will raise a RuntimeError: dictionary changed size during iteration.
To prevent this, we should protect accesses to self._async_computations using a lock.
Please define a lock in RecordingManager.__init__:
self._lock = threading.Lock()And update _wait_for_dependencies to copy the values under the lock before iterating:
with self._lock:
async_computations_copy = list(self._async_computations.values())
for dep in pcolls_to_check:
is_computing = self._env.is_pcollection_computing(dep)
if is_computing:
for comp in async_computations_copy:
if dep in comp._pcolls:
computing_deps[dep] = comp| self._env.unmark_pcollection_computing(self._pcolls) | ||
| self._recording_manager._async_computations.pop(self._display_id, None) |
There was a problem hiding this comment.
To ensure thread safety and prevent RuntimeError when _wait_for_dependencies iterates over _async_computations, we should pop the computation from the dictionary under the same lock.
| self._env.unmark_pcollection_computing(self._pcolls) | |
| self._recording_manager._async_computations.pop(self._display_id, None) | |
| self._env.unmark_pcollection_computing(self._pcolls) | |
| with self._recording_manager._lock: | |
| self._recording_manager._async_computations.pop(self._display_id, None) |
| computed_pcolls = set( | ||
| pcoll for pcoll in pcolls | ||
| if pcoll in ie.current_env().computed_pcollections) | ||
| uncomputed_pcolls = set(pcolls).difference(computed_pcolls) |
There was a problem hiding this comment.
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
|
Assigning reviewers: R: @claudevdm for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
This PR resolves several critical caching issues in Interactive Beam when executing pipelines in notebook environments (like Colab), preventing deadlocks, duplicate execution of runner jobs, and stale dependency resolution.
Changes:
Fix Background Thread Deadlock:
Updated
_wait_for_dependenciesto skip checking the target PCollection itself when executing inside the background thread (indicated by the presence ofasync_result). This avoids a self-deadlock where the background thread blocked waiting for its own future to resolve.Fix Wait Race Condition:
Previously, calling
future.result()in_wait_for_dependencieswas insufficient because the future resolves before the_on_donecallback finishes executing to mark the PCollection as computed. This created a race condition where the main thread resumed, saw the PCollection was still "uncomputed", and started a duplicate execution job.We resolved this by introducing a
threading.Event(_completed_event) inAsyncComputationResultthat is set at the very end of_on_done. The main thread now blocks oncomp.wait_for_completion(), guaranteeing that it only continues after the PCollection is fully marked as computed.Recalculate Uncomputed PCollections:
Updated
record()to recalculate the subset of remaining uncomputed PCollections after the dependency wait completes. If a PCollection finished computing during the wait, it skips the duplicate fragment execution.Fix Stale Pipeline Graph:
Removed caching of the
PipelineGraphinRecordingManager. Re-creating the graph dynamically ensures that any new transforms added by users in subsequent notebook cells are correctly detected and resolved as dependencies.Unit Tests Added:
Added three new tests to
recording_manager_test.pycovering:async_result.PipelineGraphwhen new transforms are appended.record()after wait completion.fixes #37220
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.