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.
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.
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.
Request cancellation through a channel that reaches every part, as listed below.
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.
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.
A process. The channel is a signal, a numbered notification that the kernel, the core of the operating system, delivers to a process.
SIGTERMasks the process to terminate; the process can catch it and clean up, or ignore it.SIGKILLends the process and cannot be caught or ignored. The end record is the exit status §13.24 that the parent’s wait call,waitpid, returns only after the child has terminated.All descendants. A signal sent to one process does not reach its descendants. A cgroup §13.20 (Linux control group) is a set of processes that the kernel tracks. A child starts in its parent’s cgroup and stays there unless a process with permission to write the cgroup’s files moves it. Writing
1to the cgroup’scgroup.killfile kills every member. The linepopulated 0in itscgroup.eventsfile is the end record: no live process remains in it.A thread. Most systems offer no safe way to stop a thread at an arbitrary point from outside; the thread’s own code must reach a point where it checks for a cancellation request, such as a flag, and return. The end record is a completed join, a call that returns only after the thread has finished.
A job on a device or another computer. The channel is the cancel message of that job’s protocol. The end record is an acknowledgment that names the job and states its final state: stopped before it took effect, completed, or undone.
Worked example
A controller starts worker at time 0 with a timeout of 5 s. While it runs, appends 10 records per second to a shared result file.
At 5 s the wait returns a timeout error.
At 6 s the controller sends
SIGTERMto .finishes its current step and exits at 8 s, and the wait call reports its exit status at 8 s.
kept running for s after the timeout. Cancellation took s from request to end. In the 3 s after the timeout, appended records. A controller that started the next attempt at 5 s ran both attempts together for 3 s, and the 30 late records of 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 , written after its timeout.
Now let have started a helper process that uploads its records. SIGTERM went to only, so still runs after 8 s, and the exit status of says nothing about . With and 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 be the time at which the timeout expires, the time at which cancellation is requested, the time at which the last part of the worker set actually stops, and the time at which the waiter holds an end record for every part. In the first version of the example, s, s and s.
The timeout carries no information about . The worker may have finished before , with its result lost or late; it may stop at any time after ; or it may never stop.
An end record reports a stop that has already happened, so .
Work that must not overlap the worker may start at or later. Starting it at is safe only by luck.
The waiter may conclude that the worker has stopped when three conditions hold.
Complete membership. The worker set contains every part that can act for the worker, and no new part can join it after the check.
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.
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 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.
For a child process, the end record is the exit status returned by the parent’s wait call (
waitpidon systems that follow POSIX, the Portable Operating System Interface, Linux among them), which returns only after the process has terminated.For the process and all its descendants, it is evidence that none of them remains, such as a Linux control group (cgroup) that reports
populated 0.For a thread, it is a completed join, a call that returns only when the thread has finished.
For work handed to a device or another computer, it is an acknowledgment from that device or computer that names the job and states its final state: stopped before it took effect, completed, or undone.
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).