Jenkins / Persistent state and controlled delivery / Lesson 21
Choose timeout scope around agent allocation
Compare top-level and stage-level Declarative timeouts and verify what the budget includes before applying it to real work.
On this page
Lesson goals
- Explain when each timeout starts relative to agent allocation.
- Separate queue or provisioning time from execution time.
- Test timeout and recovery policy in an isolated job.
Define the time budget
A timeout should express a deliberate operational budget, not simply hide slow or queued work. In Declarative Pipeline, timeout placement affects what time is counted. A top-level agent and a stage-level agent have different ordering relative to their options. The examples use synthetic echo steps and require a compatible Jenkins installation; the linux label in the second example must exist in the test environment.
Inspect the top-level timeout
The first Jenkinsfile declares a top-level agent and a top-level timeout option. Jenkins allocates that agent before applying the top-level timeout, so the wait to allocate it is not included in that timeout. The option then limits the Pipeline under the documented behavior. This is not a guarantee that the whole request including queue time finishes within ten minutes; use additional operational monitoring for allocation delays.
pipeline {
agent any
options { timeout(time: 10, unit: "MINUTES") }
stages {
stage("Check") {
steps { echo "Synthetic work" }
}
}
}
The top-level agent is allocated before the top-level timeout is applied.
Inspect the stage timeout
The second independent Jenkinsfile uses agent none at the top level and a stage-level agent and timeout. Stage options are invoked before the stage agent is allocated, so this timeout includes its allocation time. A delayed or unavailable agent can therefore consume the budget before the step runs. Stage options also precede evaluation of stage when conditions under the documented ordering; design conditional and timeout behavior with that order in mind.
pipeline {
agent none
stages {
stage("Check") {
agent { label "linux" }
options { timeout(time: 10, unit: "MINUTES") }
steps { echo "Synthetic work" }
}
}
}
The stage timeout begins before stage-agent allocation, so allocation delay is included.
Plan interruption and recovery
A timeout interrupts the enclosed work, but it does not prove a remote write was undone or every external process stopped. Define which work is safe to retry, how owned resources are cleaned up, and how the run reports an incomplete result. Avoid a broad error handler that silently converts a timeout into success. For multi-step effects, use the target systemโs status and idempotency contracts before retrying.
Verify with controlled contention
In an owned test controller, compare the examples with an available agent and with a deliberately delayed allocation. Record queue time, allocation time, stage start, final result, and cleanup. Inject a long controlled step separately to test execution timeout, then verify that no real deployment or notification is triggered. Confirm behavior for the installed Jenkins and plugin versions and retain the result as environment-specific evidence.
Common errors and fixes
A stage times out before its first step
- Why it happens
- Agent allocation consumed a stage-level timeout budget.
- How to fix it
- Inspect queue and provisioning time and choose a budget appropriate to the scope.
- Prevent it next time
- Test delayed allocation separately from slow execution.
A top-level timeout is assumed to cover queue time
- Why it happens
- The agent is allocated before that timeout starts.
- How to fix it
- Monitor allocation separately or choose a different scoped policy.
- Prevent it next time
- Record allocation and execution timestamps in a controlled test.
A timeout triggers a duplicate write on retry
- Why it happens
- Interruption was mistaken for proof that an external effect did not happen.
- How to fix it
- Check target status and use a supported idempotency or recovery contract.
- Prevent it next time
- Test an ambiguous outcome without a real external mutation.
2 practice exercises
1. Which example includes stage-agent allocation in its timeout?
Show worked solution
The second example: its stage option is applied before the stage-level agent is allocated. A delayed agent can consume the stage budget before the echo step begins.
2. Does a timeout prove a remote operation was reversed?
Show worked solution
No. It interrupts Pipeline work but external effects can already exist or continue. Check the target and follow its recovery and retry contract before repeating the operation.
Key takeaways
- Timeout placement determines whether agent allocation consumes the budget.
- Treat interruption, external effects, and retry safety as separate concerns.