diff --git a/src/Executor/Runner/Docker.php b/src/Executor/Runner/Docker.php index f709717f..3385bd1c 100644 --- a/src/Executor/Runner/Docker.php +++ b/src/Executor/Runner/Docker.php @@ -1095,13 +1095,26 @@ public function createExecution( if ($runtime->listening === 0) { // Wait for cold-start to finish (app listening on port) $pingStart = \microtime(true); - $validator = new TCP(); + // A gone container's stale address drops the connect instead of refusing it, and the loop pings again anyway + $validator = new TCP(1); + // The first inspect waits until the runtime is 2s old, also for requests that joined its cold start + $crashCheckAt = \max($pingStart, $runtime->created + 2); while (true) { // If timeout is passed, stop and return error if (\microtime(true) - $pingStart >= $timeout) { throw new ExecutorException(ExecutorException::RUNTIME_TIMEOUT); } + // Checked before the ping, which waits out its timeout once the container is gone + if (\microtime(true) >= $crashCheckAt) { + $crash = $this->getCrash($runtimeName, $logging); + if ($crash !== null) { + throw new ExecutorException(ExecutorException::RUNTIME_FAILED, $crash); + } + + $crashCheckAt = \microtime(true) + 3; + } + $online = $validator->isValid($hostname . ':' . 3000); if ($online) { break; @@ -1191,6 +1204,51 @@ public function createExecution( return $execution; } + /** + * Returns why a runtime's server exited, with the tail of its output when logging is enabled, or null while it may still start listening. + */ + private function getCrash(string $runtimeName, bool $logging): ?string + { + $output = ''; + $stderr = ''; + $code = Console::execute('docker container inspect --format ' . \escapeshellarg('{{.State.Status}} {{.State.ExitCode}} {{.State.OOMKilled}}') . ' ' . \escapeshellarg($runtimeName), '', $output, $stderr, 5); + if ($code !== 0) { + return null; + } + + [$status, $exitCode, $oomKilled] = \explode(' ', \trim($output)) + ['', '0', 'false']; + + // A running container may have restarted after a crash and serve now, so only the ping decides it + if (!\in_array($status, ['restarting', 'exited', 'dead'], true)) { + return null; + } + + $details = ['exit code ' . $exitCode]; + if ($oomKilled === 'true') { + $details[] = 'out of memory'; + } + + $message = 'Runtime crashed while starting (' . \implode(', ', $details) . ')'; + + if (!$logging) { + return $message . '.'; + } + + // tail -c bounds what is buffered, since --tail limits lines and not their length + $failed = 'OPR_LOGS_FAILED'; + $logs = ''; + $code = Console::execute('(docker logs --tail 100 ' . \escapeshellarg($runtimeName) . ' 2>&1 || echo ' . $failed . ') | tail -c ' . (10 * 1024), '', $logs, $stderr, 5); + $logs = \trim($logs); + if ($code !== 0 || $logs === '' || \str_ends_with($logs, $failed)) { + return $message . '.'; + } + + // Invalid UTF-8, including a character cut by tail, would fail the JSON encoding of the error response + $logs = \mb_convert_encoding($logs, 'UTF-8', 'UTF-8'); + + return $message . ":\n" . $logs; + } + public function getRuntimes(): mixed { $runtimes = []; diff --git a/tests/e2e/ExecutorTest.php b/tests/e2e/ExecutorTest.php index bf304b1f..d8b4d4ae 100644 --- a/tests/e2e/ExecutorTest.php +++ b/tests/e2e/ExecutorTest.php @@ -1084,19 +1084,19 @@ public function testRestartPolicy(): void { $output = ''; $stderr = ''; - Console::execute('cd /app/tests/resources/functions/php-exit && tar --exclude code.tar.gz -czf code.tar.gz .', '', $output, $stderr); + Console::execute('cd /app/tests/resources/functions/node-exit && tar --exclude code.tar.gz -czf code.tar.gz .', '', $output, $stderr); - $command = 'php src/server.php'; + $command = 'bash helpers/server.sh'; /** Build runtime */ $params = [ 'runtimeId' => 'test-build-restart-policy', - 'source' => '/storage/functions/php-exit/code.tar.gz', + 'source' => '/storage/functions/node-exit/code.tar.gz', 'destination' => '/storage/builds/test-restart-policy', - 'entrypoint' => 'index.php', - 'image' => 'openruntimes/php:v5-8.1', - 'command' => 'tar -zxf /tmp/code.tar.gz -C /mnt/code && bash helpers/build.sh ""' + 'entrypoint' => 'index.js', + 'image' => 'openruntimes/node:v5-22', + 'command' => 'tar -zxf /tmp/code.tar.gz -C /mnt/code && bash helpers/build.sh "npm i"' ]; $response = $this->client->call(Client::METHOD_POST, '/runtimes', [], $params); @@ -1107,14 +1107,12 @@ public function testRestartPolicy(): void /** Execute function */ $response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec-restart-policy/executions', [], [ 'source' => $buildPath, - 'entrypoint' => 'index.php', - 'image' => 'openruntimes/php:v5-8.1', + 'entrypoint' => 'index.js', + 'image' => 'openruntimes/node:v5-22', 'runtimeEntrypoint' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $command . '"', 'restartPolicy' => 'always' ]); - // The runtime reports the function's exit() as a failed execution, so the - // executor call itself succeeds and carries statusCode 500. The server - // process still dies afterwards, which is what the restart count asserts. + // The function answers 500 and then ends the server process, which is what the restart count asserts. $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(500, $response['body']['statusCode']); @@ -1122,8 +1120,8 @@ public function testRestartPolicy(): void $response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec-restart-policy/executions', [], [ 'source' => $buildPath, - 'entrypoint' => 'index.php', - 'image' => 'openruntimes/php:v5-8.1', + 'entrypoint' => 'index.js', + 'image' => 'openruntimes/node:v5-22', 'runtimeEntrypoint' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $command . '"' ]); $this->assertEquals(200, $response['headers']['status-code']); diff --git a/tests/resources/functions/node-exit/index.js b/tests/resources/functions/node-exit/index.js new file mode 100644 index 00000000..79a9c970 --- /dev/null +++ b/tests/resources/functions/node-exit/index.js @@ -0,0 +1,5 @@ +// Answers, then ends the server process, so the restart policy brings the runtime back +module.exports = async (context) => { + setTimeout(() => process.exit(1), 100); + return context.res.send('', 500); +}; diff --git a/tests/resources/functions/node-exit/package.json b/tests/resources/functions/node-exit/package.json new file mode 100644 index 00000000..586de1c4 --- /dev/null +++ b/tests/resources/functions/node-exit/package.json @@ -0,0 +1,12 @@ +{ + "name": "node-exit", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/tests/resources/functions/php-exit/index.php b/tests/resources/functions/php-exit/index.php deleted file mode 100644 index 1ece8129..00000000 --- a/tests/resources/functions/php-exit/index.php +++ /dev/null @@ -1,6 +0,0 @@ -res->send('OK'); -};