Repository navigation
Description
Bug description
Under Laravel Octane, an entry that is created by one process can't be found by a worker that was already running. Its page returns a 404 from that worker until the worker is reloaded, while a fresh PHP process serves it with a 200. The same happens after an entry's slug is changed: the new URL 404s in the existing worker.
In practice that means that with more than one Octane worker, an entry saved in the Control Panel through one worker is missing for requests handled by the others. It also applies to anything else that writes content outside the worker: a queue worker, a console command, a deploy.
In my testing, queries in the stale worker did count the new entry, since the indexes are read from the cache again on each request. It's fetching the entry itself that fails.
Cause, as far as I can tell. The Stache and its stores are made when the app boots (Stache\ServiceProvider::boot()), so under Octane they live in the base application and are shared by every request a worker handles. Each store memoises its id-to-path list in Store::$paths the first time it's used and never reads it again. BasicStore::getItem() looks the id up in that list, doesn't find it, and returns null.
Queue workers had the same problem and got a reset between jobs (Stache\ServiceProvider::resetMemoizedStateBetweenJobs(), after #15225). Nothing equivalent happens when an Octane request arrives.
A fix that works here. Doing the same reset on Octane's RequestReceived makes every step below return what a fresh process returns:
Event::listen(\Laravel\Octane\Events\RequestReceived::class, function () {
Stache::stores()->each->resetMemoizedState();
});AppServiceProvider already listens for that event to clear the FFmpeg binary cache, so there's a precedent for where it would go. I'm happy to send a PR with a test if that's the direction you'd take.
How to reproduce
- A Statamic 6 site with
laravel/octaneinstalled and a collection with a route, sayarticlesat/articles/{slug}. - Start Octane with one worker and request any existing article, so the worker has used the collection.
- From another process, create an entry:
// php artisan tinker Statamic\Facades\Entry::make()->collection('articles')->slug('made-elsewhere')->data(['title' => 'Made elsewhere'])->save();
- Request
/articles/made-elsewhere. It's a 404 from the running worker. Restart Octane and it's a 200.
What I actually ran, so the result doesn't depend on timing or on which worker picks up a request, drives Octane's own Laravel\Octane\Worker class in one PHP process with a client that hands it requests, and uses a second PHP process for the writes. It doesn't need Swoole, RoadRunner or FrankenPHP. I have not run it against a live Octane server.
| Step | Long-lived Octane worker | Fresh PHP process |
|---|---|---|
| An existing article | 200 | |
| An article created by another process | 404 | 200 |
| The same request again | 404 | |
| Its new URL after another process changes its slug | 404 | 200 |
| Its old URL | 404 | |
| Its URL after another process deletes it | 404 |
With the reset above added, the second, third and fourth rows are 200 and show the right title.
The worker script
<?php
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Laravel\Octane\ApplicationFactory;
use Laravel\Octane\Contracts\Client;
use Laravel\Octane\OctaneResponse;
use Laravel\Octane\RequestContext;
use Laravel\Octane\Worker;
require __DIR__.'/vendor/autoload.php';
$client = new class implements Client
{
public ?OctaneResponse $response = null;
public function marshalRequest(RequestContext $context): array
{
return [$context->request, $context];
}
public function respond(RequestContext $context, OctaneResponse $response): void
{
$this->response = $response;
}
public function error(Throwable $e, Application $app, Request $request, RequestContext $context): void
{
throw $e;
}
};
$worker = new Worker(new ApplicationFactory(__DIR__), $client);
$worker->boot();
$get = function (string $uri) use ($worker, $client) {
$request = Request::create('http://localhost'.$uri, 'GET');
$worker->handle($request, new RequestContext(['request' => $request]));
return $client->response->response->getStatusCode();
};
echo 'existing article: '.$get('/articles/an-existing-article')."\n";
// Another process creates an entry, and can see it.
$tinker = fn (string $code) => trim(shell_exec(escapeshellarg(PHP_BINARY).' artisan tinker --execute='.escapeshellarg($code)));
$tinker("Statamic\\Facades\\Entry::make()->collection('articles')->slug('made-elsewhere')->data(['title' => 'Made elsewhere'])->save();");
echo 'in another process: '.$tinker("echo Statamic\\Facades\\Entry::findByUri('/articles/made-elsewhere') ? 'found' : 'missing';")."\n";
echo 'in this worker: '.$get('/articles/made-elsewhere')."\n";Logs
(none: the worker returns a normal 404)
Environment
Statamic 6.41.0 (source at 7097691b0); also reproduced on 6.40.0
Laravel 13.42.0
laravel/octane 2.24.0
PHP 8.5.12
Stache watcher: disabled
Stache cache store: file
Installation
statamic/statamic, with generated content.
Additional details
- Turning the Stache watcher on didn't change the result when I tried it. A store only checks for file changes once (
$fileChangesHandled), and that flag lives on the same long-lived store. - A queue worker in the same situation is fine, because of the between-jobs reset.
- Related: Deleted entries can be resurrected as empty stub files by a long-lived queue worker (Stache/Blink staleness +
watcher=false) #15225 (the queue worker equivalent), [6.x] Fix error response codes sticking to later pages in long-lived processes #15454 and [6.x] Reset Antlers interpolated variables between requests #15815 (other state that outlived a request in long-lived processes).
Activity