https://textual.textualize.io/blog/2023/02/11/the-heisenbug-lurking-in-your-async-code/ [ ] [ ] Skip to content Textual The Heisenbug lurking in your async code ( ) ( ) [ ] Initializing search GitHub * Introduction * Tutorial * Guide * Roadmap * Reference * API * Blog Textual GitHub * [ ] Introduction Introduction + Getting started + Help * Tutorial * [ ] Guide Guide + Devtools + App Basics + Styles + Textual CSS + Design System + DOM Queries + Layout + Events and Messages + Input + Actions + Reactivity + Widgets + Animation + Screens * Roadmap * [ ] Reference Reference + [ ] CSS Types CSS Types o o o o o o o o o o o o + [ ] Events Events o Blur o DescendantBlur o DescendantFocus o Enter o Focus o Hide o Key o Leave o Load o Mount o MouseCapture o Click o MouseDown o MouseMove o MouseRelease o MouseScrollDown o MouseScrollUp o MouseUp o Paste o Resize o ScreenResume o ScreenSuspend o Show + [ ] Styles Styles o Align o Background o Border o Box-sizing o Color o Content-align o Display o Dock o [ ] Grid Grid # Column-span # Grid-columns # Grid-gutter # Grid-rows # Grid-size # Row-span o Height o Layer o Layers o Layout o [ ] Links Links # Link-background # Link-color # Link-hover-background # Link-hover-color # Link-hover-style # Link-style o Margin o Max-height o Max-width o Min-height o Min-width o Offset o Opacity o Outline o Overflow o Padding o [ ] Scrollbar colors Scrollbar colors # Scrollbar-background # Scrollbar-background-active # Scrollbar-background-hover # Scrollbar-color # Scrollbar-color-active # Scrollbar-color-hover # Scrollbar-corner-color o Scrollbar-gutter o Scrollbar-size o Text-align o Text-opacity o Text-style o Tint o Visibility o Width + [ ] Widgets Widgets o Button o DataTable o DirectoryTree o Footer o Header o Input o Label o List Item o List View o Placeholder o Static o Switch o TextLog o Tree * [ ] API API + App + Binding + Button + Color + Containers + Coordinate + Data table + Directory tree + Dom node + Events + Footer + Geometry + Header + Input + Label + List view + List item + Message pump + Message + Pilot + Placeholder + Query + Reactive + Screen + Scroll view + Static + Strip + Switch + Text log + Timer + Tree + Tree node + Walk + Widget * [*] Blog Blog + [ ] Archive Archive o 2023 o 2022 + [ ] Categories Categories o DevLog o News o Release Back to index Will McGugan Will McGugan CEO / code-monkey * Metadata * February 11, 2023 * in DevLog * 2 min read The Heisenbug lurking in your async codeP I'm taking a brief break from blogging about Textual to bring you this brief PSA for Python developers who work with async code. I wanted to expand a little on this tweet. If you have ever used asyncio.create_task you may have created a bug for yourself that is challenging (read almost impossible) to reproduce. If it occurs, your code will likely fail in unpredictable ways. The root cause of this Heisenbug is that if you don't hold a reference to the task object returned by create_task then the task may disappear without warning when Python runs garbage collection. In other words the code in your task will stop running with no obvious indication why. This behavior is well documented, as you can see from this excerpt (emphasis mine): create task But who reads all the docs? And who has perfect recall if they do? A search on GitHub indicates that there are a lot of projects where this bug is waiting for just the right moment to ruin somebody's day. I suspect the reason this mistake is so common is that tasks are a lot like threads (conceptually at least). With threads you can just launch them and forget. Unless you mark them as "daemon" threads they will exist for the lifetime of your app. Not so with Tasks. The solution recommended in the docs is to keep a reference to the task for as long as you need it to live. On modern Python you could use TaskGroups which will keep references to your tasks. As long as all the tasks you spin up are in TaskGroups, you should be fine. Previous So you're looking for a wee bit of Textual help... Copyright (c) Textualize, Inc Made with Material for MkDocs Insiders