Skip to content

fix(mkdir): mitigate directory creation race condition #1019

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
merged 1 commit into from
Mar 8, 2021

Conversation

rivy
Copy link
Contributor

@rivy rivy commented Feb 14, 2021

Fixes #1018

@rivy rivy force-pushed the fix.mkdir branch 3 times, most recently from 5ae1933 to 51a0034 Compare February 14, 2021 21:20
@rivy
Copy link
Contributor Author

rivy commented Feb 14, 2021

Only previously created directories should be allowed/ignored, so I added an isFile() check to the catch logic.

rivy added a commit to rivy/js.os-paths that referenced this pull request Feb 14, 2021
rivy added a commit to rivy/js.xdg-portable that referenced this pull request Feb 14, 2021
- avoids fairly frequent build failures due to parallel duplicate `shx mkdir ...` targets
- ref: [issue](shelljs/shelljs#1018)
- ref: [PR/fix](shelljs/shelljs#1019)
rivy added a commit to rivy/js.os-paths that referenced this pull request Feb 14, 2021
- avoids fairly frequent build failures due to parallel duplicate `shx mkdir ...` targets
- ref: [issue](shelljs/shelljs#1018)
- ref: [PR/fix](shelljs/shelljs#1019)
@rivy
Copy link
Contributor Author

rivy commented Feb 15, 2021

This doesn't touch 'test', so I think the single AppVeyor failure is unrelated (and likely a random "cosmic-ray" failure).

@codecov-io
Copy link

codecov-io commented Feb 21, 2021

Codecov Report

Merging #1019 (4b97d90) into master (cb64b92) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1019      +/-   ##
==========================================
- Coverage   97.22%   97.22%   -0.01%     
==========================================
  Files          35       35              
  Lines        1332     1331       -1     
==========================================
- Hits         1295     1294       -1     
  Misses         37       37              
Impacted Files Coverage Δ
src/mkdir.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cb64b92...44a1324. Read the comment docs.

@rivy rivy requested a review from nfischer February 21, 2021 18:54
Copy link
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

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

Looks good! I think we can remove the istanbul ignore next part, but this is great otherwise!

src/mkdir.js Outdated
fs.mkdirSync(dir, parseInt('0777', 8));
} catch (e) {
// swallow directory exists errors ('EEXIST'); mitigates race conditions
/* istanbul ignore next */
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove istanbul ignore next because this now handles the non-racy condition where base dir exists.

You could also update the comment since this isn't only for race conditions (it's just the non-racy way to create a directory if it doesn't already exist).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like this caught some missing test coverage 😄 I think we weren't testing the case where mkdir -p is called on a directory which already exists. I filed issue #1022 to track this.

@nfischer nfischer merged commit 71085db into shelljs:master Mar 8, 2021
@nfischer nfischer added the fix Bug/defect, or a fix for such a problem label Mar 8, 2021
@nfischer nfischer added this to the v0.9.0 milestone Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug/defect, or a fix for such a problem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mkdirSyncRecursive() shouldn't fail when directory exists unexpectedly
3 participants