Skip to content
EngineeringEmad Khair17 min read

What Happens to Work After a Timeout?

Timeouts can return errors while work keeps running. We trace cancellation across JavaScript, Python, Go, and Rust, and show how to avoid the fallout.

What Happens to Work After a Timeout?

A timeout is one of the simplest ways to protect an application from a slow operation. Give a request a deadline, stop waiting when that deadline expires, and return an error.

But what happens to the operation after the error is returned?

To investigate, we ran a small experiment. An operation would take 350 milliseconds to complete, while its caller would wait for only 100 milliseconds. We ran equivalent versions in Node.js and Python.

Both produced nearly identical results:

text
Node.js 22.16.0
100ms  Caller timed out
350ms  Work completed

Python 3.13.5
101ms  Caller timed out
351ms  Work completed

The timeout worked as configured: the caller regained control after approximately 100 milliseconds. The underlying work continued for another quarter of a second.

That distinction can be easy to overlook because most timeout implementations are evaluated by measuring how quickly they return. Once the caller receives an error, the application often stops tracking the operation that produced it.

For a timer, the consequence is harmless. For a database query, background worker, or request that modifies persistent state, it can lead to wasted resources, overlapping retries, and operations that complete long after their callers have abandoned them.

JavaScript, Python, Go, and Rust all provide mechanisms for limiting asynchronous work. Their implementations differ, but the underlying problem is shared: ending a wait does not necessarily end the work being waited on.

Night over an Icelandic coast under green aurora. On the caller island the octopus mascot stands by a clock post marked 100 ms, caller stops waiting. A boat carrying one parcel is already past the caller's dock and still crossing toward the server island, where a processing station is marked 350 ms, work completes.

The caller's deadline and the operation's lifetime are different measurements.

Reproducing the problem in JavaScript

JavaScript provides a convenient way to impose a timeout on an arbitrary promise using Promise.race():

javascript
function withTimeout(operation, ms) {
  return Promise.race([
    operation,
    new Promise((_, reject) => {
      setTimeout(() => reject(new Error("Timed out")), ms);
    }),
  ]);
}

The function creates two promises. One represents the operation, and the other rejects after a specified duration. Whichever settles first determines the value returned to the caller.

We tested it with an operation that takes 350 milliseconds:

javascript
const start = performance.now();

function log(message) {
  const elapsed = Math.round(performance.now() - start);
  console.log(`${elapsed}ms  ${message}`);
}

const work = new Promise((resolve) => {
  setTimeout(() => {
    log("Work completed");
    resolve();
  }, 350);
});

try {
  await withTimeout(work, 100);
} catch {
  log("Caller timed out");
}

The caller reports a timeout after approximately 100 milliseconds, but the operation still prints its completion message around 350 milliseconds.

There is no unusual scheduling behavior involved. This follows directly from the contract of Promise.race(). A promise represents an eventual result. Settling the race does not instruct the losing promises to stop, and the timer scheduled by work remains in the event loop.

The same problem appears in HTTP code when a fetch is merely raced against a timeout promise:

javascript
await Promise.race([
  fetch(url),
  timeout(1000), // An ordinary promise that rejects after 1 s
]);

If the timeout wins, the returned promise rejects. The fetch has not been instructed to abort. The timeout's own timer should also be cleared if the operation finishes first; otherwise it is another piece of work left behind.

Two wooden piers on the same 0 to 400 millisecond scale. The upper pier, Caller waiting, ends at 100 ms with an orange pennant. The lower pier, Underlying work, carries a row of pearls without a break past 100 ms and ends at 350 ms.

Promise.race() settles the result at the first event; it does not cancel the other operation.

JavaScript provides AbortController and AbortSignal for APIs that support cancellation. With fetch(), the timeout can be attached to the operation itself:

javascript
const response = await fetch(url, {
  signal: AbortSignal.timeout(1000),
});

When the signal expires, the Fetch API can abort its work and reject the associated promise. This is stronger than racing two unrelated promises.

It does not, however, establish that the remote server stopped processing the request. The server might already have received it or committed a transaction before it learned that the client had disconnected.

Python cancels a coroutine, but not its worker thread

Python's asyncio uses a different cancellation model. asyncio.wait_for() requests cancellation when the deadline expires. A coroutine suspended at an await point can receive asyncio.CancelledError and run cleanup code before terminating.

python
import asyncio

async def work():
    try:
        await asyncio.sleep(10)
    finally:
        print("Cleaning up")

async def main():
    try:
        await asyncio.wait_for(work(), timeout=0.1)
    except TimeoutError:
        print("Timed out")

asyncio.run(main())

Here cancellation interrupts the coroutine while it is suspended in asyncio.sleep(). Its finally block executes before the caller receives the timeout.

Now move the work into a thread:

python
import asyncio
import time

start = time.perf_counter()

def blocking_work():
    time.sleep(0.35)
    print(f"{(time.perf_counter() - start) * 1000:.0f}ms  Work completed")

async def main():
    try:
        await asyncio.wait_for(
            asyncio.to_thread(blocking_work),
            timeout=0.1,
        )
    except TimeoutError:
        print(f"{(time.perf_counter() - start) * 1000:.0f}ms  Caller timed out")

asyncio.run(main())

The asyncio task waiting for the result is cancelled. Once the blocking function has started in the thread, however, its execution is not forcibly terminated. In our run, the caller timed out at 101 milliseconds and the thread printed its completion message at 351 milliseconds.

Python cannot generally kill an arbitrary thread safely. It might own locks, be modifying shared objects, or be in the middle of code that has no cancellation mechanism. The worker is allowed to finish independently.

There is another subtlety: asyncio.wait_for() normally waits for cancellation to finish. A coroutine that performs lengthy cleanup, or deliberately suppresses cancellation, can cause it to exceed its nominal timeout. The deadline is not necessarily a hard bound on the total time spent in wait_for().

Go signals that work should stop

Go approaches cancellation through context.Context. A context can carry a deadline down a call chain, and its Done() channel closes when that context is cancelled.

But a function that merely accepts a context is not automatically cancellable:

go
func work(ctx context.Context) {
    time.Sleep(350 * time.Millisecond)
    fmt.Println("Work completed")
}

This function never reads ctx. If a caller starts it in a goroutine and stops waiting after 100 milliseconds, the goroutine continues sleeping.

A cooperative version waits for either completion or cancellation:

go
func work(ctx context.Context) error {
    select {
    case <-time.After(350 * time.Millisecond):
        fmt.Println("Work completed")
        return nil
    case <-ctx.Done():
        return ctx.Err()
    }
}

This function can return promptly when cancellation arrives first. More complicated functions must check cancellation around their own blocking points, and operations that have already committed a side effect cannot necessarily be reversed.

Go's approach is especially useful when requests span multiple layers. A context can pass from an HTTP handler to database queries and outgoing HTTP requests. For example, a request created with http.NewRequestWithContext() can respond to cancellation in supported parts of the HTTP client's work.

The guarantee extends only as far as the components that observe the context. Arbitrary functions, external processes, and libraries that ignore it remain unaffected.

Rust can drop a future without stopping independent work

Rust futures represent computations advanced by polling. Tokio's timeout() wraps a future with a deadline:

rust
use std::time::Duration;
use tokio::time::{sleep, timeout};

async fn work() {
    sleep(Duration::from_millis(350)).await;
    println!("Work completed");
}

#[tokio::main]
async fn main() {
    let result = timeout(
        Duration::from_millis(100),
        work(),
    ).await;

    if result.is_err() {
        println!("Caller timed out");
    }
}

When the timeout expires, the wrapped future is dropped. The remaining instructions in work() are not executed, so the completion message does not appear.

Dropping a future stops that future from being polled, but does not necessarily undo things it has already initiated. There is also a scheduling qualification: Tokio checks its timeout when it can poll the future and timer. A future that runs long synchronous code without yielding may exceed the deadline before the timeout is observed.

The limit is clearer with Tokio's spawn_blocking():

rust
use std::{thread, time::Duration};
use tokio::{task, time::timeout};

#[tokio::main]
async fn main() {
    let worker = task::spawn_blocking(|| {
        thread::sleep(Duration::from_millis(350));
        println!("Work completed");
    });

    let result = timeout(
        Duration::from_millis(100),
        worker,
    ).await;

    if result.is_err() {
        println!("Caller timed out");
    }
}

The timeout is applied to the handle used to await the worker. When it expires, the handle is dropped; an already-running blocking task continues. Tokio documents that spawn_blocking() tasks cannot be forcibly aborted after they start.

Rust's ability to cancel an owned future is useful. It does not provide a general mechanism for terminating a function executing in another thread, or for reversing a request already processed by another service.

Four implementations, one boundary

The behavior depends on the specific API, not merely the programming language:

Runtime and API Effect of timeout What may keep running
JavaScript Promise.race() Settles the returned promise Losing operations
Python asyncio.wait_for() Requests task cancellation Blocking threads and cancellation-resistant tasks
Go context.WithTimeout() Cancels the context Functions or goroutines that do not observe it
Rust Tokio timeout() Drops the wrapped future on expiry Independently spawned or blocking operations

There are three distinct events in this process:

  1. Deadline expiration: The caller has reached its limit for waiting.
  2. Cancellation: The caller or runtime requests that the operation stop, or drops the computation it owns.
  3. Termination: The operation has actually stopped doing work.

Those events need not happen at the same time. The second may not occur at all, and the third may be delayed, impossible to enforce safely, or unknowable to the caller.

Four lanes share one orange deadline line. JavaScript Promise.race(): the awaiting caller stops, the losing promise keeps running. Python asyncio.wait_for(): the coroutine is cancelled, the to_thread() worker keeps running. Go context.WithTimeout(): a worker that checks ctx.Done() stops, a worker that ignores ctx keeps running. Rust tokio::time::timeout(): the owned future is dropped, a spawn_blocking() task keeps running.

Every runtime has a boundary beyond which cancellation is not automatic.

So far, these examples have focused on computations within one process. Network requests introduce another problem: the caller may no longer know whether the operation completed at all.

When a server commits after the client gives up

Suppose a client sends an HTTP request to create a payment. It gives the request five seconds to complete. The server receives it, validates the input, and begins a transaction.

At five seconds, the client times out. It closes the connection and records a failure. But the server may already be in the middle of a transaction it can finish independently of that connection.

If the client retries, both attempts might commit:

text
Time       Client                     Server
-------------------------------------------------------------
0s         Sends request              Receives original request
2s                                    Begins original transaction
5s         Times out; disconnects
6s         Retries                    Receives retry
8s                                    Original transaction commits
9s                                    Retry transaction commits

The client has observed one timeout and one successful retry. The server may have created two payments.

No client-side timeout can establish whether the original request committed. The disconnect might occur before processing, during processing, or after the transaction completes but before the response reaches the client. This is an uncertainty created by the network boundary, not a peculiarity of any one programming language.

The mascot on the caller island stands by a clock post marked Timeout 5 s. Two boats carry identical payment parcels to the server island: the Original, striped blue, is already at the dock and the Retry, striped orange, follows. Two receipts on the server island are stamped 8 s and 9 s. A timeline below shows the Original running past the 5 s timeout to Commit 8 s, and the Retry sent at 6 s reaching Commit 9 s.

A timeout leaves the outcome uncertain; an uncoordinated retry can repeat the side effect.

The usual defense is to make retries safe. For a side-effecting endpoint, the client can send an idempotency key identifying the logical operation, not the individual HTTP attempt:

http
POST /payments
Idempotency-Key: payment-order-4821

A retry uses the same key. The server recognizes both requests as attempts to perform one logical payment and coordinates them so the operation is not duplicated.

The implementation must be concurrency-safe. A separate "check whether key exists" followed by "create payment" is insufficient if two attempts can pass the check simultaneously. A transactional unique constraint, a durable operation record, or equivalent atomic coordination must decide which attempt owns the operation. The key should also be bound to the original request parameters to prevent accidental reuse for a different payment.

Idempotency does not terminate the first request. It ensures that retrying after an uncertain outcome does not create a second side effect.

The capacity cost of abandoned work

Duplicate transactions are one consequence of ineffective cancellation. Resource exhaustion is another.

Suppose a service receives 100 requests per second. Each request starts a separate operation that runs for 30 seconds, but the caller times out after one second.

If work continues after the timeout, the steady-state number of active operations is approximately:

text
100 operations/second × 30 seconds = 3,000 active operations

If operations can be stopped after one second, that number is closer to:

text
100 operations/second × 1 second = 100 active operations

These are idealized calculations at a constant arrival rate, assuming operations start immediately and have fixed lifetimes. Real systems encounter thread limits, queues, CPU contention, connection pools, and other bottlenecks; those constraints turn surplus work into waiting and saturation.

The key observation remains: a one-second timeout at the caller can coexist with 30 seconds of resource usage in the underlying system.

A chart over a night harbor. At 100 operations arriving per second, the orange line for work that continues 30 seconds rises steadily to 3,000 active operations at 30 seconds and levels off. The green line for work that ends after 1 second reaches 100 at 1 second and levels off. Model: 100 operations/s, immediate starts, fixed durations.

Returning an error quickly does not necessarily reduce the amount of outstanding work.

Retries can amplify the problem. As the service slows down, more callers time out. Some retry, creating additional operations while the original work still consumes resources. Increased load makes the service slower still.

A request-latency metric may not reveal this feedback loop, because the measurement ends when the caller receives an error. We also need to track how long underlying work remains alive.

Passing deadlines through the entire request

There is no single timeout wrapper that can cancel every operation. A more reliable design starts by treating the deadline as a property of the whole request.

Imagine an incoming request has a five-second deadline and calls three services sequentially. If each service starts a fresh five-second timeout, the full operation can last substantially longer than intended.

Instead, each layer should receive the remaining budget from the original deadline:

text
Request starts                0 ms
Overall deadline          5,000 ms

Authentication       0  ->  400 ms
Database query    400  -> 1,900 ms
External API     1,900 -> 5,000 ms

The external API receives at most the time remaining in the original budget, not another five seconds. Across process boundaries, services need a consistent way to communicate a deadline or remaining-time budget, and to account for clock differences.

A deadline must also reach the API that can actually interrupt the work. In JavaScript, that often means passing an AbortSignal. In Python, it means allowing coroutine cancellation to propagate and designing blocking workers to cooperate separately. In Go, downstream functions need to observe their caller's context. In Rust, code must distinguish cancelling an owned future from merely dropping the handle to an independent worker.

Two piers on one 0 to 15 second scale pass the same three stations: Authentication, Database and External API. Top row, Resetting each timeout: each station has its own hourglass and a fresh 5 seconds, 0 to 5 s, 5 to 10 s and 10 to 15 s. Bottom row, Shared deadline: the mascot carries one clock and a single 5 s budget covers Authentication 0.0 to 0.4 s, Database 0.4 to 1.9 s and External API 1.9 to 5.0 s.

A deadline propagated through a call chain prevents downstream services from restarting the full waiting budget.

Cleaning up work that can be cancelled

Stopping computation is only part of the problem. A cancelled operation may still hold locks, own temporary files, have an open connection, or have partially changed application state.

Cancellation-aware code needs cleanup paths that run when an operation is interrupted:

  • In Python, try/finally can release resources when a coroutine receives cancellation, provided cancellation is allowed to propagate.
  • In Go, defer can perform cleanup when a function returns after observing its context.
  • In Rust, owned resources are normally released when a future is dropped, although asynchronous cleanup may require additional coordination.
  • In JavaScript, finally blocks and API-specific abort or disposal mechanisms help release resources after cancellation-aware operations settle.

Blocking jobs that cannot be safely stopped need a different defense: bounded concurrency, bounded queues, and sometimes isolation in separate worker processes. Terminating a worker process may stop its local computation, but it still cannot reverse an external transaction that already committed.

Testing what happens after the timeout

A typical timeout test verifies that a caller receives an error within the configured duration. That proves the waiting deadline works. It says little about the operation's lifetime.

A better test continues observing the system after the timeout. Start a worker that would perform a visible side effect after a delay; cancel it beforehand, then check whether the side effect occurred. For an operation that cannot be interrupted, retry with the same idempotency key and verify that only one logical effect is committed.

The most revealing tests exercise races near the deadline. An operation can finish just before cancellation arrives, commit while the connection closes, or observe a cancellation signal after completing some of its work. All permitted outcomes should leave the system in a valid state.

Instrumentation should similarly distinguish the moment a caller times out from the moment the underlying work stops. Useful signals include cancellation requests, task termination, in-flight worker counts, and whether a side effect committed after the caller left.

Before relying on a timeout in production, check the following:

  • Identify whether it only limits waiting, requests cancellation, or actually stops work.
  • Propagate one overall deadline through request-scoped operations.
  • Use cancellation-aware network, database, and worker APIs where available.
  • Release locks, connections, and temporary resources on cancellation.
  • Bound concurrency for operations that cannot be safely interrupted.
  • Make side-effecting operations safe to retry after uncertain outcomes.
  • Measure how much work remains active after its caller has timed out.
  • Test races between cancellation, completion, and irreversible side effects.

Conclusion

Our experiment started with a small discrepancy: the caller timed out after 100 milliseconds, while its operation completed approximately 250 milliseconds later.

JavaScript and Python made it easy to reproduce. Looking at Go and Rust showed different cancellation mechanisms, but a similar limitation whenever work crossed a boundary the runtime could not control.

The important measurement is not just how quickly a timeout returns. It is how much work remains after the timeout, whether that work can still change the system's state, and what happens if the caller tries again.

A reliable implementation needs to know which operations can be interrupted, which must finish, and which may have already committed an effect. Deadlines, cancellation, bounded work, cleanup, and idempotent retries address different parts of that problem.

A timeout tells the caller when to stop waiting. It does not, by itself, tell the rest of the system when to stop.

References