Skip to content

Commit ba5fb5c

Browse files
authored
Avoid uneeded fragmented TLS work around for PHP 7.3.3+ (#202)
Avoid uneeded fragmented TLS work around for PHP 7.3.3+
2 parents 032db4d + 5ffadc3 commit ba5fb5c

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,15 @@ This library does not take responsibility over these context options, so it's
13721372
up to consumers of this library to take care of setting appropriate context
13731373
options as described above.
13741374

1375+
PHP < 7.3.3 (and PHP < 7.2.15) suffers from a bug where feof() might
1376+
block with 100% CPU usage on fragmented TLS records.
1377+
We try to work around this by always consuming the complete receive
1378+
buffer at once to avoid stale data in TLS buffers. This is known to
1379+
work around high CPU usage for well-behaving peers, but this may
1380+
cause very large data chunks for high throughput scenarios. The buggy
1381+
behavior can still be triggered due to network I/O buffers or
1382+
malicious peers on affected versions, upgrading is highly recommended.
1383+
13751384
PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
13761385
chunks of data over TLS streams at once.
13771386
We try to work around this by limiting the write chunk size to 8192

src/Connection.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ class Connection extends EventEmitter implements ConnectionInterface
4343

4444
public function __construct($resource, LoopInterface $loop)
4545
{
46+
// PHP < 7.3.3 (and PHP < 7.2.15) suffers from a bug where feof() might
47+
// block with 100% CPU usage on fragmented TLS records.
48+
// We try to work around this by always consuming the complete receive
49+
// buffer at once to avoid stale data in TLS buffers. This is known to
50+
// work around high CPU usage for well-behaving peers, but this may
51+
// cause very large data chunks for high throughput scenarios. The buggy
52+
// behavior can still be triggered due to network I/O buffers or
53+
// malicious peers on affected versions, upgrading is highly recommended.
54+
// @link https://bugs.php.net/bug.php?id=77390
55+
$clearCompleteBuffer = \PHP_VERSION_ID < 70215 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70303);
56+
4657
// PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
4758
// chunks of data over TLS streams at once.
4859
// We try to work around this by limiting the write chunk size to 8192
@@ -53,14 +64,10 @@ public function __construct($resource, LoopInterface $loop)
5364
// See https://github.com/reactphp/socket/issues/105
5465
$limitWriteChunks = (\PHP_VERSION_ID < 70018 || (\PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70104));
5566

56-
// Construct underlying stream to always consume complete receive buffer.
57-
// This avoids stale data in TLS buffers and also works around possible
58-
// buffering issues in legacy PHP versions. The buffer size is limited
59-
// due to TCP/IP buffers anyway, so this should not affect usage otherwise.
6067
$this->input = new DuplexResourceStream(
6168
$resource,
6269
$loop,
63-
-1,
70+
$clearCompleteBuffer ? -1 : null,
6471
new WritableResourceStream($resource, $loop, null, $limitWriteChunks ? 8192 : null)
6572
);
6673

0 commit comments

Comments
 (0)