Document Job Timed Out? Don't Let One Task Stall the Batch

A scanned PDF got stuck, and the subsequent ninety-nine tasks remained idle. Adding a timeout to the request seemed to resolve the issue, but in reality, the pr

Batch jobs are easy to stall. One scanned PDF gets stuck, and the ninety-nine tasks behind it sit idle. Adding a timeout to the request can look like the fix, but a timeout is not the same as a stopped job. This guide explains what a timeout actually does, what evidence you need after one fires, and when retrying makes sense.

It is written for developers and operators who run document processing in batches, especially when some tasks depend on external processes or remote services. You will get a clear set of rules for handling timeouts, the records to keep, and the questions to ask before retrying.

This guide uses synthetic samples to demonstrate the processing method. Before processing important files, keep the original copies and check your results against the boundaries and acceptance criteria described here.

How do you keep one stalled task from holding up a batch? The short answer

Treat a timeout as three separate actions: stop waiting, stop the work, and mark the output as undeliverable. A plain timeout does not guarantee the second one.

Your situationWhat to doWhy
Your batch calls a subprocess with a timeoutDon't assume the work has stoppedsubprocess.run(timeout=...) covers direct child processes only
You set a task deadlineTreat the timeout as reached when time equals the deadline, and document it in your testsA consistent boundary keeps results predictable
A task times outRecord the phase, termination confirmation, temporary output identity, and retry countThese records show what is still left after the timeout
The same input fails at the same step every timeIsolate the sample and investigate its complexity instead of retryingRetrying helps only when the failure is recoverable and the call is idempotent
A remote translation task seems stuckQuery known tasks before you submit againStopping your wait does not mean the remote task is gone

A timeout is not the same as termination

Setting a timeout stops your code from waiting, but it does not guarantee the work has ended. The process might keep using memory in the background.

Why a timeout can leave work running

  • subprocess.run(timeout=...) handles direct child processes.
  • It cannot be generalised to "any process tree has been cleaned up."
  • The outcome depends on the operating system, the way the process was started, and the behaviour of grandchild processes.
  • Production systems need isolation mechanisms such as process groups, containers, or job objects.
  • For the exact timeout behaviour, see the Python subprocess timeout behavior reference.

Define the task status contract first

Before you write timeout code, agree on the states a task can be in. The example below only simulates state decisions. It does not start external commands or pause processes. Its main point is this: once the deadline has passed, a task that submits its result late cannot be marked as successful.

from dataclasses import dataclass

@dataclass(frozen=True)
class Decision:
    state: str
    deliver: bool
    terminate_requested: bool

def decide(now, deadline, worker_done, artifact_valid):
    if now >= deadline:
        return Decision("timed_out", False, not worker_done)
    if not worker_done:
        return Decision("processing", False, False)
    if not artifact_valid:
        return Decision("invalid_artifact", False, False)
    return Decision("verified", True, False)

assert decide(9, 10, False, False).state == "processing"
assert decide(10, 10, False, False).terminate_requested
assert not decide(11, 10, True, True).deliver
assert decide(9, 10, True, True).deliver
assert decide(9, 10, True, False).state == "invalid_artifact"

Rules for the deadline

  • A timeout occurs when the time equals the deadline.
  • You may choose a different boundary, but document it in the tests.
  • Measure elapsed time with a monotonic clock suited to the computation interval.
  • Time zone changes or wall-clock adjustments must not change the task budget.

What evidence should you keep after a timeout?

A timeout should leave a record, not just a missing result. Keep these four items:

What you need to knowPurposeNote
Timeout phaseShows whether the task timed out while downloading, parsing, or translatingDon't record just one generic timeout
Termination confirmationPrevents leftover background workA request to terminate does not mean the process has exited
Temporary output identityStops half-finished output from being handed over by mistakeKeep it separate from the final result
Retry countControls the total time budgetEach retry should not get unlimited time

Retrying is not the automatic next step

Retrying can make a stalled batch worse. Before you retry, check the cause.

When to retry, and when not to

  • If the same input keeps using up resources at the same step, isolate that sample and investigate its complexity.
  • Retry only when the cause is recoverable and the call is idempotent, meaning running it again does not create duplicate effects.
  • Show users the difference between "service busy, retrying" and "input unsupported, needs handling." They need different responses.

Handling a remote translation task

When you use OmniDoc (Product Entry) for translation, show three things separately: how long the client has been waiting, the platform's processing status, and the file download status.

Before you click again

  1. Stopping the page's wait does not mean the remote task has disappeared.
  2. Query any task you already know about first.
  3. Only then submit again, so you avoid wasting resources or creating several results that are hard to match up.

🎯 Which approach fits you?

If your batch runs external processes

Set timeouts, but also plan for process groups, containers, or job objects, because a timeout alone may not stop the work.

If a remote task may still be running

Query the known task before you resubmit, and show waiting, processing, and download status as separate items.

If the same input fails at the same step

Isolate that sample and investigate it rather than retrying it automatically.

❓ FAQ

1. Does subprocess.run(timeout=...) stop every process my task started?

No. It handles direct child processes, and the result depends on the operating system, how the process was started, and any grandchild processes. Production systems need process groups, containers, or job objects.

2. When exactly does a task time out?

In this protocol, a timeout occurs when the time equals the deadline. You can choose another boundary, but document it in your tests.

3. Should I retry a task after it times out?

Only if the failure is recoverable and the call is idempotent. If the same input keeps failing at the same step, isolate it instead.

4. What should I record after a timeout?

Record the timeout phase, whether termination was confirmed, the identity of any temporary output, and the retry count.

📝 Summary

A timeout stops your wait, but it does not guarantee that the work has stopped, so track termination and output state separately. Define your deadline rules, keep the evidence, and retry only when the cause is recoverable. Check known remote tasks before you submit again.

Download OmniDoc for free: documents, spreadsheets, slides and PDFs in one app

Updated 2026-11-29