@@ -43,6 +43,17 @@ class Connection extends EventEmitter implements ConnectionInterface
43
43
44
44
public function __construct ($ resource , LoopInterface $ loop )
45
45
{
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
+
46
57
// PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
47
58
// chunks of data over TLS streams at once.
48
59
// We try to work around this by limiting the write chunk size to 8192
@@ -53,14 +64,10 @@ public function __construct($resource, LoopInterface $loop)
53
64
// See https://github.com/reactphp/socket/issues/105
54
65
$ limitWriteChunks = (\PHP_VERSION_ID < 70018 || (\PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70104 ));
55
66
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.
60
67
$ this ->input = new DuplexResourceStream (
61
68
$ resource ,
62
69
$ loop ,
63
- - 1 ,
70
+ $ clearCompleteBuffer ? - 1 : null ,
64
71
new WritableResourceStream ($ resource , $ loop , null , $ limitWriteChunks ? 8192 : null )
65
72
);
66
73
0 commit comments