liveBook · Manning

Concurrency bug

As explained in Python Concurrency with asyncio

A concurrency bug is a software defect caused by interactions among overlapping or concurrently executing tasks.

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

How Python Concurrency with asyncio covers it

How deep it goes
Explains race timing, critical sections, event-loop mistakes, and lock-based repair, but does not develop a general debugging procedure.
Examples
Uses competing message and disconnect coroutines, mock sockets, critical sections, and lock-acquisition output.
What it assumes
Builds on asyncio basics, including coroutines, awaits, tasks, and event loops.
What you can do after
Diagnose and prevent asyncio races by protecting critical sections with locks or restructuring shared state.
How it connects
Connects asyncio coroutines with race conditions, shared state, locks, event loops, semaphores, events, and conditions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.