Skip to content

docs: Add executor starvation avoidance guide and tests#1029

Open
mPokornyETM wants to merge 2 commits intomasterfrom
docs/697-executor-starvation-avoidance
Open

docs: Add executor starvation avoidance guide and tests#1029
mPokornyETM wants to merge 2 commits intomasterfrom
docs/697-executor-starvation-avoidance

Conversation

@mPokornyETM
Copy link
Copy Markdown
Contributor

Summary

Documents the recommended pattern for using lockable resources in declarative pipelines without causing executor starvation, as discussed in #697.

Problem

When a pipeline uses agent any (or a specific label) at the top level together with options { lock(...) }, every build allocates an executor before acquiring the lock. Builds waiting for the lock hold their executors hostage, blocking other jobs.

Solution documented

Use agent none at the pipeline level and move agent into individual stages alongside options { lock(...) }:

pipeline {
  agent none
  stages {
    stage('Test') {
      options {
        lock('end-to-end-test-resource')
      }
      agent { label 'some-label' }
      steps {
        echo 'Running tests...'
      }
    }
  }
}

Changes

  1. New example docavoiding-executor-starvation.md covering:

    • The anti-pattern (executor starvation)
    • The solution (agent none + stage-level agent)
    • Preparation stages without locks
    • Multiple stages under one lock
    • Scripted pipeline equivalent
  2. Updated examples index — added link to the new guide in readme.md

  3. New test classDeclarativePipelineExecutorStarvationTest with four scenarios:

Test Description
agentNoneWithStageLevelLockDoesNotConsumeExecutorWhileWaiting Resource-name lock — waiter uses only flyweight executor, unrelated job runs freely
agentNoneWithStageLevelLabelLockDoesNotConsumeExecutor Label-based lock — same behavior
preparationStageRunsWithoutLock Build stage runs without lock, deploy stage acquires it
nestedStagesShareLockWithoutExecutorStarvation Parent stage lock wraps nested stages, no executor starvation

Closes #697

Document the recommended pattern for using lockable resources in
declarative pipelines without causing executor starvation: use
agent none at the pipeline level and move agent directives into
individual stages alongside options { lock(...) }.

Add avoiding-executor-starvation.md with examples of the problem,
the solution, preparation stages, and nested multi-stage locks.

Add DeclarativePipelineExecutorStarvationTest with four scenarios:
- stage-level lock with agent none (resource name)
- stage-level lock with agent none (label)
- preparation stage without lock, deploy stage with lock
- nested stages sharing one lock

Fixes #697
@mPokornyETM mPokornyETM requested a review from a team as a code owner April 27, 2026 06:31
@mPokornyETM mPokornyETM added the merge-in-3-days-without-review Auto-approve countdown label Apr 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

⏸️ Auto-merge countdown PAUSED: CI checks are not passing. The countdown will resume when all checks are green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unavailable Resource Sends Build back to Queue

1 participant