Skip to content

Fix hang on shutdown when asyncio.create_subprocess is used - #44

Merged
lackhove merged 7 commits into
lackhove:mainfrom
cdleonard:feature/fix-async-hang
Apr 4, 2026
Merged

lackhove merged 7 commits into
lackhove:mainfrom
cdleonard:feature/fix-async-hang

Conversation

@cdleonard

@cdleonard cdleonard commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Fixes #41 - can now using asyncio.create_subprocess while coverage_sh is enabled.

As far as I can tell asyncio.create_subprocess will initialize a subprocess.Popen object internally and use it with a custom PID waiter, skipping Popen.wait and Popen.poll. I was only able to get this to work by hooking into Popen.__del__.

There are other use cases where hooking Popen.__del__ is useful, for example if the user simply leaks a subprocess.Popen that shouldn't cause coverage_sh threads to simple hang.

It might make sense to also mark CoverageParserThread as a daemon thread. This shouldn't replace the __del__ hook because then threads would leak until main process exit.

Includes a new test_end2end_async_simple which does coverage on a new main_async.py script which calls asyncio.create_subprocess. Without PatchedPopen.__del__ it hangs - demonstrating the original issue. It also surprisingly shows that coverage works as expected!

@cdleonard cdleonard changed the title Fix async hang #43 Fix async hang #41 Mar 31, 2026
@cdleonard
cdleonard force-pushed the feature/fix-async-hang branch from 3e607f4 to 6a5960d Compare March 31, 2026 13:06
@cdleonard cdleonard changed the title Fix async hang #41 Fix hang on shutdown when asyncio.create_subprocess is used Mar 31, 2026
@lackhove

Copy link
Copy Markdown
Owner

Very cool, thanks! I will probably look into this next weekend. FYI: #40 and #45 will certainly conflict with all previous PRs, so i would like to get this one (and maybe #42) merged first.

@lackhove lackhove self-assigned this Apr 1, 2026
@cdleonard
cdleonard force-pushed the feature/fix-async-hang branch from 6a5960d to 55425c5 Compare April 1, 2026 10:13
@cdleonard

Copy link
Copy Markdown
Contributor Author

Removed dependency on #42

@lackhove
lackhove merged commit f896ef6 into lackhove:main Apr 4, 2026
7 checks passed
@lackhove

lackhove commented Apr 4, 2026

Copy link
Copy Markdown
Owner

@cdleonard awesome, thank you!

@cdleonard
cdleonard deleted the feature/fix-async-hang branch April 20, 2026 21:43
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.

Hang on shutdown when using asyncio.create_subprocess_exec

2 participants