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
27 changes: 27 additions & 0 deletions src/InvalidStreamException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace StreamIpc;

use RuntimeException;
use Throwable;

/**
* Exception thrown when a registered stream becomes invalid.
*/
class InvalidStreamException extends RuntimeException
{
private IpcSession $session;

public function __construct(IpcSession $session, ?string $message = null, int $code = 0, ?Throwable $previous = null)
{
$this->session = $session;
parent::__construct($message ?? 'Invalid stream resource', $code, $previous);
}

public function getSession(): IpcSession
{
return $this->session;
}
}
23 changes: 20 additions & 3 deletions src/NativeIpcPeer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace StreamIpc;

use RuntimeException;
use TypeError;
use ValueError;
use StreamIpc\Transport\MessageTransport;
use StreamIpc\Transport\NativeMessageTransport;

Expand Down Expand Up @@ -145,9 +147,24 @@ public function tick(?float $timeout = null): void
}

$writes = $except = null;
if (@stream_select($reads, $writes, $except, $sec, $usec) <= 0) {
// no streams ready or error occurred
return;
try {
if (@stream_select($reads, $writes, $except, $sec, $usec) <= 0) {
// no streams ready or error occurred
return;
}
} catch (TypeError|ValueError $e) {
foreach ($this->readSet as $key => $stream) {
$test = [$stream];
$w = $ex = null;
try {
stream_select($test, $w, $ex, 0, 0);
} catch (TypeError|ValueError) {
[$session] = $this->fdMap[$key];
throw new InvalidStreamException($session, null, 0, $e);
}
}

throw $e;
}

foreach ($reads as $stream) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/NativeIpcPeerInvalidStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace StreamIpc\Tests\Unit;

use PHPUnit\Framework\TestCase;
use StreamIpc\InvalidStreamException;
use StreamIpc\NativeIpcPeer;

final class NativeIpcPeerInvalidStreamTest extends TestCase
{
public function testTickThrowsInvalidStreamExceptionWithSession(): void
{
[$a, $b] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$peer = new NativeIpcPeer();
$session = $peer->createStreamSession($a, $a, $b);
fclose($b);

try {
$peer->tick();
$this->fail('No exception thrown');
} catch (InvalidStreamException $e) {
$this->assertSame($session, $e->getSession());
} finally {
fclose($a);
}
}
}