Skip to content

Commit 960e6a9

Browse files
committed
Merge branch 'refs/heads/0.3' into 0.4
Conflicts: CHANGELOG.md src/Stream/Buffer.php
2 parents 000c79a + 3b9b9d6 commit 960e6a9

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
### 0.3.4 (2014-03-30)
5+
6+
* Bug fix: [Stream] Fixed 100% CPU spike from non-empty write buffer on closed stream
7+
* Buf fix: [Socket] Reset socket to non-blocking after shutting down (PHP bug)
8+
49
### 0.4.0 (2014-02-02)
510

611
* Feature: Added ChildProcess to run async child processes within the event loop (@jmikola)

src/Socket/Connection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public function handleData($stream)
2121
public function handleClose()
2222
{
2323
if (is_resource($this->stream)) {
24+
// http://chat.stackoverflow.com/transcript/message/7727858#7727858
2425
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
26+
stream_set_blocking($this->stream, false);
2527
fclose($this->stream);
2628
}
2729
}

src/Stream/Buffer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Evenement\EventEmitter;
66
use React\EventLoop\LoopInterface;
7-
use React\Stream\WritableStreamInterface;
87

98
/** @event full-drain */
109
class Buffer extends EventEmitter implements WritableStreamInterface
@@ -21,16 +20,11 @@ class Buffer extends EventEmitter implements WritableStreamInterface
2120
'file' => '',
2221
'line' => 0,
2322
);
24-
private $meta;
2523

2624
public function __construct($stream, LoopInterface $loop)
2725
{
2826
$this->stream = $stream;
2927
$this->loop = $loop;
30-
31-
if (is_resource($stream)) {
32-
$this->meta = stream_get_meta_data($stream);
33-
}
3428
}
3529

3630
public function isWritable()
@@ -83,8 +77,8 @@ public function close()
8377

8478
public function handleWrite()
8579
{
86-
if (!is_resource($this->stream) || ('generic_socket' === $this->meta['stream_type'] && feof($this->stream))) {
87-
$this->emit('error', array(new \RuntimeException('Tried to write to closed or invalid stream.'), $this));
80+
if (!is_resource($this->stream)) {
81+
$this->emit('error', array(new \RuntimeException('Tried to write to invalid stream.'), $this));
8882

8983
return;
9084
}
@@ -110,6 +104,12 @@ public function handleWrite()
110104
return;
111105
}
112106

107+
if (0 === $sent && feof($this->stream)) {
108+
$this->emit('error', array(new \RuntimeException('Tried to write to closed stream.'), $this));
109+
110+
return;
111+
}
112+
113113
$len = strlen($this->data);
114114
if ($len >= $this->softLimit && $len - $sent < $this->softLimit) {
115115
$this->emit('drain', [$this]);

tests/Stream/BufferTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,32 @@ public function testError()
184184

185185
$buffer->write('Attempting to write to bad stream');
186186
$this->assertInstanceOf('Exception', $error);
187-
$this->assertSame('Tried to write to closed or invalid stream.', $error->getMessage());
187+
$this->assertSame('Tried to write to invalid stream.', $error->getMessage());
188+
}
189+
190+
public function testWritingToClosedStream()
191+
{
192+
if ('Darwin' === PHP_OS) {
193+
$this->markTestSkipped('OS X issue with shutting down pair for writing');
194+
}
195+
196+
list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
197+
$loop = $this->createWriteableLoopMock();
198+
199+
$error = null;
200+
201+
$buffer = new Buffer($a, $loop);
202+
$buffer->on('error', function($message) use (&$error) {
203+
$error = $message;
204+
});
205+
206+
$buffer->write('foo');
207+
stream_socket_shutdown($b, STREAM_SHUT_RD);
208+
stream_socket_shutdown($a, STREAM_SHUT_RD);
209+
$buffer->write('bar');
210+
211+
$this->assertInstanceOf('Exception', $error);
212+
$this->assertSame('Tried to write to closed stream.', $error->getMessage());
188213
}
189214

190215
private function createWriteableLoopMock()

0 commit comments

Comments
 (0)
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