@@ -66,11 +66,10 @@ public function __construct(pq\Connection $handle) {
6666
6767 $ this ->poll = Loop::onReadable ($ this ->handle ->socket , static function ($ watcher ) use (&$ deferred , &$ listeners , &$ handle ) {
6868 if ($ handle ->poll () === pq \Connection::POLLING_FAILED ) {
69+ $ exception = new ConnectionException ($ handle ->errorMessage );
6970 $ handle = null ; // Marks connection as dead.
7071 Loop::disable ($ watcher );
7172
72- $ exception = new ConnectionException ($ handle ->errorMessage );
73-
7473 foreach ($ listeners as $ listener ) {
7574 $ listener ->fail ($ exception );
7675 }
@@ -97,9 +96,22 @@ public function __construct(pq\Connection $handle) {
9796 }
9897 });
9998
100- $ this ->await = Loop::onWritable ($ this ->handle ->socket , static function ($ watcher ) use (&$ deferred , $ handle ) {
101- if (!$ handle ->flush ()) {
102- return ; // Not finished sending data, continue polling for writability.
99+ $ this ->await = Loop::onWritable ($ this ->handle ->socket , static function ($ watcher ) use (&$ deferred , &$ listeners , &$ handle ) {
100+ try {
101+ if (!$ handle ->flush ()) {
102+ return ; // Not finished sending data, continue polling for writability.
103+ }
104+ } catch (pq \Exception $ exception ) {
105+ $ exception = new ConnectionException ("Flushing the connection failed " , 0 , $ exception );
106+ $ handle = null ; // Marks connection as dead.
107+
108+ foreach ($ listeners as $ listener ) {
109+ $ listener ->fail ($ exception );
110+ }
111+
112+ if ($ deferred !== null ) {
113+ $ deferred ->fail ($ exception );
114+ }
103115 }
104116
105117 Loop::disable ($ watcher );
0 commit comments