Skip to content
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

[1.x] Ensure connection close handler is cleaned up for each request #515

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Io/StreamingServer.php
Expand Up @@ -157,10 +157,17 @@ public function handleRequest(ConnectionInterface $conn, ServerRequestInterface
}

// cancel pending promise once connection closes
$connectionOnCloseResponseCancelerHandler = function () {};
if ($response instanceof PromiseInterface && \method_exists($response, 'cancel')) {
$conn->on('close', function () use ($response) {
$connectionOnCloseResponseCanceler = function () use ($response) {
$response->cancel();
});
};
$connectionOnCloseResponseCancelerHandler = function () use ($connectionOnCloseResponseCanceler, $conn) {
if ($connectionOnCloseResponseCanceler !== null) {
$conn->removeListener('close', $connectionOnCloseResponseCanceler);
}
};
$conn->on('close', $connectionOnCloseResponseCanceler);
}

// happy path: response returned, handle and return immediately
Expand Down Expand Up @@ -201,7 +208,7 @@ function ($error) use ($that, $conn, $request) {
$that->emit('error', array($exception));
return $that->writeError($conn, Response::STATUS_INTERNAL_SERVER_ERROR, $request);
}
);
)->then($connectionOnCloseResponseCancelerHandler, $connectionOnCloseResponseCancelerHandler);
}

/** @internal */
Expand Down
42 changes: 35 additions & 7 deletions tests/Io/StreamingServerTest.php
Expand Up @@ -25,9 +25,17 @@ class StreamingServerTest extends TestCase
*/
public function setUpConnectionMockAndSocket()
{
$this->connection = $this->getMockBuilder('React\Socket\Connection')
$this->connection = $this->mockConnection();

$this->socket = new SocketServerStub();
}


private function mockConnection(array $additionalMethods = null)
{
$connection = $this->getMockBuilder('React\Socket\Connection')
->disableOriginalConstructor()
->setMethods(
->setMethods(array_merge(
array(
'write',
'end',
Expand All @@ -39,14 +47,15 @@ public function setUpConnectionMockAndSocket()
'getRemoteAddress',
'getLocalAddress',
'pipe'
)
)
),
(is_array($additionalMethods) ? $additionalMethods : array())
))
->getMock();

$this->connection->method('isWritable')->willReturn(true);
$this->connection->method('isReadable')->willReturn(true);
$connection->method('isWritable')->willReturn(true);
$connection->method('isReadable')->willReturn(true);

$this->socket = new SocketServerStub();
return $connection;
}

public function testRequestEventWillNotBeEmittedForIncompleteHeaders()
Expand Down Expand Up @@ -3245,6 +3254,25 @@ public function testNewConnectionWillInvokeParserTwiceAfterInvokingRequestHandle
$this->assertCount(1, $this->connection->listeners('close'));
}

public function testCompletingARequestWillRemoveConnectionOnCloseListener()
{
$connection = $this->mockConnection(array('removeListener'));

$request = new ServerRequest('GET', 'http://localhost/');

$server = new StreamingServer(Loop::get(), function () {
return \React\Promise\resolve(new Response());
});

$server->listen($this->socket);
$this->socket->emit('connection', array($connection));

$connection->expects($this->once())->method('removeListener');

// pretend parser just finished parsing
$server->handleRequest($connection, $request);
}

private function createGetRequest()
{
$data = "GET / HTTP/1.1\r\n";
Expand Down