Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ProcessMaker/Listeners/HandleRedirectListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ protected function setRedirectTo(ProcessRequest $processRequest, string $method,
self::$redirectionParams = $params;
}

/**
* Reset the static state for Octane compatibility.
* This prevents data leaks between requests in long-running workers.
*/
public static function reset(): void
{
self::$processRequest = null;
self::$redirectionMethod = '';
self::$redirectionParams = [];
}

public static function sendRedirectToEvent()
{
$method = self::$redirectionMethod;
Expand Down
17 changes: 17 additions & 0 deletions ProcessMaker/Octane/ResetRequestState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Octane;

use ProcessMaker\Listeners\HandleRedirectListener;
use ProcessMaker\Providers\ProcessMakerServiceProvider;

final class ResetRequestState
{
public function handle(): void
{
ProcessMakerServiceProvider::beginRequestTiming();
HandleRedirectListener::reset();
}
}
37 changes: 30 additions & 7 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\SystemProcessCounter;
use Laravel\Horizon\WorkerCommandString;
use Laravel\Octane\Events\RequestTerminated;
use Laravel\Passport\Client as PassportClient;
use Lavary\Menu\Menu;
use OpenApi\Analysers\AttributeAnnotationFactory;
Expand All @@ -49,6 +51,7 @@
use ProcessMaker\Models;
use ProcessMaker\Multitenancy\Tenant;
use ProcessMaker\Observers;
use ProcessMaker\Octane\ResetRequestState;
use ProcessMaker\PolicyExtension;
use ProcessMaker\Providers\PermissionServiceProvider;
use ProcessMaker\Repositories\SettingsConfigRepository;
Expand Down Expand Up @@ -106,6 +109,9 @@ public function boot(): void

$this->checkConfigCache();

// Register Octane listeners if Octane is enabled
$this->registerOctaneListeners();

// Hook after service providers boot
self::$bootTime = (microtime(true) - self::$bootStart) * 1000; // Convert to milliseconds
}
Expand Down Expand Up @@ -257,7 +263,7 @@ protected static function registerEvents(): void
{
// Listen to the events for our core screen
// types and add our javascript
Facades\Event::listen(ScreenBuilderStarting::class, function ($event) {
Event::listen(ScreenBuilderStarting::class, function ($event) {
// Add any extensions to form builder
// and renderer from packages
$event->manager->addPackageScripts($event->type);
Expand All @@ -276,7 +282,7 @@ protected static function registerEvents(): void
});

// Log Notifications
Facades\Event::listen(NotificationSent::class, function ($event) {
Event::listen(NotificationSent::class, function ($event) {
$id = $event->notifiable->id;
$notifiable = get_class($event->notifiable);
$notification = get_class($event->notification);
Expand All @@ -285,24 +291,24 @@ protected static function registerEvents(): void
});

// Log Broadcasts (messages sent to laravel-echo-server and redis)
Facades\Event::listen(BroadcastNotificationCreated::class, function ($event) {
Event::listen(BroadcastNotificationCreated::class, function ($event) {
$channels = implode(', ', $event->broadcastOn());

Log::debug('Broadcasting Notification ' . $event->broadcastType() . 'on channel(s) ' . $channels);
});

// Fire job when task is assigned to a user
Facades\Event::listen(ActivityAssigned::class, function ($event) {
Event::listen(ActivityAssigned::class, function ($event) {
$task_id = $event->getProcessRequestToken()->id;
// Dispatch the SmartInbox job with the processRequestToken as parameter
SmartInbox::dispatch($task_id);
});

Facades\Event::listen(MadeTenantCurrentEvent::class, function ($event) {
Event::listen(MadeTenantCurrentEvent::class, function ($event) {
event(new TenantResolved($event->tenant));
});

Facades\Event::listen(TenantNotFoundForRequestEvent::class, function ($event) {
Event::listen(TenantNotFoundForRequestEvent::class, function ($event) {
if (config('app.multitenancy') === false || self::actuallyRunningInConsole()) {
// This is expected if multitenancy is disabled.
// We also need to check if we are running in a console command because
Expand All @@ -327,7 +333,7 @@ protected static function registerEvents(): void
}
});

Facades\Event::listen(function (CommandStarting $event) {
Event::listen(function (CommandStarting $event) {
if ($event->command === 'l5-swagger:generate') {
// Set the analyser to use the legacy DocBlockAnnotationFactory. This must
// be set here because this config value is not serializable and cannot be cached.
Expand Down Expand Up @@ -573,6 +579,23 @@ public static function getPackageBootTiming(): array
return self::$packageBootTiming;
}

/**
* Reset per-request static state between Octane requests.
*
* Octane workers stay alive across requests, so static properties must be
* cleared to avoid leaking data from one request into the next. Singletons
* holding mutable state are handled by the 'flush' list in config/octane.php,
* which Octane applies on its own.
*/
private function registerOctaneListeners(): void
{
if (!class_exists(RequestTerminated::class)) {
return;
}

Event::listen(RequestTerminated::class, ResetRequestState::class);
}

/**
* Find the tenant based on the environment variable
*/
Expand Down
34 changes: 34 additions & 0 deletions config/octane.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Octane Flush & Warm Lists
|--------------------------------------------------------------------------
|
| 'flush' — Services with mutable state that must be recreated per request.
| These singletons will be flushed (re-bound) on each request
| automatically while the application runs under Octane.
|
| 'warm' — Services to pre-resolve once when an Octane worker starts,
| avoiding lazy-resolution overhead on the first request.
|
*/

'flush' => [
// Services with mutable state that must be recreated per request
ProcessMaker\Models\AnonymousUser::class,
ProcessMaker\ImportExport\Extension::class,
ProcessMaker\ImportExport\SignalHelper::class,
ProcessMaker\Managers\MenuManager::class,
],

'warm' => [
...Laravel\Octane\Octane::defaultServicesToWarm(),

// Services to pre-resolve on worker start
ProcessMaker\Managers\PackageManager::class,
ProcessMaker\Managers\LoginManager::class,
ProcessMaker\Managers\IndexManager::class,
],
];
74 changes: 74 additions & 0 deletions tests/unit/ProcessMaker/Octane/ResetRequestStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\ProcessMaker\Octane;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Laravel\Octane\Events\RequestTerminated;
use ProcessMaker\Events\RedirectToEvent;
use ProcessMaker\Listeners\HandleRedirectListener;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Octane\ResetRequestState;
use ProcessMaker\Providers\ProcessMakerServiceProvider;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;

class ResetRequestStateTest extends TestCase
{
public function test_it_clears_request_timing_before_the_next_request(): void
{
DB::select('SELECT 1');

$this->assertGreaterThan(0, ProcessMakerServiceProvider::getQueryTime());

$listener = new ResetRequestState();
$listener->handle();

$this->assertSame(0.0, ProcessMakerServiceProvider::getQueryTime());
}

public function test_it_prevents_redirect_state_from_leaking_into_the_next_request(): void
{
Event::fake([RedirectToEvent::class]);

$redirectListener = new RedirectStateProbe();
$redirectListener->queue(ProcessRequest::factory()->create());

$listener = new ResetRequestState();
$listener->handle();

HandleRedirectListener::sendRedirectToEvent();

Event::assertNotDispatched(RedirectToEvent::class);
}

public function test_octane_request_termination_automatically_resets_request_state(): void
{
Event::fake([RedirectToEvent::class]);

$redirectListener = new RedirectStateProbe();
$redirectListener->queue(ProcessRequest::factory()->create());

event(new RequestTerminated(
$this->app,
$this->app,
Request::create('/first-request'),
new Response()
));

HandleRedirectListener::sendRedirectToEvent();

Event::assertNotDispatched(RedirectToEvent::class);
}
}

final class RedirectStateProbe extends HandleRedirectListener
{
public function queue(ProcessRequest $processRequest): void
{
$this->setRedirectTo($processRequest, 'processUpdated');
}
}
Loading