-
Notifications
You must be signed in to change notification settings - Fork 202
Fix state.yml race condition with PID-based file locking #1783
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
Merged
+173
−8
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd80d2d
Replace mkdir-based locking with PID-based locking for state.yml
firecow 888ade7
Extract withFileLock to pid-file-lock.ts and simplify
firecow 0c9f3e7
Fix two bugs in stale lock detection
firecow 7328df2
Fix SonarCloud issues: use node: protocol, Number methods, and remove…
firecow 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,5 @@ | ||
| --- | ||
| test-job: | ||
| image: busybox | ||
| script: | ||
| - echo "CI_PIPELINE_IID=$CI_PIPELINE_IID" |
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,67 @@ | ||
| import {WriteStreamsMock} from "../../../src/write-streams.js"; | ||
| import {handler} from "../../../src/handler.js"; | ||
| import {initSpawnSpy} from "../../mocks/utils.mock.js"; | ||
| import {WhenStatics} from "../../mocks/when-statics.js"; | ||
| import * as state from "../../../src/state.js"; | ||
| import fs from "fs-extra"; | ||
| import path from "path"; | ||
|
|
||
| const cwd = "tests/test-cases/pipeline-iid"; | ||
| const stateDir = ".gitlab-ci-local"; | ||
| const stateFile = path.join(cwd, stateDir, "state.yml"); | ||
| const lockFile = path.join(cwd, stateDir, "state.lock"); | ||
|
|
||
| beforeAll(() => { | ||
| initSpawnSpy(WhenStatics.all); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.removeSync(stateFile); | ||
| fs.removeSync(lockFile); | ||
| }); | ||
|
|
||
| describe("pipeline-iid state locking", () => { | ||
| test("sequential increments return 0, 1, 2", async () => { | ||
| const first = await state.incrementPipelineIid(cwd, stateDir); | ||
| const second = await state.incrementPipelineIid(cwd, stateDir); | ||
| const third = await state.incrementPipelineIid(cwd, stateDir); | ||
|
|
||
| expect(first).toBe(0); | ||
| expect(second).toBe(1); | ||
| expect(third).toBe(2); | ||
| }); | ||
|
|
||
| test("concurrent increments produce unique IIDs", async () => { | ||
| const count = 20; | ||
| const promises = Array.from({length: count}, () => | ||
| state.incrementPipelineIid(cwd, stateDir), | ||
| ); | ||
| const results = await Promise.all(promises); | ||
|
|
||
| const sorted = [...results].sort((a, b) => a - b); | ||
| expect(sorted).toEqual(Array.from({length: count}, (_, i) => i)); | ||
| }); | ||
|
|
||
| test("stale lock from dead PID is auto-cleaned", async () => { | ||
| fs.ensureDirSync(path.dirname(lockFile)); | ||
| fs.writeFileSync(lockFile, "999999999"); | ||
|
|
||
| const result = await state.incrementPipelineIid(cwd, stateDir); | ||
| expect(result).toBe(0); | ||
| expect(fs.existsSync(lockFile)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| test("pipeline-iid handler increments IID across runs", async () => { | ||
| const writeStreams = new WriteStreamsMock(); | ||
| await handler({cwd: cwd}, writeStreams); | ||
|
|
||
| const iid = await state.getPipelineIid(cwd, stateDir); | ||
| expect(iid).toBe(0); | ||
|
|
||
| const writeStreams2 = new WriteStreamsMock(); | ||
| await handler({cwd: cwd}, writeStreams2); | ||
|
|
||
| const iid2 = await state.getPipelineIid(cwd, stateDir); | ||
| expect(iid2).toBe(1); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.