Skip to content

Fix ExtEventLoop to keep track of stream resources (refcount) #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2017
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
10 changes: 9 additions & 1 deletion src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ExtEventLoop implements LoopInterface
private $streamCallback;
private $streamEvents = [];
private $streamFlags = [];
private $streamRefs = [];
private $readListeners = [];
private $writeListeners = [];
private $running;
Expand Down Expand Up @@ -110,7 +111,8 @@ private function removeStream($stream)
$this->streamFlags[$key],
$this->streamEvents[$key],
$this->readListeners[$key],
$this->writeListeners[$key]
$this->writeListeners[$key],
$this->streamRefs[$key]
);
}
}
Expand Down Expand Up @@ -224,6 +226,12 @@ private function subscribeStreamEvent($stream, $flag)

$this->streamEvents[$key] = $event;
$this->streamFlags[$key] = $flag;

// ext-event does not increase refcount on stream resources for PHP 7+
// manually keep track of stream resource to prevent premature garbage collection
if (PHP_VERSION_ID >= 70000) {
$this->streamRefs[$key] = $stream;
}
}

$event->add();
Expand Down
33 changes: 33 additions & 0 deletions tests/AbstractLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@ public function testAddReadStreamIgnoresSecondCallable()
$this->tickLoop($this->loop);
}

public function testAddReadStreamReceivesDataFromStreamReference()
{
$this->received = '';
$this->subAddReadStreamReceivesDataFromStreamReference();
$this->assertEquals('', $this->received);

$this->assertRunFasterThan($this->tickTimeout * 2);
$this->assertEquals('[hello]X', $this->received);
}

/**
* Telper for above test. This happens in another helper method to verify
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper typo

* the loop keep track of assigned stream resources (refcount).
*/
private function subAddReadStreamReceivesDataFromStreamReference()
{
list ($input, $output) = $this->createSocketPair();

fwrite($input, 'hello');
fclose($input);

$this->loop->addReadStream($output, function ($output) {
$chunk = fread($output, 1024);
if ($chunk === '') {
$this->received .= 'X';
$this->loop->removeReadStream($output);
fclose($output);
} else {
$this->received .= '[' . $chunk . ']';
}
});
}

public function testAddWriteStream()
{
list ($input) = $this->createSocketPair();
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy