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
5 changes: 5 additions & 0 deletions src/FpmRuntime/FpmHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public function handleRequest(HttpRequestEvent $event, Context $context): HttpRe
throw new FastCgiCommunicationFailed;
}

// PHP-FPM sends no output when it fails to start the request, such as running out of memory parsing the body
if ($response->getOutput() === '') {
throw new FastCgiCommunicationFailed('PHP-FPM failed to start the request, the PHP-FPM logs contain the error');
}

$responseHeaders = $this->getResponseHeaders($response);
// The status code is returned separately, it is not an HTTP header
unset($responseHeaders['status']);
Expand Down
28 changes: 28 additions & 0 deletions tests/FpmRuntime/FpmHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bref\Test\FpmRuntime;

use Bref\Context\Context;
use Bref\FpmRuntime\FastCgi\FastCgiCommunicationFailed;
use Bref\FpmRuntime\FastCgi\Timeout;
use Bref\FpmRuntime\FpmHandler;
use Bref\Test\HttpRequestProxyTest;
Expand Down Expand Up @@ -1257,6 +1258,33 @@ public function test worker logs are still written in case of a timeou
}
}

public function test requests that FPM fails to start are reported as errors()
{
$this->fpm = new FpmHandler(__DIR__ . '/fixtures/request.php', __DIR__ . '/fixtures/php-fpm-low-memory.conf');
$this->fpm->start();

try {
$this->fpm->handle([
'version' => '1.0',
'httpMethod' => 'POST',
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => implode('&', array_map(fn (int $i) => "a{$i}[]=1", range(1, 50000))),
], $this->fakeContext);
$this->fail('No exception was thrown');
} catch (FastCgiCommunicationFailed $e) {
self::assertSame('PHP-FPM failed to start the request, the PHP-FPM logs contain the error', $e->getMessage());

// PHP-FPM should work after that
$statusCode = $this->fpm->handle([
'version' => '1.0',
'httpMethod' => 'GET',
], $this->fakeContext)['statusCode'];
self::assertEquals(200, $statusCode);
}
}

/**
* @see https://github.com/brefphp/bref/issues/316
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/FpmRuntime/fixtures/php-fpm-low-memory.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error_log = /dev/null

[default]
pm = static
pm.max_children = 1
listen = /tmp/.bref/php-fpm.sock
; Run out of memory parsing the test's request body, without displaying the error
php_admin_value[memory_limit] = 8M
php_admin_value[max_input_vars] = 100000
php_admin_value[display_errors] = Off
Loading