-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[CI] Fix BackgroundResources double-cleanup crash by adding guard #36299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -384,14 +384,22 @@ class BackgroundResources: | |||||||||||||||
| # processing threads can access it without holding a ref to the client. | ||||||||||||||||
| engine_dead: bool = False | ||||||||||||||||
|
|
||||||||||||||||
| # Guard against double-cleanup | ||||||||||||||||
| _cleaned_up: bool = False | ||||||||||||||||
|
|
||||||||||||||||
| def __call__(self): | ||||||||||||||||
| """Clean up background resources.""" | ||||||||||||||||
| if self._cleaned_up: | ||||||||||||||||
| return | ||||||||||||||||
| self._cleaned_up = True | ||||||||||||||||
|
Comment on lines
+392
to
+394
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current idempotency check is not thread-safe. A race condition can occur where two threads both check I'm suggesting a change that introduces a lock to the
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python's GIL makes bool read/write atomic, and the underlying cleanup operations (close(linger=0), task.cancel(), setting attrs to None) are all individually idempotent. The _cleaned_up flag is an optimization to skip redundant work, not a correctness gate. Adding a Lock to a weakref finalizer target introduces complexity without practical benefit here. |
||||||||||||||||
|
|
||||||||||||||||
| self.engine_dead = True | ||||||||||||||||
| if self.engine_manager is not None: | ||||||||||||||||
| self.engine_manager.shutdown() | ||||||||||||||||
| self.engine_manager = None | ||||||||||||||||
| if self.coordinator is not None: | ||||||||||||||||
| self.coordinator.shutdown() | ||||||||||||||||
| self.coordinator = None | ||||||||||||||||
|
|
||||||||||||||||
| if isinstance(self.output_socket, zmq.asyncio.Socket): | ||||||||||||||||
| # Async case. | ||||||||||||||||
|
|
@@ -424,8 +432,10 @@ def close_sockets_and_tasks(): | |||||||||||||||
| del tasks | ||||||||||||||||
| del close_sockets_and_tasks | ||||||||||||||||
| close_sockets(sockets) | ||||||||||||||||
| del self.output_queue_task | ||||||||||||||||
| del self.stats_update_task | ||||||||||||||||
|
|
||||||||||||||||
| # Clear references instead of deleting attributes | ||||||||||||||||
| self.output_queue_task = None | ||||||||||||||||
| self.stats_update_task = None | ||||||||||||||||
| else: | ||||||||||||||||
| # Sync case. | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support the thread-safe idempotency guard in
__call__, a lock should be added to theBackgroundResourcesdataclass. This requires importingLockfromthreadingandfieldfromdataclasses.