jdecool/scaleway-runtimepublic Fork
⑂ 955033d
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: serve bin/console as a Front Controller without resetting an unbooted KernelUnverified

FrameworkBundle's Console Application cannot reset the services of a Kernel
that is not booted yet, which failed the first Command Invocation of such a
Front Controller. Services are now reset before a command only after HTTP
Invocations, and a failed reset after it is logged without replacing the
command's Function Response.
Jérémy DECOOL committed 2026-11-13T23:03:26+01:00 Browse files
955033d parent: ff07ec9
modified src/Runner/ConsoleRunner.php +13 -3
@@ -56,12 +56,22 @@ final class ConsoleRunner implements InvocationRunner
5656 $input->setInteractive(false);
5757 $output = new BufferedOutput(decorated: false);
5858
59- // The Kernel resets its services before handling a request, not before a command, nor after one.
60- $this->console->reset();
59+ // The Kernel resets its services before handling a request, not before a command: the previous HTTP Invocation
60+ // left them dirty. Only a runner serving HTTP has had such Invocations, and its Kernel is booted already.
61+ if (null !== $this->httpRunner) {
62+ $this->console->reset();
63+ }
6164 try {
6265 $exitCode = $this->console->run($input, $output);
6366 } finally {
64- $this->console->reset();
67+ // Nor after a command, so the next Invocation would inherit its state.
68+ try {
69+ $this->console->reset();
70+ } catch (\Throwable $e) {
71+ // A Kernel that failed to boot has nothing to reset, and its boot error is in the output already;
72+ // like a failed Kernel termination, it must not replace the Function Response.
73+ fwrite($this->errorLog, 'Service reset failed: '.$e.\PHP_EOL);
74+ }
6575 }
6676
6777 $body = $output->fetch();
@@ -56,12 +56,22 @@ final class ConsoleRunner implements InvocationRunner
56 $input->setInteractive(false);56 $input->setInteractive(false);
57 $output = new BufferedOutput(decorated: false);57 $output = new BufferedOutput(decorated: false);
58 58
59- // The Kernel resets its services before handling a request, not before a command, nor after one.59+ // The Kernel resets its services before handling a request, not before a command: the previous HTTP Invocation
60- $this->console->reset();60+ // left them dirty. Only a runner serving HTTP has had such Invocations, and its Kernel is booted already.
61+ if (null !== $this->httpRunner) {
62+ $this->console->reset();
63+ }
61 try {64 try {
62 $exitCode = $this->console->run($input, $output);65 $exitCode = $this->console->run($input, $output);
63 } finally {66 } finally {
64- $this->console->reset();67+ // Nor after a command, so the next Invocation would inherit its state.
68+ try {
69+ $this->console->reset();
70+ } catch (\Throwable $e) {
71+ // A Kernel that failed to boot has nothing to reset, and its boot error is in the output already;
72+ // like a failed Kernel termination, it must not replace the Function Response.
73+ fwrite($this->errorLog, 'Service reset failed: '.$e.\PHP_EOL);
74+ }
65 }75 }
66 76
67 $body = $output->fetch();77 $body = $output->fetch();
added tests/Fixtures/App/bin/console.php +15 -0
new file mode 100644
@@ -0,0 +1,15 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+use JDecool\ScalewayRuntime\Tests\Fixtures\App\Kernel;
6+use Symfony\Bundle\FrameworkBundle\Console\Application;
7+
8+require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
9+
10+// The bin/console of a Symfony application, as a Front Controller.
11+return static function (array $context): Application {
12+ assert(is_string($context['APP_ENV']));
13+
14+ return new Application(new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']));
15+};
new file mode 100644
@@ -0,0 +1,15 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+use JDecool\ScalewayRuntime\Tests\Fixtures\App\Kernel;
6+use Symfony\Bundle\FrameworkBundle\Console\Application;
7+
8+require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
9+
10+// The bin/console of a Symfony application, as a Front Controller.
11+return static function (array $context): Application {
12+ assert(is_string($context['APP_ENV']));
13+
14+ return new Application(new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']));
15+};
added tests/Fixtures/front-controllers/console-reset-fails.php +20 -0
new file mode 100644
@@ -0,0 +1,20 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+use Symfony\Component\Console\Application;
6+use Symfony\Component\Console\Command\Command;
7+
8+return static function (): Application {
9+ $application = new class extends Application {
10+ public function reset(): void
11+ {
12+ throw new LogicException('Reset failed on purpose.');
13+ }
14+ };
15+ $command = new Command('app:ok');
16+ $command->setCode(static fn (): int => Command::SUCCESS);
17+ $application->addCommand($command);
18+
19+ return $application;
20+};
new file mode 100644
@@ -0,0 +1,20 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+use Symfony\Component\Console\Application;
6+use Symfony\Component\Console\Command\Command;
7+
8+return static function (): Application {
9+ $application = new class extends Application {
10+ public function reset(): void
11+ {
12+ throw new LogicException('Reset failed on purpose.');
13+ }
14+ };
15+ $command = new Command('app:ok');
16+ $command->setCode(static fn (): int => Command::SUCCESS);
17+ $application->addCommand($command);
18+
19+ return $application;
20+};
modified tests/HandlerTest.php +8 -0
@@ -210,6 +210,14 @@ final class HandlerTest extends TestCase
210210 self::assertStringContainsString('Command Invocation rejected', $this->errorLog());
211211 }
212212
213+ public function testFailedResetAfterACommandKeepsItsResponseAndIsLogged(): void
214+ {
215+ $response = $this->handler(['front_controller' => 'front-controllers/console-reset-fails.php'])->invoke($this->commandEvent('app:ok'), []);
216+
217+ self::assertSame(200, $response['statusCode']);
218+ self::assertStringContainsString('Service reset failed: LogicException: Reset failed on purpose.', $this->errorLog());
219+ }
220+
213221 public function testCommandFrontControllerRunsItsCommandWhateverTheName(): void
214222 {
215223 $handler = $this->handler(['front_controller' => 'front-controllers/command.php']);
@@ -210,6 +210,14 @@ final class HandlerTest extends TestCase
210 self::assertStringContainsString('Command Invocation rejected', $this->errorLog());210 self::assertStringContainsString('Command Invocation rejected', $this->errorLog());
211 }211 }
212 212
213+ public function testFailedResetAfterACommandKeepsItsResponseAndIsLogged(): void
214+ {
215+ $response = $this->handler(['front_controller' => 'front-controllers/console-reset-fails.php'])->invoke($this->commandEvent('app:ok'), []);
216+
217+ self::assertSame(200, $response['statusCode']);
218+ self::assertStringContainsString('Service reset failed: LogicException: Reset failed on purpose.', $this->errorLog());
219+ }
220+
213 public function testCommandFrontControllerRunsItsCommandWhateverTheName(): void221 public function testCommandFrontControllerRunsItsCommandWhateverTheName(): void
214 {222 {
215 $handler = $this->handler(['front_controller' => 'front-controllers/command.php']);223 $handler = $this->handler(['front_controller' => 'front-controllers/command.php']);
modified tests/IntegrationTest.php +16 -0
@@ -186,6 +186,22 @@ final class IntegrationTest extends TestCase
186186 self::assertSame(404, $forged['statusCode'], 'A body with a wrong secret is a plain HTTP POST.');
187187 }
188188
189+ public function testConsoleFrontControllerOfAKernelRunsCommandsOnly(): void
190+ {
191+ [$first, $second, $http] = $this->invoke([
192+ ['method' => 'POST', 'body' => '{"command":"app:count","args":{"--times":"3"},"secret":"cron-secret"}'],
193+ ['method' => 'POST', 'body' => '{"command":"app:count","secret":"cron-secret"}'],
194+ ['path' => '/state'],
195+ ], ['front_controller' => 'bin/console.php']);
196+
197+ self::assertSame(200, $first['statusCode']);
198+ self::assertIsString($first['body']);
199+ self::assertStringStartsWith('count=3 kernelInstances=1 ', $first['body']);
200+ self::assertIsString($second['body']);
201+ self::assertStringStartsWith('count=1 kernelInstances=1 ', $second['body'], 'Services must be reset after each command.');
202+ self::assertSame(403, $http['statusCode']);
203+ }
204+
189205 /**
190206 * @param list<array<string, mixed>> $events
191207 * @param array<string, mixed> $options runtime options on top of "project_dir"
@@ -186,6 +186,22 @@ final class IntegrationTest extends TestCase
186 self::assertSame(404, $forged['statusCode'], 'A body with a wrong secret is a plain HTTP POST.');186 self::assertSame(404, $forged['statusCode'], 'A body with a wrong secret is a plain HTTP POST.');
187 }187 }
188 188
189+ public function testConsoleFrontControllerOfAKernelRunsCommandsOnly(): void
190+ {
191+ [$first, $second, $http] = $this->invoke([
192+ ['method' => 'POST', 'body' => '{"command":"app:count","args":{"--times":"3"},"secret":"cron-secret"}'],
193+ ['method' => 'POST', 'body' => '{"command":"app:count","secret":"cron-secret"}'],
194+ ['path' => '/state'],
195+ ], ['front_controller' => 'bin/console.php']);
196+
197+ self::assertSame(200, $first['statusCode']);
198+ self::assertIsString($first['body']);
199+ self::assertStringStartsWith('count=3 kernelInstances=1 ', $first['body']);
200+ self::assertIsString($second['body']);
201+ self::assertStringStartsWith('count=1 kernelInstances=1 ', $second['body'], 'Services must be reset after each command.');
202+ self::assertSame(403, $http['statusCode']);
203+ }
204+
189 /**205 /**
190 * @param list<array<string, mixed>> $events206 * @param list<array<string, mixed>> $events
191 * @param array<string, mixed> $options runtime options on top of "project_dir"207 * @param array<string, mixed> $options runtime options on top of "project_dir"