Skip to content

fix(router): clear the matched route between requests - #2278

Merged
aidan-casey merged 2 commits into
tempestphp:3.xfrom
osbre:fix/matched-route-staleness
Sep 6, 2026
Merged

fix(router): clear the matched route between requests#2278
aidan-casey merged 2 commits into
tempestphp:3.xfrom
osbre:fix/matched-route-staleness

Conversation

@osbre

@osbre osbre commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Implement RouterReset to unregister MatchedRoute from the container during resets, and clear the binding in MatchRouteMiddleware before matching.

Problem

MatchRouteMiddleware binds MatchedRoute into the container as a singleton. In long-running workers (FrankenPHP worker mode) or test suites where the container is reused across requests, this state leaks across boundaries: GenericContainer::reset() clears resolved singletons and dynamic initializers, but not singletonDefinitions, where the binding lives. Anything sharing the container - the next request, a console command, a deferred task - incorrectly inherits the previous request's matched route, causing stale data in URI generation, route model binding, and middleware execution.

Solution

  • Create RouterReset implementing Resettable to automatically unregister MatchedRoute::class during container resets, discovered via ResettableDiscovery and following the existing resets from worker mode support (feat(core)!: worker mode support #2172).
  • Clear any existing MatchedRoute binding inside MatchRouteMiddleware before running a new match, ensuring safe reuse even when container resets do not occur, ie in test suite:
$this->http->get('/docs/installation')->assertOk();
$this->http->get('/does-not-exist')->assertNotFound();

is_current_uri([DocsController::class, 'show'], slug: 'installation'); // true, on a 404

@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

Benchmark Results

Comparison of fix/matched-route-staleness against 3.x (cf035264805564eb789f2741734e481aa3696b4d).

Open to see the benchmark results
Benchmark Set Mem. Peak Time Variability
ViewRenderBench(benchPlainHtml) - 22.035mb 0.00% 489.072μs -7.30% ±2.09% +127.14%

Generated by phpbench against commit c31738d

@aidan-casey

Copy link
Copy Markdown
Member

Nice! Looks good to me.

Comment thread packages/router/src/MatchRouteMiddleware.php
@aidan-casey
aidan-casey merged commit 16a1a2a into tempestphp:3.x Sep 6, 2026
78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants