Skip to content

Commit

Permalink
Dont let the application catch timeout exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Oct 8, 2021
1 parent 67af5b7 commit ceb19a5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/bref/src/BrefRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bref\Event\Handler;
use Runtime\Bref\Lambda\LambdaClient;
use Runtime\Bref\Timeout\Timeout;
use Symfony\Component\Runtime\RunnerInterface;

/**
Expand All @@ -27,7 +28,7 @@ public function run(): int
$lambda = LambdaClient::fromEnvironmentVariable('symfony-runtime');

$loops = 0;
while (true) {
while (!Timeout::$_tiggered) {
if (++$loops > $this->loopMax) {
return 0;
}
Expand All @@ -41,5 +42,7 @@ public function run(): int
return 0;
}
}

return 0;
}
}
5 changes: 4 additions & 1 deletion src/bref/src/ConsoleApplicationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Runtime\Bref;

use Runtime\Bref\Lambda\LambdaClient;
use Runtime\Bref\Timeout\Timeout;
use Symfony\Component\Console\Application;
use Symfony\Component\Runtime\RunnerInterface;

Expand All @@ -24,8 +25,10 @@ public function run(): int
{
$lambda = LambdaClient::fromEnvironmentVariable('symfony-runtime-console');

while (true) {
while (!Timeout::$_tiggered) {
$lambda->processNextEvent($this->handler);
}

return 0;
}
}
31 changes: 11 additions & 20 deletions src/bref/src/Timeout/Timeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ final class Timeout
/** @var bool */
private static $initialized = false;

/** @var string|null */
private static $stackTrace = null;
/**
* @var bool
* @internal
*/
public static $_tiggered = false;

/**
* @internal
Expand Down Expand Up @@ -44,8 +47,6 @@ public static function enable(int $remainingTimeInMillis): void
*/
private static function init(): void
{
self::$stackTrace = null;

if (self::$initialized) {
return;
}
Expand All @@ -60,25 +61,16 @@ private static function init(): void
// Setup a handler for SIGALRM that throws an exception
// This will interrupt any running PHP code, including `sleep()` or code stuck waiting for I/O.
pcntl_signal(SIGALRM, function (): void {
if (null !== Timeout::$stackTrace) {
// we have already thrown an exception, do a harder exit.
error_log('Lambda timed out');
error_log((new LambdaTimeoutException())->getTraceAsString());
error_log('Original stack trace');
error_log(Timeout::$stackTrace);

exit(1);
}

Timeout::$_tiggered = true;
$exception = new LambdaTimeoutException('Maximum AWS Lambda execution time reached');
Timeout::$stackTrace = $exception->getTraceAsString();

// Trigger another alarm after 1 second to do a hard exit.
pcntl_alarm(1);
// we have already thrown an exception, do a harder exit.
error_log($exception->getMessage());
error_log($exception->getTraceAsString());

throw $exception;
exit(1);
});

self::$_tiggered = false;
self::$initialized = true;
}

Expand All @@ -91,7 +83,6 @@ public static function reset(): void
{
if (self::$initialized) {
pcntl_alarm(0);
self::$stackTrace = null;
}
}
}

0 comments on commit ceb19a5

Please sign in to comment.