You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "shunping (via GitHub)" <gi...@apache.org> on 2026/05/08 19:58:33 UTC
[PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
shunping opened a new pull request, #38423:
URL: https://github.com/apache/beam/pull/38423
The following error occurs in the python test workflows from time to time. It happens during the shutdown of a job server, but looking at the code path, it could be a problem in other servers like expansion service.
```
Exception ignored in atexit callback <bound method StopOnExitJobServer.stop of <apache_beam.runners.portability.job_server.StopOnExitJobServer object at 0x7c8f2326c590>>:
Traceback (most recent call last):
File "/runner/_work/beam/beam/sdks/python/test-suites/tox/py313/build/srcs/sdks/python/target/.tox-py313/py313/lib/python3.13/site-packages/apache_beam/runners/portability/job_server.py", line 97, in stop
self._job_server.stop()
File "/runner/_work/beam/beam/sdks/python/test-suites/tox/py313/build/srcs/sdks/python/target/.tox-py313/py313/lib/python3.13/site-packages/apache_beam/runners/portability/job_server.py", line 127, in stop
return self._server.stop()
File "/runner/_work/beam/beam/sdks/python/test-suites/tox/py313/build/srcs/sdks/python/target/.tox-py313/py313/lib/python3.13/site-packages/apache_beam/utils/subprocess_server.py", line 255, in stop
self.stop_process()
File "/runner/_work/beam/beam/sdks/python/test-suites/tox/py313/build/srcs/sdks/python/target/.tox-py313/py313/lib/python3.13/site-packages/apache_beam/utils/subprocess_server.py", line 259, in stop_process
self._cache.purge(self._owner_id)
File "/runner/_work/beam/beam/sdks/python/test-suites/tox/py313/build/srcs/sdks/python/target/.tox-py313/py313/lib/python3.13/site-packages/apache_beam/utils/subprocess_server.py", line 93, in purge
raise ValueError(f"{owner} not in {self._live_owners}")
ValueError: 10 not in set()
```
We identified three potential issues and implemented fixes to harden the server shutdown process.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "shunping (via GitHub)" <gi...@apache.org>.
shunping commented on PR #38423:
URL: https://github.com/apache/beam/pull/38423#issuecomment-4410620271
cc'ed @tvalentyn
This seems promising to fix the "grpc deadline exceeds" error in our python test suites.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "shunping (via GitHub)" <gi...@apache.org>.
shunping commented on code in PR #38423:
URL: https://github.com/apache/beam/pull/38423#discussion_r3212048907
##########
sdks/python/apache_beam/utils/subprocess_server_test.py:
##########
@@ -302,6 +302,130 @@ def test_interleaved_owners(self):
self.assertNotEqual(cache.get('b'), b)
cache.purge(owner3)
+ def test_destructor_exception_partial_state(self):
+ # In SubprocessServer.stop_process(), we need to make sure self._owner_id is always
+ # set to None if it is not already set, even if a destructor exception happens
+ # during purge(owner_id).
+
+ destructor_calls = []
+
+ def faulty_destructor(obj):
+ destructor_calls.append(obj)
+ raise RuntimeError("Destructor failed")
+
+ custom_cache = subprocess_server._SharedCache(
+ lambda *args: "process_obj", faulty_destructor)
+
+ class CustomServer(subprocess_server.SubprocessServer):
+ _cache = custom_cache
+
+ def __init__(self):
+ super().__init__(lambda channel: None, ["dummy_cmd"], port=12345)
+
+ server = CustomServer()
+ server.start_process()
+ owner_id = server._owner_id
+ self.assertIsNotNone(owner_id)
+ self.assertIn(owner_id, custom_cache._live_owners)
+
+ # First stop attempt fails in the destructor
+ with self.assertRaises(RuntimeError):
+ server.stop_process()
+
+ # Verify fixed state: owner is purged from cache set, AND self._owner_id is successfully cleared to None
+ self.assertNotIn(owner_id, custom_cache._live_owners)
+ self.assertIsNone(server._owner_id)
+
+ # Second stop attempt safely does nothing (no ValueError raised)
+ try:
+ server.stop_process()
+ except ValueError:
+ self.fail("ValueError should not be raised here.")
+
+ def test_duplicate_atexit_registration_on_restart(self):
+ # Make sure we don't have duplicate atexit registration when reusing a
+ # StopOnExistJobServer instance.
+ from unittest.mock import patch
+ import atexit
+ from apache_beam.runners.portability import job_server
Review Comment:
Done. Thanks!
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "shunping (via GitHub)" <gi...@apache.org>.
shunping commented on PR #38423:
URL: https://github.com/apache/beam/pull/38423#issuecomment-4409489621
/gemini review
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "shunping (via GitHub)" <gi...@apache.org>.
shunping merged PR #38423:
URL: https://github.com/apache/beam/pull/38423
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #38423:
URL: https://github.com/apache/beam/pull/38423#issuecomment-4409445449
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment `assign set of reviewers`
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "derrickaw (via GitHub)" <gi...@apache.org>.
derrickaw commented on code in PR #38423:
URL: https://github.com/apache/beam/pull/38423#discussion_r3211105903
##########
sdks/python/apache_beam/utils/subprocess_server_test.py:
##########
@@ -302,6 +302,130 @@ def test_interleaved_owners(self):
self.assertNotEqual(cache.get('b'), b)
cache.purge(owner3)
+ def test_destructor_exception_partial_state(self):
+ # In SubprocessServer.stop_process(), we need to make sure self._owner_id is always
+ # set to None if it is not already set, even if a destructor exception happens
+ # during purge(owner_id).
+
+ destructor_calls = []
+
+ def faulty_destructor(obj):
+ destructor_calls.append(obj)
+ raise RuntimeError("Destructor failed")
+
+ custom_cache = subprocess_server._SharedCache(
+ lambda *args: "process_obj", faulty_destructor)
+
+ class CustomServer(subprocess_server.SubprocessServer):
+ _cache = custom_cache
+
+ def __init__(self):
+ super().__init__(lambda channel: None, ["dummy_cmd"], port=12345)
+
+ server = CustomServer()
+ server.start_process()
+ owner_id = server._owner_id
+ self.assertIsNotNone(owner_id)
+ self.assertIn(owner_id, custom_cache._live_owners)
+
+ # First stop attempt fails in the destructor
+ with self.assertRaises(RuntimeError):
+ server.stop_process()
+
+ # Verify fixed state: owner is purged from cache set, AND self._owner_id is successfully cleared to None
+ self.assertNotIn(owner_id, custom_cache._live_owners)
+ self.assertIsNone(server._owner_id)
+
+ # Second stop attempt safely does nothing (no ValueError raised)
+ try:
+ server.stop_process()
+ except ValueError:
+ self.fail("ValueError should not be raised here.")
+
+ def test_duplicate_atexit_registration_on_restart(self):
+ # Make sure we don't have duplicate atexit registration when reusing a
+ # StopOnExistJobServer instance.
+ from unittest.mock import patch
+ import atexit
+ from apache_beam.runners.portability import job_server
Review Comment:
nit: maybe move this to the top of file
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #38423:
URL: https://github.com/apache/beam/pull/38423#discussion_r3211028678
##########
sdks/python/apache_beam/runners/portability/job_server.py:
##########
@@ -94,8 +94,14 @@ def start(self):
def stop(self):
with self._lock:
if self._started:
- self._job_server.stop()
- self._started = False
+ try:
+ self._job_server.stop()
+ finally:
+ self._started = False
+ # Unregister the atexit handler to prevent duplicate
+ # registrations when the server is restarted/reused.
+ if hasattr(atexit, 'unregister'):
+ atexit.unregister(self.stop)
Review Comment:

The check `hasattr(atexit, 'unregister')` is unnecessary as Apache Beam supports Python 3.8+, and `atexit.unregister` has been available since Python 3.4. Removing this check simplifies the code and adheres to modern Python standards.
```python
atexit.unregister(self.stop)
```
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #38423:
URL: https://github.com/apache/beam/pull/38423#issuecomment-4409435174
## Summary of Changes
Hello, I'm Gemini Code Assist[^1]! 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 intermittent shutdown errors in job servers by hardening the cleanup process. It introduces better state management, ensures thread safety during cache operations, and prevents resource leaks or duplicate registrations by properly managing atexit handlers and ensuring cleanup routines complete reliably.
### Highlights
* **Atexit Handler Management**: Added logic to unregister atexit handlers when a job server is stopped, preventing duplicate registrations during server restarts.
* **Thread Safety in Cache Purging**: Improved thread safety in the shared cache by wrapping the owner membership check and removal within a lock, preventing race conditions.
* **Robust Error Recovery**: Implemented try-finally blocks in server shutdown processes to ensure internal state, such as owner IDs, is correctly cleared even if errors occur during cleanup.
<details>
<summary><b>New Features</b></summary>
🧠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](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
</details>
<details>
<summary><b>Using Gemini Code Assist</b></summary>
<br>
The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
<b>Invoking Gemini</b>
You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
Feature | Command | Description
--- | --- | ---
Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help | `/gemini help` | Displays a list of available commands.
<b>Customization</b>
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 `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
<b>Limitations & Feedback</b>
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
</details>
[^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "shunping (via GitHub)" <gi...@apache.org>.
shunping commented on PR #38423:
URL: https://github.com/apache/beam/pull/38423#issuecomment-4409438354
r: @derrickaw
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix race conditions, error recovery, and exit handlers in job servers [beam]
Posted by "shunping (via GitHub)" <gi...@apache.org>.
shunping commented on code in PR #38423:
URL: https://github.com/apache/beam/pull/38423#discussion_r3211045455
##########
sdks/python/apache_beam/runners/portability/job_server.py:
##########
@@ -94,8 +94,14 @@ def start(self):
def stop(self):
with self._lock:
if self._started:
- self._job_server.stop()
- self._started = False
+ try:
+ self._job_server.stop()
+ finally:
+ self._started = False
+ # Unregister the atexit handler to prevent duplicate
+ # registrations when the server is restarted/reused.
+ if hasattr(atexit, 'unregister'):
+ atexit.unregister(self.stop)
Review Comment:
Done,
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org