A concurrency bug arises when overlapping tasks interact in an unsafe order. Even single-threaded asyncio code can expose one when shared state changes while a coroutine is suspended at await. section 11.6
Closing a socket during message delivery
A message-sending coroutine can still be using Eric’s socket when a disconnect coroutine closes it, causing a crash. Acquiring the same lock before sending and disconnecting makes the operations wait for one another. section 11.2
Use synchronization when refactoring shared state is not enough:
- Prefer immutable state or designs that avoid modifying shared data while a coroutine is suspended. section 11.2
