Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion src/Executor/Runner/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
24 changes: 11 additions & 13 deletions tests/e2e/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -1107,23 +1107,21 @@ 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']);

\sleep(5);

$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']);
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/functions/node-exit/index.js
Original file line number Diff line number Diff line change
@@ -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);
};
12 changes: 12 additions & 0 deletions tests/resources/functions/node-exit/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
6 changes: 0 additions & 6 deletions tests/resources/functions/php-exit/index.php

This file was deleted.

Loading