Skip to content

Fix closing socket resource before removing from loop #141

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 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix closing socket resource before removing from loop
  • Loading branch information
clue committed Dec 23, 2017
commit 006b41b858d3d2bc368b08094f9b19d19f2820b4
3 changes: 2 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ public function handleClose()
// side already closed. Shutting down may return to blocking mode on
// some legacy versions, so reset to non-blocking just in case before
// continuing to close the socket resource.
// Underlying Stream implementation will take care of closing file
// handle, so we otherwise keep this open here.
@stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
stream_set_blocking($this->stream, false);
fclose($this->stream);
}

public function getRemoteAddress()
Expand Down
39 changes: 39 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace React\Tests\Socket;

use React\Socket\Connection;

class ConnectionTest extends TestCase
{
public function testCloseConnectionWillCloseSocketResource()
{
$resource = fopen('php://memory', 'r+');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$connection = new Connection($resource, $loop);
$connection->close();

$this->assertFalse(is_resource($resource));
}

public function testCloseConnectionWillRemoveResourceFromLoopBeforeClosingResource()
{
$resource = fopen('php://memory', 'r+');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop->expects($this->once())->method('addWriteStream')->with($resource);

$onRemove = null;
$loop->expects($this->once())->method('removeWriteStream')->with($this->callback(function ($param) use (&$onRemove) {
$onRemove = is_resource($param);
return true;
}));

$connection = new Connection($resource, $loop);
$connection->write('test');
$connection->close();

$this->assertTrue($onRemove);
$this->assertFalse(is_resource($resource));
}
}
22 changes: 22 additions & 0 deletions tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ public function connectionToTcpServerShouldSucceedWithNullAddressesAfterConnecti
$this->assertNull($connection->getLocalAddress());
}

/** @test */
public function connectionToTcpServerWillCloseWhenOtherSideCloses()
{
$loop = Factory::create();

// immediately close connection and server once connection is in
$server = new TcpServer(0, $loop);
$server->on('connection', function (ConnectionInterface $conn) use ($server) {
$conn->close();
$server->close();
});

$once = $this->expectCallableOnce();
$connector = new TcpConnector($loop);
$connector->connect($server->getAddress())->then(function (ConnectionInterface $conn) use ($once) {
$conn->write('hello');
$conn->on('close', $once);
});

$loop->run();
}

/** @test */
public function connectionToEmptyIp6PortShouldFail()
{
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