Files
gitea/services/actions/cancel.go
T
BenVandZettat123 c344004e53 fix(actions): settle cancelled reusable workflow runs stuck in cancelling (#39706)
Fixes #39702

**Root Cause:**

Cancelling a reusable workflow caller wrote it `cancelled` right away,
even while a child was still `cancelling`. When the runner acknowledged,
the caller did not change, so the run was never refreshed and stayed
`cancelling`.

SQLite hid this because the child is cancelled a second time, which
re-aggregates the caller, while MySQL reports no affected rows for that
unchanged update.

**Changes:**

- A caller now takes its status only from its children: it stays
`cancelling` until its last child finishes, and its final status then
refreshes the run.
- Cancelling a run whose jobs are all done only settles the run, which
did not wake the runs waiting for its concurrency group. It now does, so
force-cancelling an already stuck run also frees them.

---------

Signed-off-by: BenV <165034+benv666@users.noreply.github.com>
Co-authored-by: Zettat123 <zettat123@gmail.com>
2026-10-10 09:46:57 +02:00

89 lines
3.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"context"
"fmt"
"gitea.dev/actionslib/pkg/expreval"
actions_model "gitea.dev/models/actions"
"gitea.dev/models/db"
"gitea.dev/modules/actions/jobparser"
"gitea.dev/modules/log"
)
// CancelRun cancels a run's cancellable jobs and returns the run's post-cancellation state.
// A runner that supports it gets to run its post-cancel cleanup before the job reaches its final status.
func CancelRun(ctx context.Context, run *actions_model.ActionRun, jobs []*actions_model.ActionRunJob) (*actions_model.ActionRun, error) {
return cancelRun(ctx, run, jobs, false)
}
// ForceCancelRun cancels a run like CancelRun, but does not wait for the runners to acknowledge it:
// the jobs are marked cancelled at once and whatever a runner reports for them afterwards is discarded.
func ForceCancelRun(ctx context.Context, run *actions_model.ActionRun, jobs []*actions_model.ActionRunJob) (*actions_model.ActionRun, error) {
return cancelRun(ctx, run, jobs, true)
}
func cancelRun(ctx context.Context, run *actions_model.ActionRun, jobs []*actions_model.ActionRunJob, force bool) (*actions_model.ActionRun, error) {
var updatedJobs []*actions_model.ActionRunJob
if err := db.WithTx(ctx, func(ctx context.Context) (err error) {
updatedJobs, err = actions_model.CancelJobs(ctx, cancellableJobs(run, jobs, force), force)
if err != nil {
return fmt.Errorf("CancelJobs: %w", err)
}
if len(updatedJobs) > 0 {
return nil // a job update already refreshed the run
}
return actions_model.SettleRunAfterCancel(ctx, run)
}); err != nil {
return nil, err
}
// updatedJobs, not jobs: cancelOneJob re-reads the cancelled rows, the input ones still carry their pre-cancel status
CreateCommitStatusForRunJobs(ctx, run, updatedJobs...)
EmitJobsIfReadyByJobs(updatedJobs)
NotifyWorkflowJobsStatusUpdate(ctx, updatedJobs...)
reloaded, err := actions_model.GetRunByRepoAndID(ctx, run.RepoID, run.ID)
if err != nil {
return nil, fmt.Errorf("GetRunByRepoAndID: %w", err)
}
if len(updatedJobs) > 0 || reloaded.Status != run.Status {
NotifyWorkflowRunStatusUpdate(ctx, reloaded)
}
if len(updatedJobs) == 0 && reloaded.Status != run.Status {
// the run's status was updated, so emit it for the emitter to check
if err := EmitJobsIfReadyByRun(run.ID); err != nil {
log.Error("Check jobs of run %d: %v", run.ID, err)
}
}
return reloaded, nil
}
func cancellableJobs(run *actions_model.ActionRun, jobs []*actions_model.ActionRunJob, force bool) []*actions_model.ActionRunJob {
if force || (run.Started.IsZero() && !run.Status.In(actions_model.StatusRunning, actions_model.StatusCancelling)) {
return jobs
}
toCancel := make([]*actions_model.ActionRunJob, 0, len(jobs))
for _, job := range jobs {
if !runsAfterCancellation(job) {
toCancel = append(toCancel, job)
}
}
return toCancel
}
func runsAfterCancellation(job *actions_model.ActionRunJob) bool {
if job.Status.IsDone() {
return false
}
parsed, err := job.ParseJob()
if err != nil || parsed.If.Value == "" {
return false
}
condition := jobparser.IfExpression(parsed.If.Value)
return expreval.CallsFunction(condition, "always") && !expreval.CallsFunction(condition, "cancelled")
}