Skip to content

Commit 827971f

Browse files
authored
Merge pull request #11225 from vimeo/log_long_running
Log files being processed for too long
2 parents 487e9ce + 98f880c commit 827971f

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

src/Psalm/Internal/Cli/Psalm.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ public static function getThreads(array $options, Config $config, bool $in_ci, b
431431
}
432432

433433
if ($for_scan) {
434-
if (isset($options['scanThreads'])) {
435-
$threads = max(1, (int)$options['scanThreads']);
434+
if (isset($options['scan-threads'])) {
435+
$threads = max(1, (int)$options['scan-threads']);
436436
} elseif (isset($options['debug']) || $in_ci) {
437437
$threads = 1;
438438
} elseif ($config->scan_threads) {

src/Psalm/Internal/Fork/ForkContext.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Amp\Parallel\Context\Internal\ExitFailure;
1414
use Amp\Parallel\Context\Internal\ExitSuccess;
1515
use Amp\Parallel\Ipc\IpcHub;
16+
use Amp\Serialization\NativeSerializer;
1617
use Amp\Serialization\SerializationException;
1718
use Amp\TimeoutCancellation;
1819
use Error;
@@ -25,6 +26,7 @@
2526
use function Amp\Parallel\Ipc\connect;
2627
use function count;
2728
use function define;
29+
use function extension_loaded;
2830
use function fwrite;
2931
use function is_file;
3032
use function is_string;
@@ -63,6 +65,10 @@ public static function start(
6365
?Cancellation $cancellation = null,
6466
int $childConnectTimeout = self::DEFAULT_START_TIMEOUT,
6567
): self {
68+
$serializer = extension_loaded('igbinary')
69+
? new IgbinarySerializer
70+
: new NativeSerializer;
71+
6672
$key = $ipcHub->generateKey();
6773

6874
// Fork
@@ -74,10 +80,10 @@ public static function start(
7480
if ($pid > 0) {
7581
try {
7682
$socket = $ipcHub->accept($key, $cancellation);
77-
$ipcChannel = new StreamChannel($socket, $socket);
83+
$ipcChannel = new StreamChannel($socket, $socket, $serializer);
7884

7985
$socket = $ipcHub->accept($key, $cancellation);
80-
$resultChannel = new StreamChannel($socket, $socket);
86+
$resultChannel = new StreamChannel($socket, $socket, $serializer);
8187
} catch (Throwable $exception) {
8288
$cancellation?->throwIfRequested();
8389

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

99105
try {
100106
$socket = connect($uri, $key, $connectCancellation);
101-
$ipcChannel = new StreamChannel($socket, $socket);
107+
$ipcChannel = new StreamChannel($socket, $socket, $serializer);
102108

103109
$socket = connect($uri, $key, $connectCancellation);
104-
$resultChannel = new StreamChannel($socket, $socket);
110+
$resultChannel = new StreamChannel($socket, $socket, $serializer);
105111
} catch (Throwable $exception) {
106112
trigger_error($exception->getMessage(), E_USER_ERROR);
107113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Psalm\Internal\Fork;
6+
7+
use Amp\Serialization\SerializationException;
8+
use Amp\Serialization\Serializer;
9+
use Throwable;
10+
11+
use function igbinary_serialize;
12+
use function igbinary_unserialize;
13+
use function sprintf;
14+
15+
/**
16+
* @internal
17+
*/
18+
final class IgbinarySerializer implements Serializer
19+
{
20+
public function serialize(mixed $data): string
21+
{
22+
try {
23+
$data = igbinary_serialize($data);
24+
if ($data === false) {
25+
throw new SerializationException("Could not serialize data!");
26+
}
27+
return $data;
28+
} catch (Throwable $exception) {
29+
throw new SerializationException(
30+
sprintf(
31+
'The given data could not be serialized: %s',
32+
$exception->getMessage(),
33+
),
34+
0,
35+
$exception,
36+
);
37+
}
38+
}
39+
40+
public function unserialize(string $data): mixed
41+
{
42+
try {
43+
return igbinary_unserialize($data);
44+
} catch (Throwable $exception) {
45+
throw new SerializationException(
46+
'Exception thrown when unserializing data',
47+
0,
48+
$exception,
49+
);
50+
}
51+
}
52+
}

src/Psalm/Internal/Fork/Pool.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use AssertionError;
1919
use Closure;
2020
use Psalm\Progress\Progress;
21-
use Throwable;
21+
use Revolt\EventLoop;
2222

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

0 commit comments

Comments
 (0)