Skip to content

Commit 30e1f0f

Browse files
committed
fix: Patch pkg to fix race condition in mkdir
Replace custom recursive directory creation with built-in recursive mkdir in node addon loading When multiple packaged applications try to start simultaneously, they can encounter race conditions when creating temporary directories for loading native node addons. This manifests as an "EEXIST" error when calling `mkdirSync()`. The issue occurs in the native module loading code path where pkg needs to extract native addons to a temporary location before they can be loaded via `process.dlopen()`. The current implementation uses a custom `createDirRecursively()` function that has a race condition - it checks if a directory exists and then tries to create it, but another process could create the directory between the check and creation. Node.js has built-in support for recursive directory creation via the `recursive: true` option in `mkdirSync()`. This handles race conditions properly - if the directory already exists, it will not throw an error. This is exactly what we need.
1 parent 13292d9 commit 30e1f0f

File tree

3 files changed

+35
-187
lines changed

3 files changed

+35
-187
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
diff --git a/prelude/bootstrap.js b/prelude/bootstrap.js
2+
index 034bd153d92d9db9e7a37b9dc63ddf4277f1a9ee..60e28754735337168db805e9c042599c838593f8 100644
3+
--- a/prelude/bootstrap.js
4+
+++ b/prelude/bootstrap.js
5+
@@ -249,12 +249,6 @@ function copyFolderRecursiveSync(source, target) {
6+
}
7+
}
8+
9+
-function createDirRecursively(dir) {
10+
- if (!fs.existsSync(dir)) {
11+
- createDirRecursively(path.join(dir, '..'));
12+
- fs.mkdirSync(dir);
13+
- }
14+
-}
15+
16+
/*
17+
18+
@@ -2209,7 +2203,7 @@ function payloadFileSync(pointer) {
19+
// Example: /tmp/pkg/<hash>
20+
const tmpFolder = path.join(tmpdir(), 'pkg', hash);
21+
22+
- createDirRecursively(tmpFolder);
23+
+ fs.mkdirSync(tmpFolder, { recursive: true });
24+
25+
// Example: moduleFolder = /snapshot/appname/node_modules/sharp/build/Release
26+
const parts = moduleFolder.split(path.sep);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
},
4747
"resolutions": {
4848
"web-auth-library": "getappmap/web-auth-library#v1.0.3-cjs",
49-
"whatwg-url": "14.0.0"
49+
"whatwg-url": "14.0.0",
50+
"pkg@5.8.1": "patch:pkg@npm:5.8.1#.yarn/patches/pkg-npm-5.8.1-db9700609f.patch"
5051
}
5152
}

yarn.lock

Lines changed: 7 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -5176,16 +5176,6 @@ __metadata:
51765176
languageName: node
51775177
linkType: hard
51785178

5179-
"@babel/types@npm:7.18.4":
5180-
version: 7.18.4
5181-
resolution: "@babel/types@npm:7.18.4"
5182-
dependencies:
5183-
"@babel/helper-validator-identifier": ^7.16.7
5184-
to-fast-properties: ^2.0.0
5185-
checksum: 85df59beb99c1b95e9e41590442f2ffa1e5b1b558d025489db40c9f7c906bd03a17da26c3ec486e5800e80af27c42ca7eee9506d9212ab17766d2d68d30fbf52
5186-
languageName: node
5187-
linkType: hard
5188-
51895179
"@babel/types@npm:7.19.0":
51905180
version: 7.19.0
51915181
resolution: "@babel/types@npm:7.19.0"
@@ -14588,7 +14578,7 @@ __metadata:
1458814578
languageName: node
1458914579
linkType: hard
1459014580

14591-
"aproba@npm:^1.0.3, aproba@npm:^1.1.1":
14581+
"aproba@npm:^1.1.1":
1459214582
version: 1.2.0
1459314583
resolution: "aproba@npm:1.2.0"
1459414584
checksum: 0fca141966559d195072ed047658b6e6c4fe92428c385dd38e288eacfc55807e7b4989322f030faff32c0f46bb0bc10f1e0ac32ec22d25315a1e5bbc0ebb76dc
@@ -14629,16 +14619,6 @@ __metadata:
1462914619
languageName: node
1463014620
linkType: hard
1463114621

14632-
"are-we-there-yet@npm:~1.1.2":
14633-
version: 1.1.7
14634-
resolution: "are-we-there-yet@npm:1.1.7"
14635-
dependencies:
14636-
delegates: ^1.0.0
14637-
readable-stream: ^2.0.6
14638-
checksum: 70d251719c969b2745bfe5ddf3ebaefa846a636e90a6d5212573676af5d6670e15457761d4725731e19cbebdce42c4ab0cbedf23ab047f2a08274985aa10a3c7
14639-
languageName: node
14640-
linkType: hard
14641-
1464214622
"arg@npm:^4.1.0":
1464314623
version: 4.1.3
1464414624
resolution: "arg@npm:4.1.3"
@@ -17573,13 +17553,6 @@ __metadata:
1757317553
languageName: node
1757417554
linkType: hard
1757517555

17576-
"code-point-at@npm:^1.0.0":
17577-
version: 1.1.0
17578-
resolution: "code-point-at@npm:1.1.0"
17579-
checksum: 17d5666611f9b16d64fdf48176d9b7fb1c7d1c1607a189f7e600040a11a6616982876af148230336adb7d8fe728a559f743a4e29db3747e3b1a32fa7f4529681
17580-
languageName: node
17581-
linkType: hard
17582-
1758317556
"collect-v8-coverage@npm:^1.0.0":
1758417557
version: 1.0.1
1758517558
resolution: "collect-v8-coverage@npm:1.0.1"
@@ -17942,7 +17915,7 @@ __metadata:
1794217915
languageName: node
1794317916
linkType: hard
1794417917

17945-
"console-control-strings@npm:^1.0.0, console-control-strings@npm:^1.1.0, console-control-strings@npm:~1.1.0":
17918+
"console-control-strings@npm:^1.0.0, console-control-strings@npm:^1.1.0":
1794617919
version: 1.1.0
1794717920
resolution: "console-control-strings@npm:1.1.0"
1794817921
checksum: 8755d76787f94e6cf79ce4666f0c5519906d7f5b02d4b884cf41e11dcd759ed69c57da0670afd9236d229a46e0f9cf519db0cd829c6dca820bb5a5c3def584ed
@@ -19621,15 +19594,6 @@ __metadata:
1962119594
languageName: node
1962219595
linkType: hard
1962319596

19624-
"decompress-response@npm:^4.2.0":
19625-
version: 4.2.1
19626-
resolution: "decompress-response@npm:4.2.1"
19627-
dependencies:
19628-
mimic-response: ^2.0.0
19629-
checksum: 4e783ca4dfe9417354d61349750fe05236f565a4415a6ca20983a311be2371debaedd9104c0b0e7b36e5f167aeaae04f84f1a0b3f8be4162f1d7d15598b8fdba
19630-
languageName: node
19631-
linkType: hard
19632-
1963319597
"decompress-response@npm:^6.0.0":
1963419598
version: 6.0.0
1963519599
resolution: "decompress-response@npm:6.0.0"
@@ -19986,15 +19950,6 @@ __metadata:
1998619950
languageName: node
1998719951
linkType: hard
1998819952

19989-
"detect-libc@npm:^1.0.3":
19990-
version: 1.0.3
19991-
resolution: "detect-libc@npm:1.0.3"
19992-
bin:
19993-
detect-libc: ./bin/detect-libc.js
19994-
checksum: daaaed925ffa7889bd91d56e9624e6c8033911bb60f3a50a74a87500680652969dbaab9526d1e200a4c94acf80fc862a22131841145a0a8482d60a99c24f4a3e
19995-
languageName: node
19996-
linkType: hard
19997-
1999819953
"detect-libc@npm:^2.0.0":
1999919954
version: 2.0.3
2000019955
resolution: "detect-libc@npm:2.0.3"
@@ -24234,22 +24189,6 @@ __metadata:
2423424189
languageName: node
2423524190
linkType: hard
2423624191

24237-
"gauge@npm:~2.7.3":
24238-
version: 2.7.4
24239-
resolution: "gauge@npm:2.7.4"
24240-
dependencies:
24241-
aproba: ^1.0.3
24242-
console-control-strings: ^1.0.0
24243-
has-unicode: ^2.0.0
24244-
object-assign: ^4.1.0
24245-
signal-exit: ^3.0.0
24246-
string-width: ^1.0.1
24247-
strip-ansi: ^3.0.1
24248-
wide-align: ^1.1.0
24249-
checksum: a89b53cee65579b46832e050b5f3a79a832cc422c190de79c6b8e2e15296ab92faddde6ddf2d376875cbba2b043efa99b9e1ed8124e7365f61b04e3cee9d40ee
24250-
languageName: node
24251-
linkType: hard
24252-
2425324192
"generic-names@npm:^1.0.2":
2425424193
version: 1.0.3
2425524194
resolution: "generic-names@npm:1.0.3"
@@ -25095,7 +25034,7 @@ __metadata:
2509525034
languageName: node
2509625035
linkType: hard
2509725036

25098-
"has-unicode@npm:^2.0.0, has-unicode@npm:^2.0.1":
25037+
"has-unicode@npm:^2.0.1":
2509925038
version: 2.0.1
2510025039
resolution: "has-unicode@npm:2.0.1"
2510125040
checksum: 1eab07a7436512db0be40a710b29b5dc21fa04880b7f63c9980b706683127e3c1b57cb80ea96d47991bdae2dfe479604f6a1ba410106ee1046a41d1bd0814400
@@ -26620,15 +26559,6 @@ __metadata:
2662026559
languageName: node
2662126560
linkType: hard
2662226561

26623-
"is-fullwidth-code-point@npm:^1.0.0":
26624-
version: 1.0.0
26625-
resolution: "is-fullwidth-code-point@npm:1.0.0"
26626-
dependencies:
26627-
number-is-nan: ^1.0.0
26628-
checksum: 4d46a7465a66a8aebcc5340d3b63a56602133874af576a9ca42c6f0f4bd787a743605771c5f246db77da96605fefeffb65fc1dbe862dcc7328f4b4d03edf5a57
26629-
languageName: node
26630-
linkType: hard
26631-
2663226562
"is-fullwidth-code-point@npm:^2.0.0":
2663326563
version: 2.0.0
2663426564
resolution: "is-fullwidth-code-point@npm:2.0.0"
@@ -31937,13 +31867,6 @@ __metadata:
3193731867
languageName: node
3193831868
linkType: hard
3193931869

31940-
"mimic-response@npm:^2.0.0":
31941-
version: 2.1.0
31942-
resolution: "mimic-response@npm:2.1.0"
31943-
checksum: 014fad6ab936657e5f2f48bd87af62a8e928ebe84472aaf9e14fec4fcb31257a5edff77324d8ac13ddc6685ba5135cf16e381efac324e5f174fb4ddbf902bf07
31944-
languageName: node
31945-
linkType: hard
31946-
3194731870
"mimic-response@npm:^3.1.0":
3194831871
version: 3.1.0
3194931872
resolution: "mimic-response@npm:3.1.0"
@@ -32692,15 +32615,6 @@ __metadata:
3269232615
languageName: node
3269332616
linkType: hard
3269432617

32695-
"node-abi@npm:^2.21.0":
32696-
version: 2.30.1
32697-
resolution: "node-abi@npm:2.30.1"
32698-
dependencies:
32699-
semver: ^5.4.1
32700-
checksum: 3f4b0c912ce4befcd7ceab4493ba90b51d60dfcc90f567c93f731d897ef8691add601cb64c181683b800f21d479d68f9a6e15d8ab8acd16a5706333b9e30a881
32701-
languageName: node
32702-
linkType: hard
32703-
3270432618
"node-abi@npm:^3.3.0":
3270532619
version: 3.57.0
3270632620
resolution: "node-abi@npm:3.57.0"
@@ -33400,18 +33314,6 @@ __metadata:
3340033314
languageName: node
3340133315
linkType: hard
3340233316

33403-
"npmlog@npm:^4.0.1":
33404-
version: 4.1.2
33405-
resolution: "npmlog@npm:4.1.2"
33406-
dependencies:
33407-
are-we-there-yet: ~1.1.2
33408-
console-control-strings: ~1.1.0
33409-
gauge: ~2.7.3
33410-
set-blocking: ~2.0.0
33411-
checksum: edbda9f95ec20957a892de1839afc6fb735054c3accf6fbefe767bac9a639fd5cea2baeac6bd2bcd50a85cb54924d57d9886c81c7fbc2332c2ddd19227504192
33412-
languageName: node
33413-
linkType: hard
33414-
3341533317
"npmlog@npm:^5.0.1":
3341633318
version: 5.0.1
3341733319
resolution: "npmlog@npm:5.0.1"
@@ -33480,13 +33382,6 @@ __metadata:
3348033382
languageName: node
3348133383
linkType: hard
3348233384

33483-
"number-is-nan@npm:^1.0.0":
33484-
version: 1.0.1
33485-
resolution: "number-is-nan@npm:1.0.1"
33486-
checksum: 13656bc9aa771b96cef209ffca31c31a03b507ca6862ba7c3f638a283560620d723d52e626d57892c7fff475f4c36ac07f0600f14544692ff595abff214b9ffb
33487-
languageName: node
33488-
linkType: hard
33489-
3349033385
"nwsapi@npm:^2.0.7, nwsapi@npm:^2.2.0":
3349133386
version: 2.2.0
3349233387
resolution: "nwsapi@npm:2.2.0"
@@ -34895,36 +34790,7 @@ __metadata:
3489534790
languageName: node
3489634791
linkType: hard
3489734792

34898-
"pkg@npm:^5.8.0":
34899-
version: 5.8.0
34900-
resolution: "pkg@npm:5.8.0"
34901-
dependencies:
34902-
"@babel/generator": 7.18.2
34903-
"@babel/parser": 7.18.4
34904-
"@babel/types": 7.18.4
34905-
chalk: ^4.1.2
34906-
fs-extra: ^9.1.0
34907-
globby: ^11.1.0
34908-
into-stream: ^6.0.0
34909-
is-core-module: 2.9.0
34910-
minimist: ^1.2.6
34911-
multistream: ^4.1.0
34912-
pkg-fetch: 3.4.2
34913-
prebuild-install: 6.1.4
34914-
resolve: ^1.22.0
34915-
stream-meter: ^1.0.4
34916-
peerDependencies:
34917-
node-notifier: ">=9.0.1"
34918-
peerDependenciesMeta:
34919-
node-notifier:
34920-
optional: true
34921-
bin:
34922-
pkg: lib-es5/bin.js
34923-
checksum: eb85975275ab4c5760884c1cc9b6251a17a8c15eb1b5a13183d0a5a0385650459e44d8438af7ecf5c5d185b55046cce9ce705b5b6d59ae058a157f49f6fb56a0
34924-
languageName: node
34925-
linkType: hard
34926-
34927-
"pkg@npm:^5.8.1":
34793+
"pkg@npm:^5.8.0, pkg@npm:^5.8.1":
3492834794
version: 5.8.1
3492934795
resolution: "pkg@npm:5.8.1"
3493034796
dependencies:
@@ -35671,29 +35537,6 @@ __metadata:
3567135537
languageName: node
3567235538
linkType: hard
3567335539

35674-
"prebuild-install@npm:6.1.4":
35675-
version: 6.1.4
35676-
resolution: "prebuild-install@npm:6.1.4"
35677-
dependencies:
35678-
detect-libc: ^1.0.3
35679-
expand-template: ^2.0.3
35680-
github-from-package: 0.0.0
35681-
minimist: ^1.2.3
35682-
mkdirp-classic: ^0.5.3
35683-
napi-build-utils: ^1.0.1
35684-
node-abi: ^2.21.0
35685-
npmlog: ^4.0.1
35686-
pump: ^3.0.0
35687-
rc: ^1.2.7
35688-
simple-get: ^3.0.3
35689-
tar-fs: ^2.0.0
35690-
tunnel-agent: ^0.6.0
35691-
bin:
35692-
prebuild-install: bin.js
35693-
checksum: de4313eda821305912af922700a2db04bb8e77fe8aa9c2788550f1000c026cbefc82da468ec0c0a37764c5417bd8169dbd540928535fb38d00bb9bbd673dd217
35694-
languageName: node
35695-
linkType: hard
35696-
3569735540
"prebuild-install@npm:7.1.1":
3569835541
version: 7.1.1
3569935542
resolution: "prebuild-install@npm:7.1.1"
@@ -36866,7 +36709,7 @@ __metadata:
3686636709
languageName: node
3686736710
linkType: hard
3686836711

36869-
"readable-stream@npm:1 || 2, readable-stream@npm:^2.0.0, readable-stream@npm:^2.0.1, readable-stream@npm:^2.0.2, readable-stream@npm:^2.0.6, readable-stream@npm:^2.1.4, readable-stream@npm:^2.1.5, readable-stream@npm:^2.2.2, readable-stream@npm:^2.3.3, readable-stream@npm:^2.3.6, readable-stream@npm:~2.3.6":
36712+
"readable-stream@npm:1 || 2, readable-stream@npm:^2.0.0, readable-stream@npm:^2.0.1, readable-stream@npm:^2.0.2, readable-stream@npm:^2.1.4, readable-stream@npm:^2.1.5, readable-stream@npm:^2.2.2, readable-stream@npm:^2.3.3, readable-stream@npm:^2.3.6, readable-stream@npm:~2.3.6":
3687036713
version: 2.3.7
3687136714
resolution: "readable-stream@npm:2.3.7"
3687236715
dependencies:
@@ -38642,7 +38485,7 @@ resolve@1.1.7:
3864238485
languageName: node
3864338486
linkType: hard
3864438487

38645-
"set-blocking@npm:^2.0.0, set-blocking@npm:~2.0.0":
38488+
"set-blocking@npm:^2.0.0":
3864638489
version: 2.0.0
3864738490
resolution: "set-blocking@npm:2.0.0"
3864838491
checksum: 6e65a05f7cf7ebdf8b7c75b101e18c0b7e3dff4940d480efed8aad3a36a4005140b660fa1d804cb8bce911cac290441dc728084a30504d3516ac2ff7ad607b02
@@ -38903,17 +38746,6 @@ resolve@1.1.7:
3890338746
languageName: node
3890438747
linkType: hard
3890538748

38906-
"simple-get@npm:^3.0.3":
38907-
version: 3.1.1
38908-
resolution: "simple-get@npm:3.1.1"
38909-
dependencies:
38910-
decompress-response: ^4.2.0
38911-
once: ^1.3.1
38912-
simple-concat: ^1.0.0
38913-
checksum: 80195e70bf171486e75c31e28e5485468195cc42f85940f8b45c4a68472160144d223eb4d07bc82ef80cb974b7c401db021a540deb2d34ac4b3b8883da2d6401
38914-
languageName: node
38915-
linkType: hard
38916-
3891738749
"simple-get@npm:^4.0.0":
3891838750
version: 4.0.1
3891938751
resolution: "simple-get@npm:4.0.1"
@@ -39809,17 +39641,6 @@ resolve@1.1.7:
3980939641
languageName: node
3981039642
linkType: hard
3981139643

39812-
"string-width@npm:^1.0.1":
39813-
version: 1.0.2
39814-
resolution: "string-width@npm:1.0.2"
39815-
dependencies:
39816-
code-point-at: ^1.0.0
39817-
is-fullwidth-code-point: ^1.0.0
39818-
strip-ansi: ^3.0.0
39819-
checksum: 5c79439e95bc3bd7233a332c5f5926ab2ee90b23816ed4faa380ce3b2576d7800b0a5bb15ae88ed28737acc7ea06a518c2eef39142dd727adad0e45c776cd37e
39820-
languageName: node
39821-
linkType: hard
39822-
3982339644
"string-width@npm:^3.0.0, string-width@npm:^3.1.0":
3982439645
version: 3.1.0
3982539646
resolution: "string-width@npm:3.1.0"
@@ -43438,7 +43259,7 @@ typescript@~4.4.3:
4343843259
languageName: node
4343943260
linkType: hard
4344043261

43441-
"wide-align@npm:^1.1.0, wide-align@npm:^1.1.2, wide-align@npm:^1.1.5":
43262+
"wide-align@npm:^1.1.2, wide-align@npm:^1.1.5":
4344243263
version: 1.1.5
4344343264
resolution: "wide-align@npm:1.1.5"
4344443265
dependencies:

0 commit comments

Comments
 (0)