Skip to content

Commit

Permalink
Merge pull request #11225 from vimeo/log_long_running
Browse files Browse the repository at this point in the history
Log files being processed for too long
  • Loading branch information
danog authored Jan 30, 2025
2 parents 487e9ce + 98f880c commit 827971f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ public static function getThreads(array $options, Config $config, bool $in_ci, b
}

if ($for_scan) {
if (isset($options['scanThreads'])) {
$threads = max(1, (int)$options['scanThreads']);
if (isset($options['scan-threads'])) {
$threads = max(1, (int)$options['scan-threads']);
} elseif (isset($options['debug']) || $in_ci) {
$threads = 1;
} elseif ($config->scan_threads) {
Expand Down
14 changes: 10 additions & 4 deletions src/Psalm/Internal/Fork/ForkContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Amp\Parallel\Context\Internal\ExitFailure;
use Amp\Parallel\Context\Internal\ExitSuccess;
use Amp\Parallel\Ipc\IpcHub;
use Amp\Serialization\NativeSerializer;
use Amp\Serialization\SerializationException;
use Amp\TimeoutCancellation;
use Error;
Expand All @@ -25,6 +26,7 @@
use function Amp\Parallel\Ipc\connect;
use function count;
use function define;
use function extension_loaded;
use function fwrite;
use function is_file;
use function is_string;
Expand Down Expand Up @@ -63,6 +65,10 @@ public static function start(
?Cancellation $cancellation = null,
int $childConnectTimeout = self::DEFAULT_START_TIMEOUT,
): self {
$serializer = extension_loaded('igbinary')
? new IgbinarySerializer
: new NativeSerializer;

$key = $ipcHub->generateKey();

// Fork
Expand All @@ -74,10 +80,10 @@ public static function start(
if ($pid > 0) {
try {
$socket = $ipcHub->accept($key, $cancellation);
$ipcChannel = new StreamChannel($socket, $socket);
$ipcChannel = new StreamChannel($socket, $socket, $serializer);

$socket = $ipcHub->accept($key, $cancellation);
$resultChannel = new StreamChannel($socket, $socket);
$resultChannel = new StreamChannel($socket, $socket, $serializer);
} catch (Throwable $exception) {
$cancellation?->throwIfRequested();

Expand All @@ -98,10 +104,10 @@ public static function start(

try {
$socket = connect($uri, $key, $connectCancellation);
$ipcChannel = new StreamChannel($socket, $socket);
$ipcChannel = new StreamChannel($socket, $socket, $serializer);

$socket = connect($uri, $key, $connectCancellation);
$resultChannel = new StreamChannel($socket, $socket);
$resultChannel = new StreamChannel($socket, $socket, $serializer);
} catch (Throwable $exception) {
trigger_error($exception->getMessage(), E_USER_ERROR);
}
Expand Down
52 changes: 52 additions & 0 deletions src/Psalm/Internal/Fork/IgbinarySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Psalm\Internal\Fork;

use Amp\Serialization\SerializationException;
use Amp\Serialization\Serializer;
use Throwable;

use function igbinary_serialize;
use function igbinary_unserialize;
use function sprintf;

/**
* @internal
*/
final class IgbinarySerializer implements Serializer
{
public function serialize(mixed $data): string
{
try {
$data = igbinary_serialize($data);
if ($data === false) {
throw new SerializationException("Could not serialize data!");
}
return $data;
} catch (Throwable $exception) {
throw new SerializationException(
sprintf(
'The given data could not be serialized: %s',
$exception->getMessage(),
),
0,
$exception,
);
}
}

public function unserialize(string $data): mixed
{
try {
return igbinary_unserialize($data);
} catch (Throwable $exception) {
throw new SerializationException(
'Exception thrown when unserializing data',
0,
$exception,
);
}
}
}
9 changes: 6 additions & 3 deletions src/Psalm/Internal/Fork/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use AssertionError;
use Closure;
use Psalm\Progress\Progress;
use Throwable;
use Revolt\EventLoop;

use function Amp\Future\await;
use function array_map;
Expand Down Expand Up @@ -95,8 +95,11 @@ public function run(
if ($task_done_closure) {
$f->map($task_done_closure);
}
$f->catch(fn(Throwable $e) => throw $e);
$f->map(function () use (&$cnt, $total): void {
$id = EventLoop::delay(10.0, function () use ($file): void {
$this->progress->write(PHP_EOL."Processing $file is taking more than 10 seconds...".PHP_EOL);
});
$f->map(function () use (&$cnt, $total, $id): void {
EventLoop::cancel($id);
$cnt++;
if (!($cnt % 10)) {
$percent = (int) (($cnt*100) / $total);
Expand Down

0 comments on commit 827971f

Please sign in to comment.