Commit fdba503b2282a40c124066503de7cabe6afac7fa

Authored by Patric Stout
1 parent 856bcc0a

fix(connection): respect disconnect() requests while connecting

Especially select() can take 100ms (as that is the timeout),
after which a lot could have changed. So re-check the state if
we aren't asked to disconnect before continueing.
Showing 1 changed file with 17 additions and 4 deletions
src/Connection.cpp
... ... @@ -198,7 +198,11 @@ void Connection::resolve()
198 198 return;
199 199 }
200 200  
201   - m_state = State::CONNECTING;
  201 + // Only change the state if no disconnect() has been requested in the mean time.
  202 + if (m_state != State::STOP)
  203 + {
  204 + m_state = State::CONNECTING;
  205 + }
202 206 }
203 207  
204 208 bool Connection::connectToAny()
... ... @@ -222,6 +226,11 @@ bool Connection::connectToAny()
222 226 }
223 227  
224 228 int result = select(FD_SETSIZE, nullptr, &write_fds, nullptr, &timeout);
  229 + // As we have waiting a bit, check if no disconnect has been requested.
  230 + if (m_state == State::STOP)
  231 + {
  232 + return true;
  233 + }
225 234  
226 235 // Check if there was an error on select(). This is hard to recover from.
227 236 if (result < 0)
... ... @@ -320,9 +329,13 @@ bool Connection::connectToAny()
320 329 LOG_WARNING(this, "Could not set socket to non-blocking; expect performance impact");
321 330 }
322 331  
323   - m_socket = socket_connected;
324   - sendConnect();
325   - m_state = State::AUTHENTICATING;
  332 + // Only change the state if no disconnect() has been requested in the mean time.
  333 + if (m_state != State::STOP)
  334 + {
  335 + m_state = State::AUTHENTICATING;
  336 + m_socket = socket_connected;
  337 + sendConnect();
  338 + }
326 339 return true;
327 340 }
328 341  
... ...