reactphp/react — pull request #273 by cboden (2014-02-16 19:54:14+00:00)
Title: [Stream] Fix infinite loop writing to closed stream

refs #211

I was able to create a 100% CPU spike by not draining the buffer on the client side and then closing the connection when there was data in the write buffer on the server side.

Because feof reports true when read OR write is closed we previously tried to determine this status by meta data but that's now proved unreliable. This PR will ignore feof in the write buffer until 0 data has been sent - which either means the buffer is full (not an error) or the buffer is closed for writing.

I ran /examples/tcp-chat.php and used this script to create the 100% CPU spike:
```php
createCached('8.8.8.8', $loop);
$connector = new React\SocketClient\Connector($loop, $dns);

$sender = null;

$connector->create('127.0.0.1', 4000)->then(function(React\Stream\Stream $stream) use (&$sender) {
$sender = $stream;

$much_data = str_repeat('a', 1024*1024*32);
$stream->write($much_data);
$stream->end();
});

$connector->create('127.0.0.1', 4000)->then(function(React\Stream\Stream $stream) use ($loop, &$sender) {
$stream->on('data', function($data) use ($stream, &$sender) {
echo "Pausing after receiving\n";

$stream->pause();

$sender->write('one more');
$stream->close();
});
});

$loop->run();
```