Skip to content

005: The site in effect under Fibers

Status: accepted, 2026-11-08.

Context

Core runs controllers in a Fiber (Renderer::executeInRenderContext()) and starts child Fibers for placeholders and for a reference's ->entity. Three stores answered "which site is in effect" per Fiber: the current site, the site switcher's stack and the canonical scopes. None of them knew the parent Fiber, so a child started under a switch ran as the request's site, and a Fiber that switched back stayed pinned to the site it had switched back to. PHP has no pointer from a Fiber to the one that started it.

Decision

One store, SiteSwitchFrames, holds the open switches and canonical scopes per Fiber; the switcher and the content version context write it, the current site reads it. A Fiber sees its own frames on top of the frames of every running Fiber (one whose start() or resume() has not returned), ordered by when they were opened, and the frames of the request below them. The request's frames lie below those of running Fibers, even when opened later: the request is the root of every running Fiber. A suspended Fiber's frames count for nobody else. Among running Fibers, the frame opened last applies.

Consequences

  • A child Fiber inherits its running ancestors' switch and canonical scope.
  • A switch the request opens after a Fiber's frame does not reach that Fiber's children.
  • Reading the site costs nothing extra while no Fiber holds a frame.
  • A Fiber that switched back follows the request's site again.
  • SiteSwitchFrameGuardTest keeps per-Fiber state out of other classes.

Rejected options

  • Nearest ancestor: not knowable without a parent pointer.
  • Snapshot at the first read (like Python's contextvars): a child resumed after its ancestor switched back would still run as the site the ancestor left.
  • Three synchronised stores: they drifted apart.

Reopen when

PHP gives Fibers a parent pointer, or a case turns up where the frame opened last among running Fibers is not the one the code means.

Last updated 2026-11-22