Commit 3439a89
authored
fix(libnpmexec): fix lock compromise logic (#8733)
Fix a race condition in `withLock` where a slow `fs.stat` call could
result in an ECOMPROMISED false positive. Due to the usage of
`setInterval`, one callback could mutate `mtime` just before an
overlapping callback's `fs.stat` promise has resolved, causing a
mismatch. By switching to `setTimeout`, we ensure that we don't have
overlapping callbacks and incorrect values.
Additionally bump the stale threshold higher, to reduce the likelihood
of another caller taking over a seemingly-stale-but-actually-active
lock. Under Windows in particular, `fs.stat` [has been
observed](#8710 (comment))
to sometimes take over 20 seconds, so we should err on the side of a
higher threshold before we judge a lock as stale. The minor potential
downside is that we might wait longer before taking over a stale lock,
but lock takeover is already a very exceptional case (i.e. it would
typically only happen if another process was SIGKILLed while holding the
same lock)
## Testing Notes
- Added a new test to cover this scenario
- Verified [the
failure](https://github.com/jenseng/cli/actions/runs/19373681768/job/55435674539)
and [the
fix](https://github.com/jenseng/cli/actions/runs/19373765497/job/55435952370)
via one-off GHA workflow that does `npx --yes jest --version`
## References
Fixes #87101 parent c6242d9 commit 3439a89
2 files changed
+33
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | | - | |
23 | | - | |
| 23 | + | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
| 148 | + | |
147 | 149 | | |
148 | 150 | | |
149 | 151 | | |
| |||
156 | 158 | | |
157 | 159 | | |
158 | 160 | | |
| 161 | + | |
159 | 162 | | |
160 | 163 | | |
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
164 | 167 | | |
165 | | - | |
166 | | - | |
| 168 | + | |
167 | 169 | | |
168 | | - | |
| 170 | + | |
169 | 171 | | |
170 | 172 | | |
171 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
228 | 248 | | |
229 | 249 | | |
230 | 250 | | |
| |||
259 | 279 | | |
260 | 280 | | |
261 | 281 | | |
262 | | - | |
| 282 | + | |
263 | 283 | | |
264 | 284 | | |
265 | 285 | | |
| |||
301 | 321 | | |
302 | 322 | | |
303 | 323 | | |
304 | | - | |
| 324 | + | |
305 | 325 | | |
306 | 326 | | |
307 | 327 | | |
| |||
314 | 334 | | |
315 | 335 | | |
316 | 336 | | |
317 | | - | |
| 337 | + | |
318 | 338 | | |
319 | 339 | | |
320 | 340 | | |
| |||
0 commit comments