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
8 changes: 8 additions & 0 deletions lib/Service/DecoderProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class DecoderProcess {
private ?int $exitCode = null;
private ?int $groupId = null;
private int $outputBytes = 0;
private bool $drainedSinceLastPoll = false;
private string $output = '';

/**
Expand Down Expand Up @@ -71,6 +72,7 @@ public static function start(array $command): ?self {
* listings, limit the combined bytes while only retaining stdout.
*/
public function drain(?int $outputLimit): ?string {
$this->drainedSinceLastPoll = false;
$remaining = self::MAX_DRAIN_PER_POLL;
foreach ([1, 2] as $index) {
while ($remaining > 0) {
Expand All @@ -82,6 +84,7 @@ public function drain(?int $outputLimit): ?string {
break;
}
$length = strlen($chunk);
$this->drainedSinceLastPoll = true;
$remaining -= $length;
if ($outputLimit !== null) {
if ($length > $outputLimit - $this->outputBytes) {
Expand All @@ -97,6 +100,11 @@ public function drain(?int $outputLimit): ?string {
return null;
}

/** Whether the last drain pass consumed bytes from either process pipe. */
public function hadOutput(): bool {
return $this->drainedSinceLastPoll;
}

public function hasExited(): bool {
$status = proc_get_status($this->process);
if ($this->isolated && $this->groupId === null) {
Expand Down
5 changes: 4 additions & 1 deletion lib/Service/ResourceLimitedProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ private static function execute(array $command, int $timeoutSeconds, ?StagingBud
if ($process->hasExited() && ($maxOutputBytes === null || $process->outputComplete())) {
break;
}
usleep(self::POLL_INTERVAL_US);
// A verbose decoder can fill the pipe much faster than a 50 ms
// polling cadence allows it to be drained. Keep reading promptly
// while data flows; retain the slower cadence when idle.
usleep($process->hadOutput() ? 1000 : self::POLL_INTERVAL_US);
}
} catch (\Throwable) {
$reason = 'inspection';
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/ResourceLimitedProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public function testLargeStdoutDoesNotBlockAValidDecoder(): void {
self::assertSame(['success' => true, 'reason' => ''], $result);
}

public function testLargeStderrDoesNotBlockAValidDecoder(): void {
$result = ResourceLimitedProcess::run(
[PHP_BINARY, '-r', 'fwrite(STDERR, str_repeat("x", 1000000));'],
$this->directory,
100,
10,
3,
);
self::assertSame(['success' => true, 'reason' => ''], $result);
}

public function testListingRejectsExcessOutput(): void {
$result = ResourceLimitedProcess::capture(
[PHP_BINARY, '-r', 'echo str_repeat("x", 1000000);'],
Expand Down
Loading