@@ -87,11 +87,14 @@ PgConnection::PgConnection(trantor::EventLoop *loop,
8787 [](PGconn *conn) { PQfinish (conn); })),
8888 channel_ (loop, PQsocket(connectionPtr_.get()))
8989{
90+ if (channel_.fd () < 0 )
91+ {
92+ LOG_ERROR << " Failed to create Postgres connection" ;
93+ }
9094}
9195
9296void PgConnection::init ()
9397{
94- PQsetnonblocking (connectionPtr_.get (), 1 );
9598 if (channel_.fd () < 0 )
9699 {
97100 LOG_ERROR << " Connection with Postgres could not be established" ;
@@ -103,6 +106,8 @@ void PgConnection::init()
103106 }
104107 return ;
105108 }
109+
110+ PQsetnonblocking (connectionPtr_.get (), 1 );
106111 channel_.setReadCallback ([this ]() {
107112 if (status_ == ConnectStatus::Bad)
108113 {
@@ -165,8 +170,11 @@ void PgConnection::disconnect()
165170 auto thisPtr = shared_from_this ();
166171 loop_->runInLoop ([thisPtr, &pro]() {
167172 thisPtr->status_ = ConnectStatus::Bad;
168- thisPtr->channel_ .disableAll ();
169- thisPtr->channel_ .remove ();
173+ if (thisPtr->channel_ .fd () >= 0 )
174+ {
175+ thisPtr->channel_ .disableAll ();
176+ thisPtr->channel_ .remove ();
177+ }
170178 thisPtr->connectionPtr_ .reset ();
171179 pro.set_value (1 );
172180 });
@@ -522,11 +530,17 @@ void PgConnection::handleFatalError(bool clearAll, bool isAbortPipeline)
522530 {
523531 for (auto &cmd : batchCommandsForWaitingResults_)
524532 {
525- cmd->exceptionCallback_ (exceptPtr);
533+ if (cmd->exceptionCallback_ )
534+ {
535+ cmd->exceptionCallback_ (exceptPtr);
536+ }
526537 }
527538 for (auto &cmd : batchSqlCommands_)
528539 {
529- cmd->exceptionCallback_ (exceptPtr);
540+ if (cmd->exceptionCallback_ )
541+ {
542+ cmd->exceptionCallback_ (exceptPtr);
543+ }
530544 }
531545 batchCommandsForWaitingResults_.clear ();
532546 batchSqlCommands_.clear ();
@@ -536,13 +550,19 @@ void PgConnection::handleFatalError(bool clearAll, bool isAbortPipeline)
536550 if (!batchSqlCommands_.empty () &&
537551 !batchSqlCommands_.front ()->preparingStatement_ .empty ())
538552 {
539- batchSqlCommands_.front ()->exceptionCallback_ (exceptPtr);
553+ if (batchSqlCommands_.front ()->exceptionCallback_ )
554+ {
555+ batchSqlCommands_.front ()->exceptionCallback_ (exceptPtr);
556+ }
540557 batchSqlCommands_.pop_front ();
541558 }
542559 else if (!batchCommandsForWaitingResults_.empty ())
543560 {
544561 auto &cmd = batchCommandsForWaitingResults_.front ();
545- cmd->exceptionCallback_ (exceptPtr);
562+ if (cmd->exceptionCallback_ )
563+ {
564+ cmd->exceptionCallback_ (exceptPtr);
565+ }
546566 batchCommandsForWaitingResults_.pop_front ();
547567 }
548568 else
0 commit comments