jdecool/scaleway-runtimepublic Fork
⑂ 01e3678
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: reach the Emulator's Warm Process over a private Unix socketUnverified

A loopback TCP port could be taken between picking and binding it, and let
any local user send Events to the application. A socket in a 0700 temp dir
has neither problem. The worker is renamed Warm Process, per the glossary.
Jérémy DECOOL committed 2026-11-13T22:30:49+01:00 Browse files
01e3678 parent: 0fec100
modified bin/scaleway-serve +26 -20
@@ -20,10 +20,16 @@ if (!is_file($handlerFile)) {
2020 exit(1);
2121 }
2222
23-// shortcut: another process may grab the port before the worker binds it; rerun if the worker fails to listen.
24-$probe = stream_socket_server('tcp://127.0.0.1:0') ?: exit(1);
25-$workerPort = (int) substr((string) strrchr((string) stream_socket_get_name($probe, false), ':'), 1);
26-fclose($probe);
23+// A Unix socket in a private directory: no port to race for, and other local users cannot send Events.
24+$socketDir = sys_get_temp_dir().'/scaleway-serve-'.bin2hex(random_bytes(4));
25+if (!mkdir($socketDir, 0o700)) {
26+ exit(1);
27+}
28+$socket = 'unix://'.$socketDir.'/warm-process.sock';
29+register_shutdown_function(static function () use ($socketDir): void {
30+ @unlink($socketDir.'/warm-process.sock');
31+ @rmdir($socketDir);
32+});
2733
2834 /**
2935 * @param list<string> $command
@@ -42,47 +48,47 @@ function start(array $command, ?array $env = null)
4248 return $process;
4349 }
4450
45-$startWorker = static function () use ($handlerFile, $workerPort) {
46- $worker = start([\PHP_BINARY, __DIR__.'/../resources/emulator/worker.php', $handlerFile, (string) $workerPort]);
47- // Waits until it listens, so the first requests do not find the port closed.
48- for ($i = 0; $i < 100 && proc_get_status($worker)['running']; ++$i) {
49- if (false !== $connection = @stream_socket_client('tcp://127.0.0.1:'.$workerPort)) {
51+$startWarmProcess = static function () use ($handlerFile, $socket) {
52+ $warmProcess = start([\PHP_BINARY, __DIR__.'/../resources/emulator/warm-process.php', $handlerFile, $socket]);
53+ // Waits until it listens, so the first requests do not find the socket closed.
54+ for ($i = 0; $i < 100 && proc_get_status($warmProcess)['running']; ++$i) {
55+ if (false !== $connection = @stream_socket_client($socket)) {
5056 fclose($connection);
5157 break;
5258 }
5359 usleep(50_000);
5460 }
5561
56- return $worker;
62+ return $warmProcess;
5763 };
5864
59-$worker = $startWorker();
65+$warmProcess = $startWarmProcess();
6066 $server = start(
6167 [\PHP_BINARY, '-d', 'enable_post_data_reading=0', '-d', 'expose_php=0', '-d', 'default_mimetype=', '-S', $address, __DIR__.'/../resources/emulator/router.php'],
62- ['SCALEWAY_EMULATOR_AUTOLOAD' => $autoload, 'SCALEWAY_EMULATOR_WORKER_PORT' => (string) $workerPort] + getenv(),
68+ ['SCALEWAY_EMULATOR_AUTOLOAD' => $autoload, 'SCALEWAY_EMULATOR_SOCKET' => $socket] + getenv(),
6369 );
64-// Children outlive a killed parent, so a SIGTERM would leave the port and the worker busy.
70+// Children outlive a killed parent, so a SIGTERM would leave the port busy and the Warm Process running.
6571 if (function_exists('pcntl_async_signals')) {
6672 pcntl_async_signals(true);
6773 foreach ([\SIGINT, \SIGTERM] as $signal) {
68- pcntl_signal($signal, static function () use (&$worker, $server): never {
69- proc_terminate($worker);
74+ pcntl_signal($signal, static function () use (&$warmProcess, $server): never {
75+ proc_terminate($warmProcess);
7076 proc_terminate($server);
7177 exit(0);
7278 });
7379 }
7480 }
75-fwrite(\STDERR, "Emulator listening on http://{$address}, Warm Process pid ".proc_get_status($worker)['pid'].\PHP_EOL);
81+fwrite(\STDERR, "Emulator listening on http://{$address}, Warm Process pid ".proc_get_status($warmProcess)['pid'].\PHP_EOL);
7682
7783 while (($status = proc_get_status($server))['running']) {
7884 // A dead Warm Process is replaced, as the platform starts a new instance after a crash.
79- if (!proc_get_status($worker)['running']) {
80- $worker = $startWorker();
81- fwrite(\STDERR, 'Emulator: the Warm Process exited, started a new one with pid '.proc_get_status($worker)['pid'].\PHP_EOL);
85+ if (!proc_get_status($warmProcess)['running']) {
86+ $warmProcess = $startWarmProcess();
87+ fwrite(\STDERR, 'Emulator: the Warm Process exited, started a new one with pid '.proc_get_status($warmProcess)['pid'].\PHP_EOL);
8288 // Keeps an application that cannot even load from flooding the console.
8389 sleep(1);
8490 }
8591 usleep(200_000);
8692 }
87-proc_terminate($worker);
93+proc_terminate($warmProcess);
8894 exit($status['exitcode']);
@@ -20,10 +20,16 @@ if (!is_file($handlerFile)) {
20 exit(1);20 exit(1);
21 }21 }
22 22
23-// shortcut: another process may grab the port before the worker binds it; rerun if the worker fails to listen.23+// A Unix socket in a private directory: no port to race for, and other local users cannot send Events.
24-$probe = stream_socket_server('tcp://127.0.0.1:0') ?: exit(1);24+$socketDir = sys_get_temp_dir().'/scaleway-serve-'.bin2hex(random_bytes(4));
25-$workerPort = (int) substr((string) strrchr((string) stream_socket_get_name($probe, false), ':'), 1);25+if (!mkdir($socketDir, 0o700)) {
26-fclose($probe);26+ exit(1);
27+}
28+$socket = 'unix://'.$socketDir.'/warm-process.sock';
29+register_shutdown_function(static function () use ($socketDir): void {
30+ @unlink($socketDir.'/warm-process.sock');
31+ @rmdir($socketDir);
32+});
27 33
28 /**34 /**
29 * @param list<string> $command35 * @param list<string> $command
@@ -42,47 +48,47 @@ function start(array $command, ?array $env = null)
42 return $process;48 return $process;
43 }49 }
44 50
45-$startWorker = static function () use ($handlerFile, $workerPort) {51+$startWarmProcess = static function () use ($handlerFile, $socket) {
46- $worker = start([\PHP_BINARY, __DIR__.'/../resources/emulator/worker.php', $handlerFile, (string) $workerPort]);52+ $warmProcess = start([\PHP_BINARY, __DIR__.'/../resources/emulator/warm-process.php', $handlerFile, $socket]);
47- // Waits until it listens, so the first requests do not find the port closed.53+ // Waits until it listens, so the first requests do not find the socket closed.
48- for ($i = 0; $i < 100 && proc_get_status($worker)['running']; ++$i) {54+ for ($i = 0; $i < 100 && proc_get_status($warmProcess)['running']; ++$i) {
49- if (false !== $connection = @stream_socket_client('tcp://127.0.0.1:'.$workerPort)) {55+ if (false !== $connection = @stream_socket_client($socket)) {
50 fclose($connection);56 fclose($connection);
51 break;57 break;
52 }58 }
53 usleep(50_000);59 usleep(50_000);
54 }60 }
55 61
56- return $worker;62+ return $warmProcess;
57 };63 };
58 64
59-$worker = $startWorker();65+$warmProcess = $startWarmProcess();
60 $server = start(66 $server = start(
61 [\PHP_BINARY, '-d', 'enable_post_data_reading=0', '-d', 'expose_php=0', '-d', 'default_mimetype=', '-S', $address, __DIR__.'/../resources/emulator/router.php'],67 [\PHP_BINARY, '-d', 'enable_post_data_reading=0', '-d', 'expose_php=0', '-d', 'default_mimetype=', '-S', $address, __DIR__.'/../resources/emulator/router.php'],
62- ['SCALEWAY_EMULATOR_AUTOLOAD' => $autoload, 'SCALEWAY_EMULATOR_WORKER_PORT' => (string) $workerPort] + getenv(),68+ ['SCALEWAY_EMULATOR_AUTOLOAD' => $autoload, 'SCALEWAY_EMULATOR_SOCKET' => $socket] + getenv(),
63 );69 );
64-// Children outlive a killed parent, so a SIGTERM would leave the port and the worker busy.70+// Children outlive a killed parent, so a SIGTERM would leave the port busy and the Warm Process running.
65 if (function_exists('pcntl_async_signals')) {71 if (function_exists('pcntl_async_signals')) {
66 pcntl_async_signals(true);72 pcntl_async_signals(true);
67 foreach ([\SIGINT, \SIGTERM] as $signal) {73 foreach ([\SIGINT, \SIGTERM] as $signal) {
68- pcntl_signal($signal, static function () use (&$worker, $server): never {74+ pcntl_signal($signal, static function () use (&$warmProcess, $server): never {
69- proc_terminate($worker);75+ proc_terminate($warmProcess);
70 proc_terminate($server);76 proc_terminate($server);
71 exit(0);77 exit(0);
72 });78 });
73 }79 }
74 }80 }
75-fwrite(\STDERR, "Emulator listening on http://{$address}, Warm Process pid ".proc_get_status($worker)['pid'].\PHP_EOL);81+fwrite(\STDERR, "Emulator listening on http://{$address}, Warm Process pid ".proc_get_status($warmProcess)['pid'].\PHP_EOL);
76 82
77 while (($status = proc_get_status($server))['running']) {83 while (($status = proc_get_status($server))['running']) {
78 // A dead Warm Process is replaced, as the platform starts a new instance after a crash.84 // A dead Warm Process is replaced, as the platform starts a new instance after a crash.
79- if (!proc_get_status($worker)['running']) {85+ if (!proc_get_status($warmProcess)['running']) {
80- $worker = $startWorker();86+ $warmProcess = $startWarmProcess();
81- fwrite(\STDERR, 'Emulator: the Warm Process exited, started a new one with pid '.proc_get_status($worker)['pid'].\PHP_EOL);87+ fwrite(\STDERR, 'Emulator: the Warm Process exited, started a new one with pid '.proc_get_status($warmProcess)['pid'].\PHP_EOL);
82 // Keeps an application that cannot even load from flooding the console.88 // Keeps an application that cannot even load from flooding the console.
83 sleep(1);89 sleep(1);
84 }90 }
85 usleep(200_000);91 usleep(200_000);
86 }92 }
87-proc_terminate($worker);93+proc_terminate($warmProcess);
88 exit($status['exitcode']);94 exit($status['exitcode']);
modified resources/emulator/router.php +2 -2
@@ -4,7 +4,7 @@ declare(strict_types=1);
44
55 /*
66 * The php -S router of the Emulator: the built-in server parses HTTP but wipes all PHP state after each
7- * request, so the Handler runs in a separate long-lived worker, and this router forwards Events to it.
7+ * request, so the Handler runs in a separate Warm Process, and this router forwards Events to it.
88 */
99
1010 use JDecool\ScalewayRuntime\Emulator;
@@ -28,7 +28,7 @@ $event = Emulator::toEvent(
2828 );
2929
3030 $response = null;
31-$connection = @stream_socket_client('tcp://127.0.0.1:'.getenv('SCALEWAY_EMULATOR_WORKER_PORT'), $errorCode, $errorMessage);
31+$connection = @stream_socket_client((string) getenv('SCALEWAY_EMULATOR_SOCKET'), $errorCode, $errorMessage);
3232 if (false !== $connection) {
3333 // Scaleway allows Invocations of up to 60 minutes.
3434 stream_set_timeout($connection, 3600);
@@ -4,7 +4,7 @@ declare(strict_types=1);
4 4
5 /*5 /*
6 * The php -S router of the Emulator: the built-in server parses HTTP but wipes all PHP state after each6 * The php -S router of the Emulator: the built-in server parses HTTP but wipes all PHP state after each
7- * request, so the Handler runs in a separate long-lived worker, and this router forwards Events to it.7+ * request, so the Handler runs in a separate Warm Process, and this router forwards Events to it.
8 */8 */
9 9
10 use JDecool\ScalewayRuntime\Emulator;10 use JDecool\ScalewayRuntime\Emulator;
@@ -28,7 +28,7 @@ $event = Emulator::toEvent(
28 );28 );
29 29
30 $response = null;30 $response = null;
31-$connection = @stream_socket_client('tcp://127.0.0.1:'.getenv('SCALEWAY_EMULATOR_WORKER_PORT'), $errorCode, $errorMessage);31+$connection = @stream_socket_client((string) getenv('SCALEWAY_EMULATOR_SOCKET'), $errorCode, $errorMessage);
32 if (false !== $connection) {32 if (false !== $connection) {
33 // Scaleway allows Invocations of up to 60 minutes.33 // Scaleway allows Invocations of up to 60 minutes.
34 stream_set_timeout($connection, 3600);34 stream_set_timeout($connection, 3600);
renamed resources/emulator/warm-process.php +8 -6
similarity index 69%
rename from resources/emulator/worker.php
rename to resources/emulator/warm-process.php
@@ -4,24 +4,26 @@ declare(strict_types=1);
44
55 /*
66 * The Warm Process of the Emulator: loads the application's handler.php once, then calls handle()
7- * for each Event the router forwards, as Scaleway does. Usage: worker.php <handler.php> <port>.
7+ * for each Event the router forwards, as Scaleway does. Usage: warm-process.php <handler.php> <unix://socket>.
88 */
99
10-[, $handlerFile, $port] = ($argv ?? []) + [1 => '', 2 => ''];
10+[, $handlerFile, $socket] = ($argv ?? []) + [1 => '', 2 => ''];
1111
1212 require $handlerFile;
1313
14-$server = stream_socket_server('tcp://127.0.0.1:'.$port, $errorCode, $errorMessage);
14+// A previous Warm Process that crashed leaves its socket file behind, which would make binding fail.
15+@unlink(substr($socket, strlen('unix://')));
16+$server = stream_socket_server($socket, $errorCode, $errorMessage);
1517 if (false === $server) {
16- fwrite(\STDERR, "Cannot listen on port {$port}: {$errorMessage}".\PHP_EOL);
18+ fwrite(\STDERR, "Cannot listen on {$socket}: {$errorMessage}".\PHP_EOL);
1719 exit(1);
1820 }
1921
20-// Accepting only fails when interrupted; the worker then exits and the Emulator starts a new one.
22+// Accepting only fails when interrupted; the Warm Process then exits and the Emulator starts a new one.
2123 while (false !== $connection = @stream_socket_accept($server, -1)) {
2224 try {
2325 $payload = (string) stream_get_contents($connection);
24- // The Emulator connects without a payload to check this worker is up.
26+ // The Emulator connects without a payload to check this process is up.
2527 if ('' === $payload) {
2628 continue;
2729 }
similarity index 69%
rename from resources/emulator/worker.php
rename to resources/emulator/warm-process.php
@@ -4,24 +4,26 @@ declare(strict_types=1);
4 4
5 /*5 /*
6 * The Warm Process of the Emulator: loads the application's handler.php once, then calls handle()6 * The Warm Process of the Emulator: loads the application's handler.php once, then calls handle()
7- * for each Event the router forwards, as Scaleway does. Usage: worker.php <handler.php> <port>.7+ * for each Event the router forwards, as Scaleway does. Usage: warm-process.php <handler.php> <unix://socket>.
8 */8 */
9 9
10-[, $handlerFile, $port] = ($argv ?? []) + [1 => '', 2 => ''];10+[, $handlerFile, $socket] = ($argv ?? []) + [1 => '', 2 => ''];
11 11
12 require $handlerFile;12 require $handlerFile;
13 13
14-$server = stream_socket_server('tcp://127.0.0.1:'.$port, $errorCode, $errorMessage);14+// A previous Warm Process that crashed leaves its socket file behind, which would make binding fail.
15+@unlink(substr($socket, strlen('unix://')));
16+$server = stream_socket_server($socket, $errorCode, $errorMessage);
15 if (false === $server) {17 if (false === $server) {
16- fwrite(\STDERR, "Cannot listen on port {$port}: {$errorMessage}".\PHP_EOL);18+ fwrite(\STDERR, "Cannot listen on {$socket}: {$errorMessage}".\PHP_EOL);
17 exit(1);19 exit(1);
18 }20 }
19 21
20-// Accepting only fails when interrupted; the worker then exits and the Emulator starts a new one.22+// Accepting only fails when interrupted; the Warm Process then exits and the Emulator starts a new one.
21 while (false !== $connection = @stream_socket_accept($server, -1)) {23 while (false !== $connection = @stream_socket_accept($server, -1)) {
22 try {24 try {
23 $payload = (string) stream_get_contents($connection);25 $payload = (string) stream_get_contents($connection);
24- // The Emulator connects without a payload to check this worker is up.26+ // The Emulator connects without a payload to check this process is up.
25 if ('' === $payload) {27 if ('' === $payload) {
26 continue;28 continue;
27 }29 }