Section 19.4.9

Callback dependency deadlock across a workqueue

Definition

A callback dependency deadlock across a workqueue is a deadlock §9.14, a state in which each member of a group waits for an event only another member can cause, where one awaited resource is a work queue’s §6.36 worker thread. A work queue holds jobs, called work items, that its workers run one at a time. The deadlock forms when a callback §14.22, a function handed to other code to call later, holds the last free worker and waits for an event only a later work item on the same queue can produce.

Where it fits

Main entry: Deadlock (§9.14).

The Deadlock entry treats deadlock as a cycle of waits. Here callback AA runs on WW, the queue’s only worker, starts a request and waits for the request’s completion. The completion arrives as a new work item BB on the same queue, a common design for asynchronous completion §9.28, in which a call returns at once and a later event reports the result. Work item BB needs WW, which stays occupied until AA returns, so AA waits for BB and BB waits for AA. All four Coffman conditions, which every deadlock needs, hold: WW runs one work item at a time (mutual exclusion), AA keeps WW while waiting (hold and wait), nothing can take WW from AA (no preemption), and the waits form a cycle (circular wait). An event loop §14.23, a single thread that runs a handler for each queued event, deadlocks the same way when a handler waits for an event that the loop itself must deliver.

Lock ordering §9.15, a fixed global order for acquiring locks, does not prevent this deadlock, because it rules out cycles among locks and the held resource here is a thread. Adding workers only raises the threshold: with NN workers, the deadlock returns when NN callbacks wait for completions at once. The reliable repair removes the wait: AA starts the request and returns, and BB finishes the work.

Limits and common mistakes

A timeout §9.34 on the wait ends the hang but not the request, which may complete later and write into memory that the caller has released. Cancellation §9.29 needs the same care: if the confirmation of a cancellation arrives on the occupied queue, waiting for it recreates the deadlock.

Questions answered

Why does moving the request to another thread sometimes fix it?

Moving the request to another thread fixes the deadlock when the move frees the work queue’s worker, so that the queue can run the completion that the waiting code needs. In the failing design, a callback runs on the only worker thread of a work queue, a queue of pending jobs, called work items, that the worker runs one at a time. The callback starts a request and waits for the request’s completion. The completion is delivered as a new work item on the same queue and waits for the worker that the callback holds, so each waits for the other. When another thread waits instead, the callback returns and the worker runs the completion, which wakes that thread.

The move fails whenever one wait of the cycle survives: if the callback still waits for the other thread, if the other thread holds a lock that the completion code needs, if the other thread is only another work item on the same single-worker queue, or if the completion is delivered to a queue that the waiting thread itself serves. The lasting repair is to stop waiting inside the callback and to continue the work in the completion.

Related entries: Asynchronous completion (§9.28), Event loop (§14.23), Lock ordering (§9.15).