Commit e110a90
Fix GH-8739: opcache_reset()/invalidate() races via epoch-based reclamation
opcache_reset() and opcache_invalidate() destroy shared memory (hash
table memset, interned strings restore, SHM watermark reset) while
concurrent reader threads/processes still hold pointers into that
memory. This causes zend_mm_heap corrupted crashes under concurrent
load in both ZTS (FrankenPHP, parallel) and FPM configurations.
A prior fix attempt (PR #14803) wrapped cache lookups in
zend_shared_alloc_lock(), which was rejected by Dmitry Stogov because
readers must remain lock-free. This patch introduces epoch-based
reclamation (EBR), a proven lock-free pattern also used by RCU in the
Linux kernel and by Crossbeam in Rust:
- Readers publish their epoch on request start (one atomic store) and
clear it on request end (one atomic store). No locks acquired.
- Writers (opcache_reset path) increment the global epoch and defer the
actual hash/interned-string cleanup until all pre-epoch readers have
completed. The deferred reset completes at the next request boundary
after the drain is complete.
- The existing immediate-cleanup fast path is retained for the case
when accel_is_inactive() reports no active readers.
Additional safety:
- When the per-slot pool (256 slots) is exhausted, readers fall back
to an aggregate overflow counter that unconditionally blocks
deferred reclamation while non-zero. This preserves safety for
installations with more concurrent readers than slots.
- UNASSIGNED vs OVERFLOW sentinels distinguish "slot not yet attempted"
from "allocation failed", avoiding per-request atomic contention on
the slot-next counter once slots are exhausted.
- A full memory barrier after zend_accel_hash_clean()'s memset ensures
readers on weak-memory architectures see the zeroed table before any
subsequent insert.
- A defensive ZCG(locked) check in accel_try_complete_deferred_reset()
prevents the ZEND_ASSERT(!ZCG(locked)) failure that would otherwise
fire if the completer is re-entered while the SHM lock is held.
Fixes GH-8739
Fixes GH-14471
Fixes GH-18517
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent fbd3017 commit e110a90
File tree
7 files changed
+424
-0
lines changed- ext/opcache
- tests
7 files changed
+424
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| 139 | + | |
| 140 | + | |
139 | 141 | | |
140 | 142 | | |
141 | 143 | | |
| |||
402 | 404 | | |
403 | 405 | | |
404 | 406 | | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
405 | 576 | | |
406 | 577 | | |
407 | 578 | | |
| |||
2692 | 2863 | | |
2693 | 2864 | | |
2694 | 2865 | | |
| 2866 | + | |
| 2867 | + | |
| 2868 | + | |
| 2869 | + | |
| 2870 | + | |
| 2871 | + | |
| 2872 | + | |
| 2873 | + | |
2695 | 2874 | | |
2696 | 2875 | | |
2697 | 2876 | | |
| |||
2735 | 2914 | | |
2736 | 2915 | | |
2737 | 2916 | | |
| 2917 | + | |
| 2918 | + | |
| 2919 | + | |
| 2920 | + | |
| 2921 | + | |
| 2922 | + | |
| 2923 | + | |
| 2924 | + | |
| 2925 | + | |
| 2926 | + | |
| 2927 | + | |
| 2928 | + | |
| 2929 | + | |
| 2930 | + | |
| 2931 | + | |
| 2932 | + | |
| 2933 | + | |
| 2934 | + | |
| 2935 | + | |
| 2936 | + | |
| 2937 | + | |
| 2938 | + | |
| 2939 | + | |
| 2940 | + | |
| 2941 | + | |
| 2942 | + | |
| 2943 | + | |
| 2944 | + | |
| 2945 | + | |
2738 | 2946 | | |
2739 | 2947 | | |
2740 | 2948 | | |
| |||
2789 | 2997 | | |
2790 | 2998 | | |
2791 | 2999 | | |
| 3000 | + | |
| 3001 | + | |
| 3002 | + | |
| 3003 | + | |
| 3004 | + | |
| 3005 | + | |
| 3006 | + | |
| 3007 | + | |
| 3008 | + | |
| 3009 | + | |
| 3010 | + | |
2792 | 3011 | | |
2793 | 3012 | | |
2794 | 3013 | | |
| |||
2936 | 3155 | | |
2937 | 3156 | | |
2938 | 3157 | | |
| 3158 | + | |
| 3159 | + | |
2939 | 3160 | | |
2940 | 3161 | | |
2941 | 3162 | | |
| |||
2962 | 3183 | | |
2963 | 3184 | | |
2964 | 3185 | | |
| 3186 | + | |
2965 | 3187 | | |
2966 | 3188 | | |
2967 | 3189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
59 | 108 | | |
60 | 109 | | |
61 | 110 | | |
| |||
117 | 166 | | |
118 | 167 | | |
119 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
120 | 175 | | |
121 | 176 | | |
122 | 177 | | |
| |||
219 | 274 | | |
220 | 275 | | |
221 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
222 | 280 | | |
223 | 281 | | |
224 | 282 | | |
| |||
282 | 340 | | |
283 | 341 | | |
284 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
285 | 351 | | |
286 | 352 | | |
287 | 353 | | |
| |||
328 | 394 | | |
329 | 395 | | |
330 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
331 | 404 | | |
332 | 405 | | |
333 | 406 | | |
| |||
0 commit comments