Kill process group on flow run cancellation#21112
Open
wavebyrd wants to merge 9436 commits intoPrefectHQ:mainfrom
Open
Kill process group on flow run cancellation#21112wavebyrd wants to merge 9436 commits intoPrefectHQ:mainfrom
wavebyrd wants to merge 9436 commits intoPrefectHQ:mainfrom
Conversation
…efectHQ#20718) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…#20716) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…#20720) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…20673) Co-authored-by: Claude Opus 4.6 <[email protected]>
…Q#20721) Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…fectHQ#20692) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Nate Nowack <[email protected]>
…refectHQ#20725) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…#20671) Co-authored-by: Claude Opus 4.6 <[email protected]>
…age E2E tests (PrefectHQ#20726) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…0727) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Pablo Cabeza <[email protected]>
…ctHQ#20730) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…s (wave 4b) (PrefectHQ#20724) Co-authored-by: Claude Opus 4.6 <[email protected]>
…refectHQ#20653) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
PrefectHQ#20723) Co-authored-by: Claude Opus 4.6 <[email protected]>
… link E2E tests (PrefectHQ#20737) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…` field for programmatic resume (PrefectHQ#20741) Co-authored-by: Claude Sonnet 4.6 <[email protected]>
…ctHQ#20701) Co-authored-by: Claude Opus 4.6 <[email protected]>
…PrefectHQ#20743) Co-authored-by: Claude Opus 4.6 <[email protected]>
…opts (wave 5a) (PrefectHQ#20740) Co-authored-by: Claude Opus 4.6 <[email protected]>
… (wave 5b) (PrefectHQ#20745) Co-authored-by: Claude Opus 4.6 <[email protected]>
…fectHQ#20744) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Nate Nowack <[email protected]>
…ave 6a) (PrefectHQ#20749) Co-authored-by: Claude Opus 4.6 <[email protected]>
…trator` (PrefectHQ#20757) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…Q#20763) Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
…#20761) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
…refectHQ#21086) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Nate Nowack <[email protected]>
…21085) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alexander Streed <[email protected]> Co-authored-by: alex.s <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
…1090) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
…fectHQ#21071) Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: alex.s <[email protected]> Co-authored-by: alex.s <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: alex.s <[email protected]> Co-authored-by: alex.s <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: bot_apk <[email protected]>
Start flow run subprocesses in their own process group (start_new_session=True) and use os.killpg instead of os.kill when cancelling. This ensures child processes spawned by ShellOperation (or anything else) get terminated along with the flow run process, instead of being orphaned. Fixes PrefectHQ#20979
desertaxle
requested changes
Mar 13, 2026
Member
desertaxle
left a comment
There was a problem hiding this comment.
Thanks for opening a PR @wavebyrd!
Please run ruff on your changes so that the static analysis stage in our checks passes. Also, it looks like these changes are causing the ' pytest ' process to be killed during the test run. That will need to be fixed before we can approve and merge this PR.
2ec67b1 to
813f9b0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #20979.
When a flow run is cancelled, the worker sends SIGTERM only to the flow run process itself. Any child processes (like those from
ShellOperation) are left orphaned.This PR:
start_new_session=Trueon POSIX)os.killtoos.killpg, targeting the whole process groupThis covers all three cancellation/kill paths:
Runner._kill_process,ProcessManager.kill, andRunner.reschedule_current_flow_runs. The newEngineCommandStarterpath also getsstart_new_session=True.Windows behavior is unchanged (still uses
CTRL_BREAK_EVENT).Test plan
test__process_manager.pytests to verifykillpg/getpgidusageShellOperation(commands=["sleep 9999"])to a process work pool, cancel via UI, confirmsleepprocess is terminated