Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ class Buffer extends EventEmitter implements WritableStreamInterface
private $writable = true;
private $loop;
private $data = '';
private $lastError = array(
'number' => 0,
'message' => '',
'file' => '',
'line' => 0,
);
private $lastError;

public function __construct($stream, LoopInterface $loop)
{
$this->stream = $stream;
$this->loop = $loop;
$this->lastErrorFlush();
}

public function isWritable()
Expand Down Expand Up @@ -83,13 +79,15 @@ public function handleWrite()
return;
}

$this->lastErrorFlush();

set_error_handler(array($this, 'errorHandler'));

$sent = fwrite($this->stream, $this->data);

restore_error_handler();

if (false === $sent) {
if ($this->lastError['number'] > 0) {
$this->emit('error', array(
new \ErrorException(
$this->lastError['message'],
Expand All @@ -104,9 +102,8 @@ public function handleWrite()
return;
}

if (0 === $sent && feof($this->stream)) {
$this->emit('error', array(new \RuntimeException('Tried to write to closed stream.'), $this));

if ($sent === false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this change is known to cause issues with running the Autobahn testsuite against Ratchet. Thanks to @cboden for digging into this!

I'm currently looking into providing a test case so that this is properly covered in the Stream component.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It ended up being line 90. The error I'm receiving is:

fwrite(): send of 8192 bytes failed with errno=11 Resource temporarily unavailable

On attempting to send a large amount of data (256k) to fwrite. The error simply states the buffer is full and to try again later, however the updated code now halts on all errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be fixed via #52.

$this->emit('error', array(new \RuntimeException('Send failed'), $this));
return;
}

Expand All @@ -132,4 +129,13 @@ private function errorHandler($errno, $errstr, $errfile, $errline)
$this->lastError['file'] = $errfile;
$this->lastError['line'] = $errline;
}

private function lastErrorFlush() {
$this->lastError = array(
'number' => 0,
'message' => '',
'file' => '',
'line' => 0,
);
}
}
2 changes: 1 addition & 1 deletion tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function testWritingToClosedStream()
$buffer->write('bar');

$this->assertInstanceOf('Exception', $error);
$this->assertSame('Tried to write to closed stream.', $error->getMessage());
$this->assertSame('fwrite(): send of 3 bytes failed with errno=32 Broken pipe', $error->getMessage());
}

private function createWriteableLoopMock()
Expand Down