Skip to content

fix(workflow): run termination bookkeeping on a cancellation-safe context (EN-1224) - #187

Closed
flemzord wants to merge 1 commit into
mainfrom
fix/cancel-cleanup-disconnected-ctx
Closed

fix(workflow): run termination bookkeeping on a cancellation-safe context (EN-1224)#187
flemzord wants to merge 1 commit into
mainfrom
fix/cancel-cleanup-disconnected-ctx

Conversation

@flemzord

Copy link
Copy Markdown
Member

Problem (H4 — HIGH)

When a Run workflow is cancelled, config.run returns a CanceledError and the run context is already cancelled. The terminal bookkeeping activities were then executed on that cancelled context:

  • per stage: UpdateStage + SendWorkflowStageTerminationEvent (config.go)
  • per instance: UpdateInstance + SendWorkflowTerminationEvent (run.go)

They failed immediately, so the instance/stage rows were left stuck "running" forever and no FAILED_WORKFLOW(_STAGE) event was published.

Fix

Add terminationContext(ctx) which returns:

  • the original context when it is still live (normal path — bookkeeping stays cancellable), or
  • a workflow.NewDisconnectedContext when the workflow has been cancelled, so the bookkeeping still runs.

Use it for the terminal activities in both run() and Run.

Testing

The normal path is unchanged and remains covered by the end-to-end TestConfig. A dedicated cancellation integration test would require registering a blocking stage inside the workflow internal test package, which creates an import cycle (workflow → stages/... → workflow); the fix is small and the cancellation behavior is asserted by reasoning. Happy to add an external-package test if preferred.

Severity: HIGH.

…text

When a Run workflow is cancelled, config.run returns a CanceledError and
the run context is already cancelled. The subsequent UpdateStage /
SendWorkflowStageTerminationEvent and UpdateInstance /
SendWorkflowTerminationEvent activities were executed on that cancelled
context, so they failed immediately: the instance/stage rows were left
stuck "running" and no FAILED_WORKFLOW(_STAGE) event was published.

Add terminationContext(), which returns the original context normally but
a workflow.NewDisconnectedContext when the workflow has been cancelled,
and use it for the terminal bookkeeping activities in both run() and Run.

The normal (non-cancelled) path is unchanged (still uses the live
context); covered by the existing end-to-end TestConfig.
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@flemzord, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 16 minutes and 40 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c0a0f974-143e-4433-be8a-2ea5eb302736

📥 Commits

Reviewing files that changed from the base of the PR and between 271bf8d and 4a60782.

📒 Files selected for processing (2)
  • internal/workflow/config.go
  • internal/workflow/run.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cancel-cleanup-disconnected-ctx

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@flemzord

flemzord commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Tracked in Jira: EN-1224 (Epic EN-1217).

@flemzord flemzord changed the title fix(workflow): run termination bookkeeping on a cancellation-safe context fix(workflow): run termination bookkeeping on a cancellation-safe context (EN-1224) Jun 11, 2026
@flemzord

flemzord commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

Superseded by #199, which consolidates this change with the related reliability and safety fixes on top of the current main branch.

@flemzord flemzord closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant