Commit 0b85ec9
committed
fix(cache-with-ttl): close TOCTOU in getOrFetch inflight dedupe
`getOrFetch` did `const cached = await get(key)` *before* consulting the
inflight map. `get()` itself awaits a cacache disk read, so two callers
landing on a cold key could both suspend on the same I/O, both resolve
`cached === undefined`, both skip the inflight check, and both start a
fresh fetch. The `set()`-before-await comment was accurate but the
check ordering was the actual race — the inflight guarantee was
invisibly broken for every concurrent first-hit, defeating the feature
the map exists to provide.
Moves the inflight check before the persistent-cache lookup so a later
caller joins an in-flight fetch instead of racing it. Adds a second
check after the disk read for the case where a peer registered an
inflight entry while we were suspended. Regression test asserts
fetchCount === 1 for three parallel cold callers.1 parent 496017d commit 0b85ec9
2 files changed
Lines changed: 49 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
421 | 421 | | |
422 | 422 | | |
423 | 423 | | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
424 | 436 | | |
425 | 437 | | |
426 | 438 | | |
427 | 439 | | |
428 | 440 | | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
435 | 446 | | |
436 | 447 | | |
437 | 448 | | |
| |||
446 | 457 | | |
447 | 458 | | |
448 | 459 | | |
449 | | - | |
| 460 | + | |
450 | 461 | | |
451 | 462 | | |
452 | 463 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
554 | 554 | | |
555 | 555 | | |
556 | 556 | | |
557 | | - | |
558 | | - | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
559 | 588 | | |
560 | 589 | | |
561 | 590 | | |
| |||
0 commit comments