Section 9.34

Deadline versus timeout

Definition

A deadline is a point in time by which an operation is required to have finished. A timeout is a limit on how long one party waits for an event, such as a reply or a free lock, after which that party stops waiting and takes another course. A deadline is a requirement on the work; a timeout is a rule for the waiter. When a timeout expires, only the waiting ends: the expiry does not stop the awaited operation, which may still finish later.

The idea

A program asks a storage service to write a payment record and waits at most 2 seconds for the confirmation. At 2.0 s the wait expires and the program reports a failure. At 2.1 s the service finishes the write. The program, believing the write failed, sends it again, and the payment is now recorded twice. The timeout ended the program’s waiting. It did not end, undo or even observe the write.

Waiting on hold on the telephone is a fair picture. Hanging up after five minutes on hold is a timeout: the caller stops waiting, but an order the caller already placed may still be processed. A deadline is a statement about the order itself: it must ship by Friday. The picture stops being accurate in one respect. A shop may notice that a caller hung up, whereas a program’s timeout is invisible to the operation it awaited unless the program sends an explicit cancellation §9.29 request.

How it works

The deadline of a whole operation and the timeouts of its steps must agree. The safe pattern computes the deadline once and derives every timeout from it:

  1. When the operation starts, read a monotonic clock §9.35, a clock that never moves backward, and compute the absolute deadline: the start time plus the total budget.

  2. Before each step, read the clock again and compute the remaining budget: the deadline minus the current reading.

  3. If the remaining budget is zero or negative, do not start the step: the deadline has already been missed.

  4. Otherwise wait for the step at most the remaining budget.

Giving each step a fresh copy of the full budget instead lets an operation with kk steps take up to kk times the budget.

When a timeout expires, the waiter must decide what happens to the operation it was waiting for. The operation’s interface, its documented contract, determines which choices exist:

Two duties follow from the fact that the operation may still be running. First, memory or other resources lent to the operation, such as a buffer it writes into, may be reused or freed only after the operation reports that it has finished with them. Freeing a buffer on timeout lets a late completion write into memory that now belongs to something else. Second, a retry must not repeat an effect that already happened. Make the operation idempotent §8.31, so that performing it twice has the same effect as performing it once, for example by attaching a request identifier whose outcome the service remembers; or first ask the service whether the earlier attempt took effect.

Deadlines come in strengths. Missing a hard deadline is a failure of the system, for example an airbag that fires late; systems with such deadlines are called hard real-time systems §6.34. Missing a firm deadline makes the late result useless, but an occasional miss is tolerable. Missing a soft deadline makes the late result less valuable but still useful. The strength decides whether a miss is merely recorded, the late result is discarded, or the system is put into a safe state.

Worked example

An operation starts when a monotonic clock reads 100 s and has a total budget of 5 s, so its deadline is 100+5=105100 + 5 = 105 s. Step 1 ends at 102 s, which leaves 105−102=3105 - 102 = 3 s, so step 2 gets a 3-second timeout. If step 2 instead received a fresh 5-second timeout, it could end at 102+5=107102 + 5 = 107 s, which is 107−105=2107 - 105 = 2 s past the deadline. With three steps that each restart a 5-second timeout, the operation can last 3×5=153 \times 5 = 15 s against a budget of 5 s.

Suppose step 2 actually ends at 106 s. Before step 3 the remaining budget is 105−106=−1105 - 106 = -1 s: the deadline was missed by 1 s, and step 3 must not start. A program that stores times as unsigned 32-bit numbers of milliseconds computes this difference as 105,000−106,000105{,}000 - 106{,}000. Unsigned wraparound §4.16 turns the result into 232−1,000=4,294,966,2962^{32} - 1{,}000 = 4{,}294{,}966{,}296 ms, about 49.7 days, and the program would wait that long instead of stopping. Compare the two times before subtracting, or use signed arithmetic.

Back to the payment write from the idea: the timeout expires at 2.0 s and the write commits at 2.1 s, 2.1−2.0=0.12.1 - 2.0 = 0.1 s after the waiter gave up. If the request carried the identifier 7731 and the service stores the outcome of each identifier, a retry with identifier 7731 receives the stored outcome of the first attempt, and the payment is recorded once.

Stated precisely

Let rr be the start time of an operation and DD its total budget, both in seconds of one monotonic clock. The absolute deadline is d=r+Dd = r + D. The operation meets its deadline exactly when its completion time cc satisfies c≤dc \le d. At a clock reading tt, the remaining budget is d−td - t. A step that starts at tt gets the timeout max(0,d−t)\max(0,\ d - t), and a timeout of 0 means that the step is not started. If every wait is limited in this way, no wait ends later than dd.

A timeout of length τ\tau that starts at time ss expires at s+τs + \tau. Its expiry establishes one fact: the waiter observed no completion before s+τs + \tau. It establishes nothing about whether the operation failed, is still running or will complete. After expiry the outcome has three possible values, succeeded, failed and unknown, and only a later query or a completion report turns unknown into one of the other two.

These statements rest on two assumptions: the clock neither moves backward nor jumps forward during the operation, which a monotonic clock provides and a wall clock §9.36 does not; and arithmetic on clock values neither overflows nor wraps around.

Limits and common mistakes

A timeout is not cancellation. Expiry alone stops nothing: the work, its effects and its use of borrowed memory continue until the operation itself stops.

A timeout is not a failure report. A timed-out write may have succeeded. Treating a timeout as a failure and repeating an operation that is not idempotent duplicates its effect.

A deadline is not enforcement. Writing down a deadline, or detecting that it was missed, makes nothing finish sooner. Meeting a hard deadline needs a schedulability §9.37 argument: proven bounds on execution times §9.32 and a scheduler analyzed to finish the work in time.

Use a monotonic clock for deadlines and timeouts. A wall clock, which shows the date and time and can be corrected, can jump forward and expire every pending timeout at once, or jump backward and extend them all.

Timers have a granularity. A kernel that counts time in ticks of 10 ms rounds a requested timeout to a whole number of ticks; rounding a 15 ms request up gives 2 ticks, which is 20 ms. The waiting thread also resumes only when the scheduler next runs it, which can be later still.

Timeouts that are too short cause retries, and retries add load to a system that is already slow, which makes still more requests time out. Waiting longer before each successive retry, for example doubling the pause each time, limits this.

An infeasible budget fails every attempt. If a step always needs at least 4 s and the remaining budget is 3 s, starting the step only wastes work.

Questions answered

What happens to the underlying task when the observer times out?

Nothing happens to the underlying task because of the timeout itself: a timeout, a limit on how long the observer waits, ends only the observer’s wait. The task, meaning the operation the observer was waiting for, keeps running unless something else stops it. It may complete normally after the observer has stopped waiting, fail, or run until a separate mechanism stops it. What can be done depends on the task’s interface: the task may accept a cancellation request, which it honors only at points where stopping is safe; a supervisor may terminate or isolate it; or the observer may leave it running and collect or ignore its late result.

Until the task reports that it has finished or stopped, the observer must assume that the task can still use any buffer or other resource it was given, and can still produce its effect. So the observer must not free shared memory on timeout, must not report that the action did not happen, and must not repeat an action that is not idempotent (safe to perform twice) without first finding out whether the earlier attempt took effect.

Related entries: Cancellation (§9.29), Monotonic clock (§9.35), Asynchronous completion (§9.28), Idempotency (§8.31), Unsigned modular arithmetic (§4.16), Real-time system (§6.34).