Distinguish timeout, failure, cancellation, and uncertain mutation outcomes.
Section 2.1 1 of 7
Overview
Focus
Distinguish timeout, failure, cancellation, and uncertain mutation outcomes.
Your route through the lesson
2.2 Awaiting results and propagating cancellation
2.3 Timeouts and structured failure types
2.4 Retries, operation status, and unknown outcomes
2.5 Put it into practice
2.6 Guided comparison
2.7 A timeout after a committed operation
All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.
Section 2.2 2 of 7
Awaiting results and propagating cancellation
Focus
Understand waiting and cancellation without assuming that stopping a caller undoes an effect.
An asynchronous operation can start now and finish later. In Python, async def defines a coroutine function; await pauses that coroutine until an awaited operation completes, allowing other work to proceed. It does not mean “pretend the result is already available.” The caller must still inspect the returned status.
Cancellation asks work to stop. It may be honored before an effect, after partial work, or too late to prevent a committed change. A cancellation acknowledgement should therefore distinguish request cancellation from confirmed absence of effects. Closing a user interface does not automatically roll back a remote operation.
Suppose a report job is queued, then canceled before processing. Its status can safely be canceled with zero effects. If the report was already sent, stopping the local waiting coroutine does not unsend it. The runtime should query operation status and explain the observed outcome. The lab uses harmless local state so these stages can be tested deterministically rather than inferred from timing luck.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 2.3 3 of 7
Timeouts and structured failure types
Focus
Represent timeout, explicit failure, and unknown mutation outcomes differently.
A timeout says the caller did not receive a result before its deadline. An explicit failure says the operation reported failure. An unknown outcome says the caller cannot yet determine whether an effect occurred. These categories overlap: a mutation timeout often creates an unknown outcome until status is checked.
Consider a refund simulator. It commits a refund and writes a durable operation result, then the response is lost. The caller times out. Reporting “refund failed” would be inaccurate; reporting “refund succeeded” without checking would also lack observed evidence. The next action is an authorized status read using the same logical operation identifier.
Structured errors make this distinction usable. Store category, operation identifier, retry guidance, and safe details. A malformed argument is generally not repaired by retrying unchanged. A transient read failure may allow a bounded retry. An uncertain mutation needs reconciliation before repetition. Error text should support correct control flow rather than being treated as ordinary successful tool content because it contains words.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 2.4 4 of 7
Retries, operation status, and unknown outcomes
Focus
Query status and preserve operation identity before retrying an uncertain mutation.
A retry repeats work after a failure or lost response. Reads are often safe to retry within limits because they do not create effects. Mutations require stronger care. Use a durable logical operation key so repeated delivery of the same request can return the original result rather than create another effect.
For operation refund-8, the simulator atomically stores the effect and its committed result. If the response is lost, status(refund-8) reveals committed. The caller uses that result and does not submit a fresh refund under refund-9. This small simulator does not persist pending or running states. In a larger service that exposes those states, a documented not-found result may permit a same-key retry, while a running result calls for bounded polling rather than duplicate execution.
Transport request identifiers and operation keys serve different purposes. A new network request can need a new request identifier while retaining the same logical operation key. Preserve both in traces. Backoff delays reduce repeated load, but delay alone does not prevent duplicate effects; deduplication and status reconciliation address that separate problem.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 2.5 5 of 7
Put it into practice
Focus
Run an asynchronous lost-response case and reconcile the durable status.
Save week10.py in your exercise folder and run python week10.py --lesson 2. The lab uses asyncio, Python’s standard-library event-loop support, and a temporary SQLite operation table. It simulates a commit followed by a lost reply. A status lookup then finds the committed result, and a retry with the same operation key creates no second effect.
asyncio.run starts and closes an event loop for a top-level coroutine. asyncio.sleep yields control without blocking the whole loop:
The full file also demonstrates cancellation before an effect and timeout after a synthetic commit. Expected output shows one logical refund effect, a committed status, and a canceled-before-effect job. The simulated reply loss is intentional; it is not evidence about real network reliability. All monetary values are synthetic integer cents inside a temporary database.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 2.6 6 of 7
Guided comparison
Focus
Compare failures by whether an effect is known, absent, or still uncertain.
The completed normal trace submits refund-8 for 300 cents, commits once, and receives the result. The lost-response trace commits the same kind of operation but raises a simulated timeout before the caller receives it. Status returns committed with 300 cents. Reusing the original operation key returns that stored result, leaving the effect count at one.
The canceled-before-effect trace stops a queued coroutine before it writes the refund. Its confirmed local status is canceled and effect count zero. These cases should not all be labeled failed. Their effect states differ, and that determines safe recovery.
Now consider a genuinely unreachable status service. The honest outcome remains unknown, and the runtime stops within its budget rather than blindly generating new operation keys. A user-facing explanation should say that confirmation is pending and avoid claiming either a completed refund or a guaranteed absence of a refund. Engineering acceptance includes representing uncertainty correctly, because an invented definitive answer can trigger a duplicate effect or an unnecessary manual action.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 2.7 7 of 7
A timeout after a committed operation
Focus
Recover from a timeout that happened after the operation committed.
A local simulator accepts transfer-preview operation op-22, records its result, then loses the reply. The caller sees a timeout. Its trace includes the operation key, so it asks status(op-22). The store returns committed and the original result. No new effect is needed.
The completed recovery uses a new request to read status but retains op-22 as the logical identity. If the caller must redeliver the original operation under the documented contract, it again supplies op-22 with identical parameters. A different destination or amount is not a retry of the same action and must be rejected under that key.
This case prepares the next lesson’s durable idempotency and approval rules. A timeout is a communication observation; a commit is an effect observation. Keeping those facts separate allows honest recovery without panic-driven repetition. The pattern applies to message sends, report generation, ticket creation, and other actions whose response can be lost after the external system has already acted.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Your personal notes
Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.