Skip to content

Entries created or renamed by another process 404 in a long-lived Octane worker #15857

Closed

Description

@DanielDarrenJones

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

  1. A Statamic 6 site with laravel/octane installed and a collection with a route, say articles at /articles/{slug}.
  2. Start Octane with one worker and request any existing article, so the worker has used the collection.
  3. From another process, create an entry:
    // php artisan tinker
    Statamic\Facades\Entry::make()->collection('articles')->slug('made-elsewhere')->data(['title' => 'Made elsewhere'])->save();
  4. 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

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions