Crash when using print statement inside closure in Concurrency Facade #57084
-
Laravel Version12.28.1 PHP Version8.4.5 Database Driver & VersionNo response DescriptionWhen using Laravel’s Concurrency::run() feature, the process crashes with Trying to access array offset on null in ProcessDriver.php if a closure contains echo or print. This happens because child processes communicate back to the parent via JSON, and printing to STDOUT corrupts the expected JSON payload, causing $result to become null. Instead of crashing, Laravel should either gracefully handle extra output, redirect it, or clearly document that closures should not use direct output and should return values or use logging instead. Please take a look at this critical architectural flaw. Steps To Reproduce`<?php use Illuminate\Support\Facades\Concurrency; require "vendor/autoload.php"; $start1 = microtime(true); `C:\Laravel\test-herd>php test.php ErrorException Trying to access array offset on null at vendor\laravel\framework\src\Illuminate\Concurrency\ProcessDriver.php:51 1 vendor\laravel\framework\src\Illuminate\Concurrency\ProcessDriver.php:51 2 vendor\laravel\framework\src\Illuminate\Collections\Arr.php:803 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You’ve actually uncovered a real design limitation in Laravel 12’s new concurrency API. Right now, Concurrency::run() relies on ProcessDriver, which launches child PHP processes and communicates back to the parent using JSON payloads over STDOUT. Why it crashes Your closures call echo (or print), which writes directly to STDOUT. Since STDOUT is also being used by Laravel to pass structured JSON-encoded results, your raw text corrupts the JSON. When Laravel tries json_decode($result->output(), true), it fails → $result becomes null. That’s why you get: Trying to access array offset on null in ProcessDriver.php Workarounds you can use now Don’t use echo or print inside concurrent tasks Concurrency::run([ Or: use Illuminate\Support\Facades\Log; Concurrency::run([ Capture output manually Concurrency::run([ This way, the closure returns the string instead of contaminating STDOUT. What Laravel should ideally do Document this clearly (like “closures must not write to STDOUT, use return/logging instead”). Or sandbox STDOUT so any echo/print goes to a buffer and doesn’t corrupt JSON. Or gracefully handle invalid JSON, returning a descriptive error instead of crashing with “Trying to access array offset on null”. |
Beta Was this translation helpful? Give feedback.
You’ve actually uncovered a real design limitation in Laravel 12’s new concurrency API.
Right now, Concurrency::run() relies on ProcessDriver, which launches child PHP processes and communicates back to the parent using JSON payloads over STDOUT.
Why it crashes
Your closures call echo (or print), which writes directly to STDOUT.
Since STDOUT is also being used by Laravel to pass structured JSON-encoded results, your raw text corrupts the JSON.
When Laravel tries json_decode($result->output(), true), it fails → $result becomes null.
That’s why you get:
Trying to access array offset on null in ProcessDriver.php
Workarounds you can use now
Don’t use echo or print inside concurrent tasks
Inst…