Repository navigation
fix(post-processor): isolate updates to the queued run attempt #1539
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
Open
Naveen Chatlapalli (1aifanatic)
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
1aifanatic:fix/1051-postprocessor-attempt-gate
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.
Open
Changes from all commits
Commits
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,68 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { PostProcessor, type PostProcessorConfig } from "./post-processor.js"; | ||
|
|
||
| const config: PostProcessorConfig = { | ||
| mongoUri: "mongodb://localhost:27017", | ||
| mongoDatabase: "test-db", | ||
| mongoCollection: "requests", | ||
| storageAccountName: "devstoreaccount1", | ||
| storageConnectionString: "UseDevelopmentStorage=true", | ||
| queueName: "post-processor", | ||
| batchSize: 1, | ||
| pollIntervalMs: 1000, | ||
| redisHost: "localhost", | ||
| redisPort: 6379, | ||
| redisPassword: "", | ||
| }; | ||
|
|
||
| describe("PostProcessor attempt isolation", () => { | ||
| async function processWithResults(...results: number[]) { | ||
| const worker = new PostProcessor(config); | ||
| const updateOne = vi.fn(); | ||
| for (const matchedCount of results) updateOne.mockResolvedValueOnce({ matchedCount }); | ||
| (worker as any).collection = { updateOne }; | ||
|
|
||
| const process = vi.fn().mockResolvedValue(undefined); | ||
| worker.registerHandler({ type: "atif", process }); | ||
| const safeDeleteMessage = vi.fn().mockResolvedValue(undefined); | ||
| const triggerReportGeneration = vi.fn().mockResolvedValue(undefined); | ||
| (worker as any).safeDeleteMessage = safeDeleteMessage; | ||
| (worker as any).triggerReportGeneration = triggerReportGeneration; | ||
|
|
||
| const log = vi.fn().mockResolvedValue(undefined); | ||
| const heartbeat = { stop: vi.fn().mockReturnValue("receipt-1") } as any; | ||
| const message = { messageId: "message-1", popReceipt: "receipt-1" } as any; | ||
| const doc = { _id: "request-1", run: { _id: "run-1", status: "done" } }; | ||
|
|
||
| await (worker as any).handleRequest(doc, message, heartbeat, log, { | ||
| type: "atif", requestId: "request-1", runId: "run-1", | ||
| }); | ||
|
|
||
| return { updateOne, process, safeDeleteMessage, triggerReportGeneration, log }; | ||
| } | ||
|
|
||
| it("discards a stale message before running its handler", async () => { | ||
| const result = await processWithResults(0); | ||
|
|
||
| expect(result.updateOne).toHaveBeenCalledWith( | ||
| { _id: "request-1", "run._id": "run-1", "run.status": "done" }, | ||
| { $set: { "run.postProcessorStatus": "processing" } }, | ||
| ); | ||
| expect(result.process).not.toHaveBeenCalled(); | ||
| expect(result.safeDeleteMessage).toHaveBeenCalledWith("message-1", "receipt-1"); | ||
| }); | ||
|
|
||
| it("does not stamp or trigger a report when a retry replaces the run during processing", async () => { | ||
| const result = await processWithResults(1, 0); | ||
|
|
||
| expect(result.process).toHaveBeenCalledTimes(1); | ||
| expect(result.updateOne.mock.calls[1][0]).toEqual({ | ||
| _id: "request-1", "run._id": "run-1", "run.status": "done", | ||
| }); | ||
| expect(result.triggerReportGeneration).not.toHaveBeenCalled(); | ||
| expect(result.safeDeleteMessage).toHaveBeenCalledWith("message-1", "receipt-1"); | ||
| }); | ||
| }); |
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
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.
[P2] Carry the originating attempt through report triggering too. There is still a retry window after
completed.matchedCountsucceeds: this branch awaits logging before callingtriggerReportGeneration, and that request sends only{ requestId }./api/v1/reports/triggerthen fetches the latest run by request ID without checking the originating run ID or completed status.I reproduced a retry replacing run A with pending run B during the awaited "Post-processing complete" log. Both guarded updates for A matched, but A's worker still posted the trigger while B was current. An
alwaystemplate can create a report for that incomplete/new attempt, and HTTP retries can hit the same gap.Please include the message's run ID in the trigger contract and enforce attempt identity/readiness where the API resolves the run and creates the report; a fresh worker-side check alone would leave another TOCTOU window. Add a regression for retry after the successful completion stamp and before the trigger is handled. The database filters here are good; this is the remaining part of the attempt-isolation fix, not a claim that those new filters caused the race.