bugger.
All challenges/Pythonhardthe-shutdown-that-never-happened-v1
THE INVESTIGATION

The shutdown that never happened

A request waiter reports success during shutdown when completion and cancellation land in the same event-loop turn.

ConcurrencyCancellationState machinesRace conditions

What’s going wrong

A service sometimes continues processing after its supervisor cancels a request. Ordinary cancellation and ordinary request completion work; the failure appears when a child request finishes before the waiting task next resumes. Reproduce the race with this deterministic event trace, without threads or sleeps.

Input is JSON with `events`, an ordered list of arrays (at most 200). One waiter and one child start pending. `['complete', value]` successfully finishes a pending child with any JSON value. `['fail', message]` finishes a pending child with an error string. Completion events for an already finished child do nothing. `['cancel']` requests external cancellation of the waiter. `['timeout']` fires its deadline only if both waiter and child are still pending. Neither event automatically resumes the waiter. `['resume']` executes one waiter step. `['cleanup']` acknowledges a previously requested child cancellation by changing a still-pending child to `cancelled`; without such a request it does nothing, and it does not resume the waiter. Event names use JSON double quotes in actual input.

A pending waiter must honor external cancellation even if the child finishes before resumption. If the child is still pending, request its cancellation and remain waiting until it finishes; otherwise commit outcome `cancelled`. Only deadline cancellation is translated to `timeout`: when resumed after a deadline, an already finished child keeps its value/error, while a pending child is asked to cancel. After that cleanup starts, child cancellation produces `timeout`, but a child that instead completes or fails produces its value/error. External cancellation received before any terminal outcome takes precedence over this deadline handling. Once an outcome is committed, later events cannot replace it.

Print one JSON object: `outcome` is `waiting`, `value`, `error`, `cancelled`, or `timeout`; `result` is the successful value or error message, otherwise null; `child` is `pending`, `value`, `error`, or `cancelled`; `cancelChild` is true if the waiter ever requested child cancellation. Do not cancel a child that has already finished. Fix the lifecycle decision without breaking cleanup or deadline behavior.

Expected behavior

An uncommitted external cancellation survives a concurrent child completion or failure; deadlines retain their separate semantics, pending children are cleaned up, and committed outcomes remain stable.

Reproduction

01 Shutdown races with a finished request
{"events":[["complete","invoice-42"],["cancel"],["resume"]]}
{"outcome":"cancelled","result":null,"child":"value","cancelChild":false}
02 Ordinary completion commits before shutdown
{"events":[["complete","invoice-42"],["resume"],["cancel"],["resume"]]}
{"outcome":"value","result":"invoice-42","child":"value","cancelChild":false}
03 Deadline waits for child cleanup
{"events":[["timeout"],["resume"],["cleanup"],["resume"]]}
{"outcome":"timeout","result":null,"child":"cancelled","cancelChild":true}

Before you begin

  • Task cancellation and cleanup
  • Event-loop scheduling
  • State machines and exception ownership

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.