@@ -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