Skip to content

Prevent race conditions in process teardown sequences and use of Execution in the body closure #88

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jakepetroules
Copy link
Contributor

This combines #81 and #87 into a single PR as it may be more difficult to understand the value of these fixes independently.

This patch ensures process termination monitoring is started after the body closure (which receives the Execution) called as part of the public API. This ensures that any use of Execution to send signals, etc., will send them to the right pid as we are guaranteed not to reap it until body returns. Additionally, we ensure the cleanup handler which runs the termination sequence is run ONLY once, and only run when the body closure throws or when the parent task is cancelled. In combination with the aforementioned process termination monitoring change, this ensures the termination sequence is similarly always sent to the right pid since it will always occur before the pid is reaped.

Closes #80
Closes #86

…hrows

If the body closure given to withAsyncTaskCleanupHandler throws, its cleanup handler is called twice: once in the catch block where body is called, and then again in the task group task once the Task.sleep throws due to cancellation, which swallows the error and then continues to call the handler as well. That results in the teardown sequence being invoked twice, as well as the teardown sequence being invoked for non-error cases.

This patch ensures the cleanup handler is invoked in failure cases only, and only once.

Closes #80
Process termination monitoring is started asynchronously with the closure which receives the Execution, meaning any use of Execution in the body to send signals, etc., may send them to a pid which no longer refers to the original process. The cleanup handler may also run after the process has already terminated.
@jakepetroules jakepetroules requested review from iCharlesHu and removed request for iCharlesHu June 21, 2025 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use of Execution and teardown sequences race with process termination withAsyncTaskCleanupHandler calls its cleanup handler twice if body throws
1 participant