r/learnpython • u/Conscious-Ball8373 • Nov 06 '24
Unawaited awaitables in asyncio programming - do you have a quick way to find them?
To give you some context, I've been writing software professionally for more than twenty years, Python for well over ten years and asyncio code for about a year.
Today, it took me more than four hours to find a place where I'd forgotten to await a coroutine. It was in the cleanup code for a test fixture; the fixture itself was passing so the warning got swallowed, but the failure to properly clean up then caused the next test to hang indefinitely.
I've completely lost count of the number of times I've been bitten by this. Do you have strategies for making awaitables that have not been awaited stick out so you see them before they cause you this sort of grief?
View all comments
Show parent comment
1
u/Conscious-Ball8373 Nov 07 '24
Again, the problem wasn't pytest per se. The problem was that one of my fixtures provided reset the database schema before every test, provided a database connection and then cleaned up that connection after the test completed. But because I'd forgotten to
awaitthe cleanup step, it held the database connection open and this caused the schema reset of the setup for the next test to hang.In principle, calling the cleanup function without
awaiting it produces a warning which gets printed onstderr. The reason pytest is relevant is that it capturesstdoutandstderrand only displays them if the test fails. This test didn't fail, so it never showed the warning. The next test hung with no obvious reason why. It took a lot of digging around to figure out why the test had hung and what was holding a database connection open.I would also like to point out that your interactive interpreter method of testing is vulnerable to a similar but different problem. Consider testing this code:
The defect is that I've forgotten to
await foo(). If you run this in an interactive interpreter like this:then you will also never see the warning, because
fwill never go out of scope and, depending on how you exit the interpreter, it will never get destructed and the warning will never be issued at all, let alone somewhere you see it and notice it.