Section 13.25

Timeout versus cancellation

Definition

A timeout §9.34 is a limit on how long one party, the waiter, waits for an operation to finish; when the limit passes, the waiter stops waiting and takes another course. Cancellation §9.29 is a request, sent to the operation itself, to stop. The difference that matters is what each acts on. A timeout acts only on the waiter, so the operation may keep running and causing effects. Cancellation acts on the operation, but only as a request: the operation has stopped only when the waiter receives a report that it ended.

The idea

A test controller starts a measurement program as a separate process and waits at most 5 seconds for it to finish. At 5 seconds the wait returns with a timeout error. The controller records the attempt as failed and starts the next attempt at once. Nobody told the first program to stop. It is still running, still appending to the shared result file and still sending commands to the device under test. Two attempts now run at the same time, and their records mix in one file. The timeout error never said that the program had stopped; the controller assumed it.

A restaurant gives the picture. A diner who gives up waiting and walks out has applied a timeout: the kitchen keeps cooking. A diner who asks the waiter to cancel the order has made a cancellation request: the kitchen may stop, or the meal may already be plated. Only the waiter’s answer, “canceled” or “too late”, tells the diner which happened. The picture stops being accurate in one respect: a program can hand work to helpers, such as other processes or a job on a device, and a request that reaches the program does not reach those helpers. Each helper must be stopped, and its end confirmed, separately.

How it works

In this entry the worker is everything the waiter started for the operation. An end record is a report, from the component that runs a part of the worker, that this part has ended. Stopping a worker reliably takes five steps.

  1. List the worker set. Name every part that does the work: the process itself; its descendants, meaning every process it started, directly or through other processes; its threads, the separately scheduled sequences of instructions inside one process; and every job it handed to a device or another computer.

  2. Stop waiting, keep the resources. When the timeout expires, the waiter records that the operation did not finish in time, but keeps the worker’s files, ports and devices reserved.

  3. Request cancellation through a channel that reaches every part, as listed below.

  4. Wait for an end record for every part, as listed below. If none arrives within a second time limit, use a stronger channel, such as a forced kill or a reset of the device. If even that brings no end record, record the worker’s state as unknown and keep its resources reserved.

  5. Release. Only after an end record has arrived for every part, release the resources and start any work that must not overlap the worker.

On Linux and other systems that follow POSIX (Portable Operating System Interface, the standard interface of Unix-like operating systems), the channels and end records are these.

Worked example

A controller starts worker WW at time 0 with a timeout of 5 s. While it runs, WW appends 10 records per second to a shared result file.

WW kept running for 8−5=38 - 5 = 3 s after the timeout. Cancellation took 8−6=28 - 6 = 2 s from request to end. In the 3 s after the timeout, WW appended 3×10=303 \times 10 = 30 records. A controller that started the next attempt at 5 s ran both attempts together for 3 s, and the 30 late records of WW are mixed with the records of the next attempt. A controller that waited for the end record started the next attempt at 8 s. The attempts did not overlap, and the 30 records are known to belong to WW, written after its timeout.

Now let WW have started a helper process HH that uploads its records. SIGTERM went to WW only, so HH still runs after 8 s, and the exit status of WW says nothing about HH. With WW and HH in one cgroup, the controller sees at 8 s that the cgroup is still populated, kills the cgroup at 9 s, reads populated 0 at 9.1 s, and only then starts the next attempt.

Stated precisely

Let tdt_d be the time at which the timeout expires, tct_c the time at which cancellation is requested, tet_e the time at which the last part of the worker set actually stops, and tot_o the time at which the waiter holds an end record for every part. In the first version of the example, td=5t_d = 5 s, tc=6t_c = 6 s and te=to=8t_e = t_o = 8 s.

The waiter may conclude that the worker has stopped when three conditions hold.

  1. Complete membership. The worker set contains every part that can act for the worker, and no new part can join it after the check.

  2. Authoritative end records. Each end record comes from the component that runs that part: the kernel for a process, the thread library for a thread, the remote system or device for a job. The absence of output for some time is not an end record.

  3. Stable identity. Each end record refers to the part that was started, not to a later part that received the same identifier.

Stopping is not undoing: every effect the worker caused before tet_e remains in place.

Limits and common mistakes

A timeout result is not a stop. In Python §12.23, a future is the object that stands for a call handed to a pool of worker threads or processes. Its result method raises TimeoutError when its timeout passes, and the call keeps running; its cancel method returns False, meaning nothing was canceled, once the call has started. In Python’s subprocess module §12.40, run with a timeout kills and waits for the child it started, but not that child’s own descendants.

Killing a parent does not kill its children: on Linux an orphaned child keeps running and another process adopts it. Closing a terminal sends the hang-up signal SIGHUP, which a program can ignore, as the nohup command arranges.

Silence and process numbers are weak evidence. A worker that has written nothing for 10 s may be blocked, not stopped. A process identifier (PID) can be given to a new process once the old process has ended and its exit status has been collected, so a check or a kill by number can reach an unrelated program. Even SIGKILL is delayed for a process in an uninterruptible wait inside the kernel, such as a wait for a disk that has stopped responding: the process ends only when that wait ends.

Cancellation is not rollback. The operation may have taken effect before the request arrived, with only its reply lost. Repeating it is then safe only if the operation is idempotent §8.31, meaning that doing it twice has the same effect as doing it once, or carries an operation identifier that the receiver uses to discard repeats. Likewise, killing the host program does not stop a command that a device has already accepted, such as erasing a memory region; that needs the device’s own cancel command or a reset, and an end record from the device.

One measurement is not a bound. The 2 s from request to end in the example describes one run. A guaranteed stopping time needs a mechanism that enforces it, such as a forced kill followed by a hardware reset, and a fault model §1.30: a stated list of the failures the guarantee covers.

Questions answered

What evidence shows a timed-out worker has actually stopped?

A timed-out worker has actually stopped only when the waiter holds an end record for every part of the worker: a report, from the component that runs that part, that the part has ended.

None of the following shows that the worker stopped: the timeout error itself, which ends only the waiting; killing the parent process, which leaves its children running; closing the terminal, whose hang-up signal can be ignored; a period without output; or the absence of a process number, since numbers are reused. If no end record arrives, record the worker’s state as unknown and do not start work that must not overlap the worker. Even a confirmed stop does not undo what the worker already did.

Related entries: Process closure (§13.23), subprocess (§12.40), Idempotency (§8.31), Exactly-once effect (§8.32), Cancellation (§9.29), Deadline versus timeout (§9.34), cgroup (§13.20), Exit status (§13.24).