Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/Process/ParallelProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public function getErrorOutput()
if (! $this->errorOutput) {
$processOutput = $this->process->getErrorOutput();

$marker = '___SPATIE_ASYNC_CHILD___';
$markerPosition = strrpos($processOutput, $marker);

if ($markerPosition !== false) {
$processOutput = substr($processOutput, $markerPosition + strlen($marker));
}

$childResult = @unserialize(base64_decode($processOutput));

if ($childResult === false || ! array_key_exists('output', $childResult)) {
Expand Down
4 changes: 3 additions & 1 deletion src/Process/ProcessCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public function triggerError()

call_user_func_array($callback, [$exception]);

break;
return;
}

throw $exception;
}

abstract protected function resolveErrorOutput(): Throwable;
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/ChildRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

$output = new \Spatie\Async\Output\SerializableException($exception);

fwrite(STDERR, base64_encode(serialize(['output' => $output])));
fwrite(STDERR, '___SPATIE_ASYNC_CHILD___'.base64_encode(serialize(['output' => $output])));

exit(1);
}
31 changes: 31 additions & 0 deletions tests/Feature/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,37 @@
expect($caughtError)->toBeInstanceOf(ParseError::class);
});

it('preserves exception type when stderr contains noise', function () {
$pool = Pool::create();

$caughtException = null;

$pool->add(childTask(function () {
trigger_error('some deprecation notice', E_USER_WARNING);

throw new MyException('test');
}))->catch(function (MyException $e) use (&$caughtException) {
$caughtException = $e;
});

$pool->wait();

expect($caughtException)->toBeInstanceOf(MyException::class);
expect($caughtException->getMessage())->toContain('test');
});

it('throws exception when no catch handler matches', function () {
$pool = Pool::create();

$pool->add(childTask(function () {
throw new MyException('test');
}))->catch(function (OtherException $e) {
// This handler should not match
});

$pool->wait();
})->throws(MyException::class);

it('can handle synchronous exception', function () {
Pool::$forceSynchronous = true;

Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@
$cntTasks = 30;

foreach (range(1, $cntTasks) as $i) {
$pool->add(childTask(function () { return 1; }));
$pool->add(childTask(function () {
return 1;
}));
}

$pool->wait();
Expand Down