Skip to content
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

Fix race condition reading existing import-map.json files #3453

Merged
merged 2 commits into from Jun 16, 2021
Merged

Conversation

matthewp
Copy link
Contributor

@matthewp matthewp commented Jun 16, 2021

When trying to read the import-map.json, if that file is being concurrently written using fs.stat won't tell you that. Using fs.open gives you a handle with r+ (read and write) permission, which throws if it's open somewhere else for writing. If not, you get a handle and can read the data.

Fixes withastro/astro#468

Changes

Changes the logic in reading import-map.json to use a filehandle.

Testing

Did not, this would be hard to recreate because it's a race condition.

Docs

N/A

When trying to read the import-map.json, if that file is being concurrently using `fs.stat` won't tell you that. Using `fs.open` gives you a handle with `r+` (read and write) permission, which throws if it's open somewhere else for writing. If not, you get a handle and can read the data.

Fixes withastro/astro#468
@matthewp matthewp requested a review from a team as a code owner June 16, 2021 20:40
@vercel
Copy link

vercel bot commented Jun 16, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/9BAGGsUu5aSpHAA4erUapgL34Ne5
✅ Preview: https://snowpack-git-race-pikapkg.vercel.app

@jasikpark
Copy link

Would it be worth looking at https://github.com/dubzzz/fast-check/blob/main/documentation/Tips.md#detect-race-conditions for being able to codify this as a test nonetheless?

const existingImportMap: ImportMap | null =
(await fs.stat(existingImportMapLoc).catch(() => null)) &&
JSON.parse(await fs.readFile(existingImportMapLoc, 'utf8'));
const importMapHandle = await fs.open(existingImportMapLoc, 'r+').catch(() => null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the PR notes said, it’s hard for me to test/confirm this but overall this seems like a pretty safe change to me, so I’m +1

@matthewp
Copy link
Contributor Author

Thanks for the suggestion @jasikpark . That seems to be its own testing framework so not something we can use here. But brings up the fact that it might be possible to test race conditions, so I'll look into libraries and see what I can find.

@matthewp matthewp merged commit d93d01e into main Jun 16, 2021
@matthewp matthewp deleted the race branch June 16, 2021 20:55
@jasikpark
Copy link

jasikpark commented Jun 16, 2021

That seems to be its own testing framework so not something we can use here.

It looks like it has support for Jest and Mocha integration, ~~so maybe it can support uvu? 🤔 ~~

Oh wait I just realized this is snowpack not astro - so then it should be usable since fast-check works out of the box with Jest!

https://github.com/dubzzz/fast-check/blob/main/documentation/RaceConditions.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Build fails unless astro dev is run first
3 participants