Skip to content

Commit

Permalink
Close connection on flush failure
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 12, 2017
1 parent f0f233b commit d979278
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 11 additions & 2 deletions lib/PgSqlHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function __construct($handle, $socket) {
}
});

$this->await = Loop::onWritable($socket, static function ($watcher) use (&$deferred, $handle) {
$this->await = Loop::onWritable($socket, static function ($watcher) use (&$deferred, &$listeners, &$handle) {
$flush = \pg_flush($handle);
if ($flush === 0) {
return; // Not finished sending data, listen again.
Expand All @@ -124,7 +124,16 @@ public function __construct($handle, $socket) {
Loop::disable($watcher);

if ($flush === false) {
$deferred->fail(new FailureException(\pg_last_error($handle)));
$exception = new ConnectionException(\pg_last_error($handle));
$handle = null; // Marks connection as dead.

foreach ($listeners as $listener) {
$listener->fail($exception);
}

if ($deferred !== null) {
$deferred->fail($exception);
}
}
});

Expand Down
22 changes: 17 additions & 5 deletions lib/PqHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ public function __construct(pq\Connection $handle) {

$this->poll = Loop::onReadable($this->handle->socket, static function ($watcher) use (&$deferred, &$listeners, &$handle) {
if ($handle->poll() === pq\Connection::POLLING_FAILED) {
$exception = new ConnectionException($handle->errorMessage);
$handle = null; // Marks connection as dead.
Loop::disable($watcher);

$exception = new ConnectionException($handle->errorMessage);

foreach ($listeners as $listener) {
$listener->fail($exception);
}
Expand All @@ -97,9 +96,22 @@ public function __construct(pq\Connection $handle) {
}
});

$this->await = Loop::onWritable($this->handle->socket, static function ($watcher) use (&$deferred, $handle) {
if (!$handle->flush()) {
return; // Not finished sending data, continue polling for writability.
$this->await = Loop::onWritable($this->handle->socket, static function ($watcher) use (&$deferred, &$listeners, &$handle) {
try {
if (!$handle->flush()) {
return; // Not finished sending data, continue polling for writability.
}
} catch (pq\Exception $exception) {
$exception = new ConnectionException("Flushing the connection failed", 0, $exception);
$handle = null; // Marks connection as dead.

foreach ($listeners as $listener) {
$listener->fail($exception);
}

if ($deferred !== null) {
$deferred->fail($exception);
}
}

Loop::disable($watcher);
Expand Down

0 comments on commit d979278

Please sign in to comment.