jdecool/scaleway-runtimepublic Fork
⑂ de59a71
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

fix: keep the Function Response when Kernel termination failsUnverified

Behind FPM the client already received the Response when terminate() runs,
so a failing kernel.terminate listener is now logged to stderr instead of
turning a successful Invocation into a 500 that clients may retry.
Jérémy DECOOL committed 2026-11-13T22:05:49+01:00 Browse files
de59a71 parent: 581465d
modified src/Handler.php +1 -1
@@ -57,7 +57,7 @@ final class Handler
5757 {
5858 try {
5959 $this->runtime ??= new ScalewayRuntime($this->runtimeOptions());
60- $this->runner ??= $this->runtime->createInvocationRunner();
60+ $this->runner ??= $this->runtime->createInvocationRunner($this->errorLog);
6161
6262 return $this->runner->invoke($event, $context);
6363 } catch (\Throwable $e) {
@@ -57,7 +57,7 @@ final class Handler
57 {57 {
58 try {58 try {
59 $this->runtime ??= new ScalewayRuntime($this->runtimeOptions());59 $this->runtime ??= new ScalewayRuntime($this->runtimeOptions());
60- $this->runner ??= $this->runtime->createInvocationRunner();60+ $this->runner ??= $this->runtime->createInvocationRunner($this->errorLog);
61 61
62 return $this->runner->invoke($event, $context);62 return $this->runner->invoke($event, $context);
63 } catch (\Throwable $e) {63 } catch (\Throwable $e) {
modified src/Runner/HttpKernelRunner.php +13 -4
@@ -12,8 +12,12 @@ use Symfony\Component\HttpKernel\TerminableInterface;
1212
1313 final readonly class HttpKernelRunner implements InvocationRunner
1414 {
15+ /**
16+ * @param resource $errorLog
17+ */
1518 public function __construct(
1619 private HttpKernelInterface $kernel,
20+ private mixed $errorLog,
1721 ) {
1822 // Booting here makes a broken container a boot failure, so the Handler retries with a fresh Application.
1923 if ($kernel instanceof KernelInterface) {
@@ -31,13 +35,18 @@ final readonly class HttpKernelRunner implements InvocationRunner
3135 $request = HttpConverter::toRequest($event);
3236 // The Kernel resets its services itself (services_resetter) when the next main request is handled.
3337 $response = $this->kernel->handle($request);
38+ $functionResponse = HttpConverter::toFunctionResponse($response);
3439
35- try {
36- return HttpConverter::toFunctionResponse($response);
37- } finally {
38- if ($this->kernel instanceof TerminableInterface) {
40+ if ($this->kernel instanceof TerminableInterface) {
41+ try {
3942 $this->kernel->terminate($request, $response);
43+ } catch (\Throwable $e) {
44+ // Behind FPM the client already got the Response when terminate() runs; turning it into
45+ // a 500 would invite retries of an operation that succeeded.
46+ fwrite($this->errorLog, 'Kernel termination failed: '.$e.\PHP_EOL);
4047 }
4148 }
49+
50+ return $functionResponse;
4251 }
4352 }
@@ -12,8 +12,12 @@ use Symfony\Component\HttpKernel\TerminableInterface;
12 12
13 final readonly class HttpKernelRunner implements InvocationRunner13 final readonly class HttpKernelRunner implements InvocationRunner
14 {14 {
15+ /**
16+ * @param resource $errorLog
17+ */
15 public function __construct(18 public function __construct(
16 private HttpKernelInterface $kernel,19 private HttpKernelInterface $kernel,
20+ private mixed $errorLog,
17 ) {21 ) {
18 // Booting here makes a broken container a boot failure, so the Handler retries with a fresh Application.22 // Booting here makes a broken container a boot failure, so the Handler retries with a fresh Application.
19 if ($kernel instanceof KernelInterface) {23 if ($kernel instanceof KernelInterface) {
@@ -31,13 +35,18 @@ final readonly class HttpKernelRunner implements InvocationRunner
31 $request = HttpConverter::toRequest($event);35 $request = HttpConverter::toRequest($event);
32 // The Kernel resets its services itself (services_resetter) when the next main request is handled.36 // The Kernel resets its services itself (services_resetter) when the next main request is handled.
33 $response = $this->kernel->handle($request);37 $response = $this->kernel->handle($request);
38+ $functionResponse = HttpConverter::toFunctionResponse($response);
34 39
35- try {40+ if ($this->kernel instanceof TerminableInterface) {
36- return HttpConverter::toFunctionResponse($response);41+ try {
37- } finally {
38- if ($this->kernel instanceof TerminableInterface) {
39 $this->kernel->terminate($request, $response);42 $this->kernel->terminate($request, $response);
43+ } catch (\Throwable $e) {
44+ // Behind FPM the client already got the Response when terminate() runs; turning it into
45+ // a 500 would invite retries of an operation that succeeded.
46+ fwrite($this->errorLog, 'Kernel termination failed: '.$e.\PHP_EOL);
40 }47 }
41 }48 }
49+
50+ return $functionResponse;
42 }51 }
43 }52 }
modified src/ScalewayRuntime.php +4 -2
@@ -39,8 +39,10 @@ class ScalewayRuntime extends SymfonyRuntime
3939
4040 /**
4141 * Builds the Application from the Front Controller and picks the runner matching its type.
42+ *
43+ * @param resource $errorLog where failures that must not change the Function Response are reported
4244 */
43- public function createInvocationRunner(): InvocationRunner
45+ public function createInvocationRunner(mixed $errorLog): InvocationRunner
4446 {
4547 $projectDir = $this->options['project_dir'] ?? null;
4648 $frontController = $this->options['front_controller'] ?? 'public/index.php';
@@ -59,7 +61,7 @@ class ScalewayRuntime extends SymfonyRuntime
5961 $application = $app(...$args);
6062
6163 return match (true) {
62- $application instanceof HttpKernelInterface => new HttpKernelRunner($application),
64+ $application instanceof HttpKernelInterface => new HttpKernelRunner($application, $errorLog),
6365 default => throw new \LogicException(\sprintf('"%s" cannot run Applications of type "%s".', static::class, get_debug_type($application))),
6466 };
6567 }
@@ -39,8 +39,10 @@ class ScalewayRuntime extends SymfonyRuntime
39 39
40 /**40 /**
41 * Builds the Application from the Front Controller and picks the runner matching its type.41 * Builds the Application from the Front Controller and picks the runner matching its type.
42+ *
43+ * @param resource $errorLog where failures that must not change the Function Response are reported
42 */44 */
43- public function createInvocationRunner(): InvocationRunner45+ public function createInvocationRunner(mixed $errorLog): InvocationRunner
44 {46 {
45 $projectDir = $this->options['project_dir'] ?? null;47 $projectDir = $this->options['project_dir'] ?? null;
46 $frontController = $this->options['front_controller'] ?? 'public/index.php';48 $frontController = $this->options['front_controller'] ?? 'public/index.php';
@@ -59,7 +61,7 @@ class ScalewayRuntime extends SymfonyRuntime
59 $application = $app(...$args);61 $application = $app(...$args);
60 62
61 return match (true) {63 return match (true) {
62- $application instanceof HttpKernelInterface => new HttpKernelRunner($application),64+ $application instanceof HttpKernelInterface => new HttpKernelRunner($application, $errorLog),
63 default => throw new \LogicException(\sprintf('"%s" cannot run Applications of type "%s".', static::class, get_debug_type($application))),65 default => throw new \LogicException(\sprintf('"%s" cannot run Applications of type "%s".', static::class, get_debug_type($application))),
64 };66 };
65 }67 }
modified tests/Fixtures/RecordingKernel.php +3 -0
@@ -48,5 +48,8 @@ final class RecordingKernel implements HttpKernelInterface, TerminableInterface
4848 public function terminate(Request $request, Response $response): void
4949 {
5050 ++$this->terminated;
51+ if ('/terminate-fails' === $request->getPathInfo()) {
52+ throw new \LogicException('Termination failed on purpose.');
53+ }
5154 }
5255 }
@@ -48,5 +48,8 @@ final class RecordingKernel implements HttpKernelInterface, TerminableInterface
48 public function terminate(Request $request, Response $response): void48 public function terminate(Request $request, Response $response): void
49 {49 {
50 ++$this->terminated;50 ++$this->terminated;
51+ if ('/terminate-fails' === $request->getPathInfo()) {
52+ throw new \LogicException('Termination failed on purpose.');
53+ }
51 }54 }
52 }55 }
modified tests/Runner/HttpKernelRunnerTest.php +23 -3
@@ -12,6 +12,16 @@ use Symfony\Component\HttpFoundation\Request;
1212
1313 final class HttpKernelRunnerTest extends TestCase
1414 {
15+ /** @var resource */
16+ private $errorLog;
17+
18+ protected function setUp(): void
19+ {
20+ $errorLog = fopen('php://memory', 'w+');
21+ self::assertNotFalse($errorLog);
22+ $this->errorLog = $errorLog;
23+ }
24+
1525 protected function tearDown(): void
1626 {
1727 Request::setTrustedProxies([], 0);
@@ -19,7 +29,7 @@ final class HttpKernelRunnerTest extends TestCase
1929
2030 public function testPlatformProxyIsTrustedByDefault(): void
2131 {
22- new HttpKernelRunner(new RecordingKernel());
32+ new HttpKernelRunner(new RecordingKernel(), $this->errorLog);
2333
2434 self::assertSame([HttpConverter::PROXY_ADDRESS], Request::getTrustedProxies());
2535 self::assertSame(HttpConverter::TRUSTED_HEADERS, Request::getTrustedHeaderSet());
@@ -29,7 +39,7 @@ final class HttpKernelRunnerTest extends TestCase
2939 {
3040 Request::setTrustedProxies(['10.0.0.0/8'], Request::HEADER_X_FORWARDED_FOR);
3141
32- new HttpKernelRunner(new RecordingKernel());
42+ new HttpKernelRunner(new RecordingKernel(), $this->errorLog);
3343
3444 self::assertSame(['10.0.0.0/8'], Request::getTrustedProxies());
3545 self::assertSame(Request::HEADER_X_FORWARDED_FOR, Request::getTrustedHeaderSet());
@@ -38,7 +48,7 @@ final class HttpKernelRunnerTest extends TestCase
3848 public function testKernelIsTerminatedAfterEachInvocation(): void
3949 {
4050 $kernel = new RecordingKernel();
41- $runner = new HttpKernelRunner($kernel);
51+ $runner = new HttpKernelRunner($kernel, $this->errorLog);
4252
4353 $runner->invoke(['path' => '/'], []);
4454 $functionResponse = $runner->invoke(['path' => '/'], []);
@@ -46,4 +56,14 @@ final class HttpKernelRunnerTest extends TestCase
4656 self::assertSame(['statusCode' => 200, 'body' => 'handled #2 /'], array_intersect_key($functionResponse, ['statusCode' => 0, 'body' => 0]));
4757 self::assertSame(2, $kernel->terminated);
4858 }
59+
60+ public function testTerminationFailureKeepsTheResponseAndIsLogged(): void
61+ {
62+ $functionResponse = new HttpKernelRunner(new RecordingKernel(), $this->errorLog)->invoke(['path' => '/terminate-fails'], []);
63+
64+ self::assertSame(200, $functionResponse['statusCode']);
65+ self::assertSame('handled #1 /terminate-fails', $functionResponse['body']);
66+ rewind($this->errorLog);
67+ self::assertStringContainsString('Termination failed on purpose.', (string) stream_get_contents($this->errorLog));
68+ }
4969 }
@@ -12,6 +12,16 @@ use Symfony\Component\HttpFoundation\Request;
12 12
13 final class HttpKernelRunnerTest extends TestCase13 final class HttpKernelRunnerTest extends TestCase
14 {14 {
15+ /** @var resource */
16+ private $errorLog;
17+
18+ protected function setUp(): void
19+ {
20+ $errorLog = fopen('php://memory', 'w+');
21+ self::assertNotFalse($errorLog);
22+ $this->errorLog = $errorLog;
23+ }
24+
15 protected function tearDown(): void25 protected function tearDown(): void
16 {26 {
17 Request::setTrustedProxies([], 0);27 Request::setTrustedProxies([], 0);
@@ -19,7 +29,7 @@ final class HttpKernelRunnerTest extends TestCase
19 29
20 public function testPlatformProxyIsTrustedByDefault(): void30 public function testPlatformProxyIsTrustedByDefault(): void
21 {31 {
22- new HttpKernelRunner(new RecordingKernel());32+ new HttpKernelRunner(new RecordingKernel(), $this->errorLog);
23 33
24 self::assertSame([HttpConverter::PROXY_ADDRESS], Request::getTrustedProxies());34 self::assertSame([HttpConverter::PROXY_ADDRESS], Request::getTrustedProxies());
25 self::assertSame(HttpConverter::TRUSTED_HEADERS, Request::getTrustedHeaderSet());35 self::assertSame(HttpConverter::TRUSTED_HEADERS, Request::getTrustedHeaderSet());
@@ -29,7 +39,7 @@ final class HttpKernelRunnerTest extends TestCase
29 {39 {
30 Request::setTrustedProxies(['10.0.0.0/8'], Request::HEADER_X_FORWARDED_FOR);40 Request::setTrustedProxies(['10.0.0.0/8'], Request::HEADER_X_FORWARDED_FOR);
31 41
32- new HttpKernelRunner(new RecordingKernel());42+ new HttpKernelRunner(new RecordingKernel(), $this->errorLog);
33 43
34 self::assertSame(['10.0.0.0/8'], Request::getTrustedProxies());44 self::assertSame(['10.0.0.0/8'], Request::getTrustedProxies());
35 self::assertSame(Request::HEADER_X_FORWARDED_FOR, Request::getTrustedHeaderSet());45 self::assertSame(Request::HEADER_X_FORWARDED_FOR, Request::getTrustedHeaderSet());
@@ -38,7 +48,7 @@ final class HttpKernelRunnerTest extends TestCase
38 public function testKernelIsTerminatedAfterEachInvocation(): void48 public function testKernelIsTerminatedAfterEachInvocation(): void
39 {49 {
40 $kernel = new RecordingKernel();50 $kernel = new RecordingKernel();
41- $runner = new HttpKernelRunner($kernel);51+ $runner = new HttpKernelRunner($kernel, $this->errorLog);
42 52
43 $runner->invoke(['path' => '/'], []);53 $runner->invoke(['path' => '/'], []);
44 $functionResponse = $runner->invoke(['path' => '/'], []);54 $functionResponse = $runner->invoke(['path' => '/'], []);
@@ -46,4 +56,14 @@ final class HttpKernelRunnerTest extends TestCase
46 self::assertSame(['statusCode' => 200, 'body' => 'handled #2 /'], array_intersect_key($functionResponse, ['statusCode' => 0, 'body' => 0]));56 self::assertSame(['statusCode' => 200, 'body' => 'handled #2 /'], array_intersect_key($functionResponse, ['statusCode' => 0, 'body' => 0]));
47 self::assertSame(2, $kernel->terminated);57 self::assertSame(2, $kernel->terminated);
48 }58 }
59+
60+ public function testTerminationFailureKeepsTheResponseAndIsLogged(): void
61+ {
62+ $functionResponse = new HttpKernelRunner(new RecordingKernel(), $this->errorLog)->invoke(['path' => '/terminate-fails'], []);
63+
64+ self::assertSame(200, $functionResponse['statusCode']);
65+ self::assertSame('handled #1 /terminate-fails', $functionResponse['body']);
66+ rewind($this->errorLog);
67+ self::assertStringContainsString('Termination failed on purpose.', (string) stream_get_contents($this->errorLog));
68+ }
49 }69 }