-
Notifications
You must be signed in to change notification settings - Fork 18
fix: Avoid swap-file races when multiple plugin instances share a tadaOutputLocation #420
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
macrozone
wants to merge
2
commits into
0no-co:main
Choose a base branch
from
macrozone:fix/swap-write-race
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.
+84
−21
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@0no-co/graphqlsp': patch | ||
| --- | ||
|
|
||
| Fix intermittent `ENOENT: no such file or directory, unlink '<output>.d.ts.tmp'` failures when multiple plugin instances write the same `tadaOutputLocation`, e.g. several TS projects in a monorepo extending a shared tsconfig. The typings swap-file now has a unique name per process and write, so concurrent regenerations no longer clobber each other's swap-files, and swap-file cleanup no longer masks the original write error |
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,56 @@ | ||
| import { describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
| import fs from 'node:fs/promises'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
|
|
||
| import { swapWrite } from '../../packages/graphqlsp/src/graphql/getSchema'; | ||
|
|
||
| let dir: string; | ||
|
|
||
| beforeEach(async () => { | ||
| dir = await fs.mkdtemp(path.join(os.tmpdir(), 'graphqlsp-swap-write-')); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe('swapWrite', () => { | ||
| it('writes a new file directly', async () => { | ||
| const target = path.join(dir, 'introspection.d.ts'); | ||
| await swapWrite(target, 'contents'); | ||
| expect(await fs.readFile(target, 'utf8')).toBe('contents'); | ||
| }); | ||
|
|
||
| it('replaces an existing file', async () => { | ||
| const target = path.join(dir, 'introspection.d.ts'); | ||
| await fs.writeFile(target, 'old'); | ||
| await swapWrite(target, 'new'); | ||
| expect(await fs.readFile(target, 'utf8')).toBe('new'); | ||
| }); | ||
|
|
||
| // Multiple plugin instances may share one `tadaOutputLocation`, e.g. | ||
| // several TS projects in a monorepo extending the same tsconfig. See: | ||
| // https://github.com/0no-co/gql.tada/issues/571 | ||
| it.each([ | ||
| { name: 'an existing target', createTarget: true }, | ||
| { name: 'a missing target', createTarget: false }, | ||
| ])('tolerates concurrent writes to $name', async ({ createTarget }) => { | ||
| const target = path.join(dir, 'introspection.d.ts'); | ||
| if (createTarget) await fs.writeFile(target, 'initial'); | ||
|
|
||
| const writers = Array.from({ length: 20 }, (_, index) => | ||
| swapWrite(target, `contents-${index}`) | ||
| ); | ||
| await expect(Promise.all(writers)).resolves.not.toThrow(); | ||
|
|
||
| // The last rename wins, but the target must match one write in full | ||
| // and no swap-files may be left behind | ||
| const contents = await fs.readFile(target, 'utf8'); | ||
| expect(contents).toMatch(/^contents-\d+$/); | ||
| const leftovers = (await fs.readdir(dir)).filter(file => | ||
| file.endsWith('.tmp') | ||
| ); | ||
| expect(leftovers).toEqual([]); | ||
| }); | ||
| }); |
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.
This still races on the first write: if the output doesn't exist yet, concurrent instances all take this direct
writeFilepath. Can we use the unique swap+rename path for missing targets too?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.
Good catch — done in 1358f0a.
swapWritenow always writes to the unique swap-file and renames into place, for missing targets too; the direct-write branch is gone. Directory/permission errors still fall through since the swap-file lives in the same directory. Extended the unit test to run the 20-way concurrent write against both an existing and a missing target.Uh oh!
There was an error while loading. Please reload this page.
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.
(above was written by fable, just to clarify)
for me it looks fine, but i am testing it currently in my large repo to see whether the issue is resolved