Skip to content

bug(opcache): refresh() unlinks the just-written patched binary when shared memory + file cache are both active #252

Closed

Description

@lisachenko

Summary

Found while building the SHM integration coverage for #125 (PR #251). In a process running with SHM active plus opcache.file_cache (the hybrid mode, not file_cache_only), BinaryCacheFile::refresh() cannot deliver its documented "next include picks the patched binary up": its save-then-invalidate ordering self-destructs.

Verified empirically on PHP 8.4.19 and against php-src:

  • opcache_invalidate($path, true) with opcache.file_cache set calls zend_file_cache_invalidate(), which unlinks the .bin — deleting the patched binary save() wrote a moment earlier. The subsequent include recompiles the original source (observed: bin-exists-after-refresh=false, re-include returns the unpatched value), and the patch is lost pool-wide (any FPM worker would re-cache the original).
  • Reordering to invalidate-then-save is necessary but not sufficient for same-process pickup: with the default opcache.revalidate_path=0, the invalidated hash entry short-circuits path resolution in persistent_compile_file(), so zend_file_cache_script_load() bails on the unresolved opened_path and the source is recompiled. With opcache.revalidate_path=1 the invalidate-then-save ordering works in-process (verified); a fresh process picks the patched bin up with default settings either way.
  • Under opcache.file_cache_only=1 (the mode RefreshWorkflowTest proves) opcache_invalidate returns false and touches nothing — the existing behavior and docs remain correct there.

Fix shape (verified during the investigation)

  1. Reorder refresh() (src/OpCache/BinaryCacheFile.php): invalidate first, then save() — so the unlink hits the stale binary, not the patched one.
  2. Document the same-process pickup caveat (opcache.revalidate_path=1, or a fresh worker) in docs/opcache-binary.md's "Refresh and shared memory" section and the refresh() docblock.
  3. Flip the PR test(opcache): prove BinaryCacheFile::refresh() semantics under real shared memory #251 test expectations that deliberately document the broken behavior (class docblock notes) into assertions of the fixed behavior.

Applies to branch 8.4 first, cascades to master. Part of the opcache meta-epic #245.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopcacheIssue is related to opcache integration. Either doesn't work well or behaves incorrect.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions