Skip to content

[Bug] [Zeta] Cancelling a job during pipeline retry backoff blocks for the full retry interval and still allocates slots / redeploys a zombie retry attempt #13488

In review

Description

@DanielLeens

Search before asking

  • I had searched in the issues and found no similar issues.

What happened

In the SeaTunnel Zeta engine (dev at 39c74ce48b0), when a failing pipeline enters its retry backoff sleep (job.retry.times > 0, job.retry.interval.seconds > 0) and a user calls cancelJob(jobId) during that backoff window:

  1. cancelJob(jobId) blocks for the entire remaining job.retry.interval.seconds duration (e.g., 6s in reproduction, or minutes in production deployments with longer retry intervals), holding both the SubPlan monitor lock and the caller/coordinator cancellation thread.
  2. After the backoff sleep ends, the engine ignores that the job was already canceled (jobMaster.isNeedRestore() == false, JobStatus == CANCELING), allocates a brand-new set of slots via jobMaster.preApplyResources(this), and deploys & starts a zombie retry attempt on workers before the blocked cancelPipeline() can finally acquire the lock and tear it down.

In production, this causes:

  • Unresponsive cancellation requests and thread-pool starvation when job.retry.interval.seconds is large.
  • Unnecessary slot allocation and contention on clusters with tight fixed-slot capacity.
  • Real side effects on external sources/sinks (connections opened, records read/written) from a zombie retry attempt launched after the user explicitly canceled the job.

Root Cause Analysis

  1. SubPlan.stateProcess() holds the SubPlan intrinsic monitor lock across Thread.sleep(...):

    • SubPlan.stateProcess() is declared private synchronized void stateProcess() (seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java:798).
    • On case FAILED: / case CANCELED: (SubPlan.java:878-885), it directly invokes prepareRestorePipeline() (SubPlan.java:609-639), which resets currPipelineStatus to CREATED and then executes Thread.sleep(pipelineRestoreIntervalSeconds * 1000) (SubPlan.java:628) while still holding both synchronized (this) and synchronized (restoreLock).
    • When cancelJob(jobId) arrives, PhysicalPlan.cancelJob() calls jobMaster.neverNeedRestore() and SubPlan.cancelPipeline(). Because cancelPipeline() is public synchronized void cancelPipeline() (SubPlan.java:492), it immediately blocks on SubPlan's monitor lock for the entire remaining sleep duration and cannot interrupt or abort the pending retry.
  2. No re-check of jobMaster.isNeedRestore() or cancellation state after waking up:

    • Once Thread.sleep(...) finishes, prepareRestorePipeline() unconditionally returns true (SubPlan.java:629).
    • Still inside the same synchronized stateProcess() invocation, lines 881-883 unconditionally run:
      jobMaster.releasePipelineResource(this);
      jobMaster.preApplyResources(this);
      restorePipeline();
    • restorePipeline() calls startSubPlanStateProcess() (re-entering synchronized stateProcess()), driving the pipeline through CREATED -> SCHEDULED -> DEPLOYING -> RUNNING, acquiring fresh slots from workers, and deploying all task groups for an already-canceled job.
    • Only after stateProcess() returns and releases the SubPlan monitor lock does the waiting cancelPipeline() finally run and cancel the freshly deployed tasks.

Expected Behavior

When cancelJob(jobId) is called while a pipeline is waiting in its retry backoff interval:

  1. The pending retry backoff should be aborted promptly (without waiting for the full job.retry.interval.seconds sleep).
  2. The pipeline should transition cleanly to CANCELED without calling preApplyResources or deploying any further retry attempt (extraSlotsAllocatedAfterCancel == 0).

How to Reproduce

  1. Start a single-node embedded Zeta cluster with fixed slots (dynamic-slot: false, slot-num: 10).
  2. Submit a failing batch job with job.retry.times = 3 and job.retry.interval.seconds = 6 (for example, a FakeSource -> failing SQL cast select cast(name as int) as name, id from fake -> Console job).
  3. Wait until attempt 1 fails and pipeline 1 enters prepareRestorePipeline() backoff sleep (subPlan.getPipelineRestoreNum() == 1 && subPlan.getCurrPipelineStatus() == PipelineStatus.CREATED).
  4. Record slotReqBefore = server.getSlotService().getSlotMetrics().getSlotRequestSuccessTotal() (equals 2 for 1 coordinator + 1 task group).
  5. Call client.cancelJob(jobId) and measure elapsed time until cancelJob(jobId) returns and the job terminates.
  6. Observe the actual runtime output:
    • cancelJob(jobId) blocks for ~6100-6200 ms (the entire 6-second retry backoff interval).
    • slotRequestSuccessTotal increases from 2 to 4 (extraSlotsAllocatedAfterCancel == 2), proving that after cancelJob was invoked and needRestore became false, the waking retry path still allocated fresh slots and deployed a zombie attempt fixed ConfigBuilder.scala #2 before cancellation took effect.

Suggested Fix Direction

  1. Do not hold the SubPlan monitor lock across the retry backoff sleep:
    • Replace the in-lock Thread.sleep(pipelineRestoreIntervalSeconds * 1000) with a cancellable scheduled delay (e.g., ScheduledFuture<?> or interruptible wait outside synchronized (this)) that cancelPipeline() / forceStopPipeline() immediately cancels/interrupts.
  2. Re-check jobMaster.isNeedRestore() and cancellation state before allocating resources or redeploying:
    • After the backoff completes (and before jobMaster.preApplyResources(this) and restorePipeline()), re-verify jobMaster.isNeedRestore() and that the job/pipeline is not being canceled/stopped. If canceled, complete subPlanDone(PipelineStatus.CANCELED) and pipelineFuture directly with zero extra slot requests.
  3. Add regression tests:
    • Add a unit/E2E test canceling a failing job during job.retry.interval.seconds (e.g., 6s) and asserting both prompt cancellation (<< retry interval) and slotRequestSuccessTotal remaining unchanged after cancelJob (extraSlotsAllocatedAfterCancel == 0).

Call for Contributors

This is a clean, well-reproducible Zeta engine state-machine bug with high practical impact for production cancellation responsiveness and slot hygiene. Community contributors are very welcome to claim and fix this issue! Feel free to leave a comment below if you'd like to take it on or discuss the cancellation-safe backoff design.

SeaTunnel Version

dev (39c74ce48b0)

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. CryoThrust commented on Nov 27, 2026

    @CryoThrust
    Contributor

    There are two related boundaries here, and I would coordinate with #12510 before implementing this issue.

    On current dev 39c74ce48, stateProcess() holds the SubPlan monitor while prepareRestorePipeline() sleeps inside restoreLock. cancelPipeline() needs the same SubPlan monitor. That supports the reported cancellation-latency problem independently of whether a retry is eventually allowed to allocate resources. I have checked the source path, not rerun the embedded reproduction yet.

    #12510 already adds isNeedRestore() checks before and after preApplyResources, including release of reservations completed after cancellation. I would not duplicate that resource-allocation fix. Its current patch does not change the in-monitor retry sleep, so promptly waking a pending backoff remains a separate piece.

    I would be interested in taking that backoff-only piece, coordinated with @hutiefang76's existing work. The acceptance boundary would be: cancellation during a nonzero retry interval returns promptly, no delayed continuation resets or deploys a cancelled pipeline, the normal retry delay/budget is unchanged, and existing failure/resource cleanup behavior stays intact. Tests should cover both cancellation before the wait is installed and cancellation while waiting, plus normal retry and force-stop controls; the embedded case should also keep the slot counter unchanged.

    Would a separate, coordinated cancellation-wakeup change be useful here? I would keep the allocation checks owned by #12510 rather than open a competing implementation. No source change or new runtime result is claimed by this comment.

  2. hutiefang76 commented on Nov 27, 2026

    @hutiefang76
    Collaborator

    @CryoThrust That split works for #12510. Its current head, 32cfc2e84145a2c46d976c1c3553f2bdd1aececc, checks cancellation before and after resource allocation and releases reservations completed after cancellation. It does not replace the retry sleep inside the SubPlan monitor.

    A separate backoff-only change would address the remaining cancellation latency without duplicating those allocation checks. Please keep the normal retry interval/budget and existing cleanup behavior intact, and include the before-wait, during-wait, normal-retry and force-stop controls you outlined. The final scope and acceptance still need maintainer confirmation. I will keep #12510 focused on its existing resource-allocation scope.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions