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.
de59a71 parent: 581465d modified
src/Handler.php +1 -1 | @@ -57,7 +57,7 @@ final class Handler | ||
| 57 | 57 | { |
| 58 | 58 | try { |
| 59 | 59 | $this->runtime ??= new ScalewayRuntime($this->runtimeOptions()); |
| 60 | - $this->runner ??= $this->runtime->createInvocationRunner(); | |
| 60 | + $this->runner ??= $this->runtime->createInvocationRunner($this->errorLog); | |
| 61 | 61 | |
| 62 | 62 | return $this->runner->invoke($event, $context); |
| 63 | 63 | } 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; | ||
| 12 | 12 | |
| 13 | 13 | final readonly class HttpKernelRunner implements InvocationRunner |
| 14 | 14 | { |
| 15 | + /** | |
| 16 | + * @param resource $errorLog | |
| 17 | + */ | |
| 15 | 18 | public function __construct( |
| 16 | 19 | private HttpKernelInterface $kernel, |
| 20 | + private mixed $errorLog, | |
| 17 | 21 | ) { |
| 18 | 22 | // Booting here makes a broken container a boot failure, so the Handler retries with a fresh Application. |
| 19 | 23 | if ($kernel instanceof KernelInterface) { |
| @@ -31,13 +35,18 @@ final readonly class HttpKernelRunner implements InvocationRunner | ||
| 31 | 35 | $request = HttpConverter::toRequest($event); |
| 32 | 36 | // The Kernel resets its services itself (services_resetter) when the next main request is handled. |
| 33 | 37 | $response = $this->kernel->handle($request); |
| 38 | + $functionResponse = HttpConverter::toFunctionResponse($response); | |
| 34 | 39 | |
| 35 | - try { | |
| 36 | - return HttpConverter::toFunctionResponse($response); | |
| 37 | - } finally { | |
| 38 | - if ($this->kernel instanceof TerminableInterface) { | |
| 40 | + if ($this->kernel instanceof TerminableInterface) { | |
| 41 | + try { | |
| 39 | 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 | } |
| @@ -12,8 +12,12 @@ use Symfony\Component\HttpKernel\TerminableInterface; | |||
| 12 | 12 | ||
| 13 | final readonly class HttpKernelRunner implements InvocationRunner | 13 | 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 | ||
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 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(): InvocationRunner | |
| 45 | + public function createInvocationRunner(mixed $errorLog): InvocationRunner | |
| 44 | 46 | { |
| 45 | 47 | $projectDir = $this->options['project_dir'] ?? null; |
| 46 | 48 | $frontController = $this->options['front_controller'] ?? 'public/index.php'; |
| @@ -59,7 +61,7 @@ class ScalewayRuntime extends SymfonyRuntime | ||
| 59 | 61 | $application = $app(...$args); |
| 60 | 62 | |
| 61 | 63 | return match (true) { |
| 62 | - $application instanceof HttpKernelInterface => new HttpKernelRunner($application), | |
| 64 | + $application instanceof HttpKernelInterface => new HttpKernelRunner($application, $errorLog), | |
| 63 | 65 | default => throw new \LogicException(\sprintf('"%s" cannot run Applications of type "%s".', static::class, get_debug_type($application))), |
| 64 | 66 | }; |
| 65 | 67 | } |
| @@ -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(): InvocationRunner | 45 | + 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 | ||
| 48 | 48 | public function terminate(Request $request, Response $response): void |
| 49 | 49 | { |
| 50 | 50 | ++$this->terminated; |
| 51 | + if ('/terminate-fails' === $request->getPathInfo()) { | |
| 52 | + throw new \LogicException('Termination failed on purpose.'); | |
| 53 | + } | |
| 51 | 54 | } |
| 52 | 55 | } |
| @@ -48,5 +48,8 @@ final class RecordingKernel implements HttpKernelInterface, TerminableInterface | |||
| 48 | public function terminate(Request $request, Response $response): void | 48 | 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; | ||
| 12 | 12 | |
| 13 | 13 | 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 | 25 | protected function tearDown(): void |
| 16 | 26 | { |
| 17 | 27 | Request::setTrustedProxies([], 0); |
| @@ -19,7 +29,7 @@ final class HttpKernelRunnerTest extends TestCase | ||
| 19 | 29 | |
| 20 | 30 | public function testPlatformProxyIsTrustedByDefault(): void |
| 21 | 31 | { |
| 22 | - new HttpKernelRunner(new RecordingKernel()); | |
| 32 | + new HttpKernelRunner(new RecordingKernel(), $this->errorLog); | |
| 23 | 33 | |
| 24 | 34 | self::assertSame([HttpConverter::PROXY_ADDRESS], Request::getTrustedProxies()); |
| 25 | 35 | self::assertSame(HttpConverter::TRUSTED_HEADERS, Request::getTrustedHeaderSet()); |
| @@ -29,7 +39,7 @@ final class HttpKernelRunnerTest extends TestCase | ||
| 29 | 39 | { |
| 30 | 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 | 44 | self::assertSame(['10.0.0.0/8'], Request::getTrustedProxies()); |
| 35 | 45 | self::assertSame(Request::HEADER_X_FORWARDED_FOR, Request::getTrustedHeaderSet()); |
| @@ -38,7 +48,7 @@ final class HttpKernelRunnerTest extends TestCase | ||
| 38 | 48 | public function testKernelIsTerminatedAfterEachInvocation(): void |
| 39 | 49 | { |
| 40 | 50 | $kernel = new RecordingKernel(); |
| 41 | - $runner = new HttpKernelRunner($kernel); | |
| 51 | + $runner = new HttpKernelRunner($kernel, $this->errorLog); | |
| 42 | 52 | |
| 43 | 53 | $runner->invoke(['path' => '/'], []); |
| 44 | 54 | $functionResponse = $runner->invoke(['path' => '/'], []); |
| @@ -46,4 +56,14 @@ final class HttpKernelRunnerTest extends TestCase | ||
| 46 | 56 | self::assertSame(['statusCode' => 200, 'body' => 'handled #2 /'], array_intersect_key($functionResponse, ['statusCode' => 0, 'body' => 0])); |
| 47 | 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 | } |
| @@ -12,6 +12,16 @@ use Symfony\Component\HttpFoundation\Request; | |||
| 12 | 12 | ||
| 13 | final class HttpKernelRunnerTest extends TestCase | 13 | 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(): void | 25 | 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(): void | 30 | 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(): void | 48 | 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 | } |