-
-
Notifications
You must be signed in to change notification settings - Fork 36
[dnm]: test cancellation #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jamieQ
wants to merge
3
commits into
main
Choose a base branch
from
test/cancel-ii
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { ActionCancellation, createActionSignalHandler } from './cancellation.js'; | ||
|
|
||
| describe('Action cancellation signals', () => { | ||
| it('aborts gracefully on the first cancellation signal', () => { | ||
| const cancellation = new ActionCancellation(); | ||
| const exit = vi.fn(); | ||
| const handler = createActionSignalHandler({ cancellation, exit }); | ||
|
|
||
| handler('SIGTERM'); | ||
|
|
||
| expect(cancellation.requested).toBe(true); | ||
| expect(cancellation.signalName).toBe('SIGTERM'); | ||
| expect(cancellation.abortController.signal.aborted).toBe(true); | ||
| expect(cancellation.exitCode).toBe(143); | ||
| expect(exit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ignores duplicate delivery before forcing a later exit', () => { | ||
| const cancellation = new ActionCancellation(); | ||
| const exit = vi.fn(); | ||
| let now = 1_000; | ||
| const handler = createActionSignalHandler({ cancellation, exit, now: () => now }); | ||
|
|
||
| handler('SIGINT'); | ||
| now += 100; | ||
| handler('SIGTERM'); | ||
| expect(exit).not.toHaveBeenCalled(); | ||
|
|
||
| now += 1_000; | ||
| handler('SIGTERM'); | ||
| expect(exit).toHaveBeenCalledWith(143); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| export type ActionCancelSignal = 'SIGINT' | 'SIGTERM'; | ||
|
|
||
| const DEFAULT_DUPLICATE_SIGNAL_WINDOW_MS = 750; | ||
|
|
||
| /** Run-scoped cancellation state shared by the Action entrypoint and workflows. */ | ||
| export class ActionCancellation { | ||
| readonly abortController = new AbortController(); | ||
| signalName: ActionCancelSignal | undefined; | ||
|
|
||
| get requested(): boolean { | ||
| return this.signalName !== undefined; | ||
| } | ||
|
|
||
| request(signalName: ActionCancelSignal): boolean { | ||
| if (this.requested) return false; | ||
|
|
||
| this.signalName = signalName; | ||
| this.abortController.abort(new Error(`Action cancelled by ${signalName}`)); | ||
| return true; | ||
| } | ||
|
|
||
| get exitCode(): number { | ||
| return this.signalName === 'SIGTERM' ? 143 : 130; | ||
| } | ||
| } | ||
|
|
||
| interface ActionSignalHandlerOptions { | ||
| cancellation: ActionCancellation; | ||
| now?: () => number; | ||
| exit?: (code: number) => void; | ||
| duplicateWindowMs?: number; | ||
| } | ||
|
|
||
| /** Create a shared SIGINT/SIGTERM handler with graceful-first, force-second behavior. */ | ||
| export function createActionSignalHandler( | ||
| options: ActionSignalHandlerOptions | ||
| ): (signalName: ActionCancelSignal) => void { | ||
| const duplicateWindowMs = options.duplicateWindowMs ?? DEFAULT_DUPLICATE_SIGNAL_WINDOW_MS; | ||
| const now = options.now ?? (() => Date.now()); | ||
| const exit = options.exit ?? ((code) => process.exit(code)); | ||
| let lastSignalAt = 0; | ||
|
|
||
| return (signalName) => { | ||
| const receivedAt = now(); | ||
| if (options.cancellation.requested && receivedAt - lastSignalAt < duplicateWindowMs) { | ||
| return; | ||
| } | ||
|
|
||
| lastSignalAt = receivedAt; | ||
| if (!options.cancellation.request(signalName)) { | ||
| exit(signalName === 'SIGTERM' ? 143 : 130); | ||
| return; | ||
| } | ||
|
|
||
| console.warn(`Cancellation requested by ${signalName}; finalizing partial results`); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cancellation after Analyze completes makes Verify reject a successful artifact
When concurrency cancellation arrives after Analyze has written its normal successful artifact but before Report runs, the job-level
cancelled()guard executes Verify against that artifact. Because successful analyze output has nooutcome: "cancelled", Verify fails, and the following broad cancellation guard can archive the valid artifact ascancelled-findings; gate these steps on an Analyze cancellation that corresponds to a cancelled artifact rather than job cancellation alone.Evidence
cancel-in-progress: true, so a newersynchronizerun can cancel the prior job between the sequential Analyze and Report steps.runAnalyzeModecompletion callswriteFindingsOutputwithout anoutcome, whilefinalizeCancelledPRRunis the path that writesoutcome: 'cancelled'.cancelled(), reads the Analyze output, and throws when the successful artifact's outcome is missing.cancelled()plus the non-empty findings-file output, so the cancellation state remains active after Verify fails and the successful artifact can be uploaded under a cancelled name.Identified by Warden · code-review · WMS-WUJ