Skip to content

Closing connection using TLS #320

Description

@dododedodonl

When closing a connection on a server using TLS, eg:

<?phprequire__DIR__ . '/vendor/autoload.php';
$server = newReact\Socket\TcpServer($port = 9900);
$server = newReact\Socket\SecureServer($server, null, [
'local_cert' => 'server.pem',
'passphrase' => 'secret'
]);
$server->on('connection', function (React\Socket\ConnectionInterface$connection) {
echo'[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
// do stuff$connection->end();
});

the connection is not closed properly, as

publicfunctionhandleClose()
{
if (!\is_resource($this->stream)) {
return;
}
// Try to cleanly shut down socket and ignore any errors in case other
// side already closed. Underlying Stream implementation will take care
// of closing stream resource, so we otherwise keep this open here.
@\stream_socket_shutdown($this->stream, \STREAM_SHUT_RDWR);
}

uses https://www.php.net/manual/en/function.stream-socket-shutdown.php.

Note comment https://www.php.net/manual/en/function.stream-socket-shutdown.php#125659 which states tls does not get shut down properly that way.

Solution seems to be to replace @\stream_socket_shutdown($this->stream, \STREAM_SHUT_RDWR); with \fclose($this->stream).

Other solution seems to be to call @\stream_socket_enable_crypto($this->stream, false); before the socket shutdown as mentioned in comment https://www.php.net/manual/en/function.stream-socket-shutdown.php#126303

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions