Fix flushing of sockets, causing random sync errors
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 12 Nov 2013 23:37:12 +0000 (00:37 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 12 Nov 2013 23:37:12 +0000 (00:37 +0100)
TIL that flushing a socket may trigger a readyRead(). In certain cases
this would lead to compression being enabled on the client side too late
(because we would flush the socket before), breaking the sync to core.

As (contrary to starting SSL) enabling the compression does not care if
there's still uncompressed data in the socket's write buffer, we can just
remove this flush.

Also, let's make it explicit that we flush immediately before starting
encryption, rather than hiding that somewhere in the LegacyPeer, to avoid
introducing weird bugs later. And also flush the socket before handing the
peer over to the CoreSession, as we used to do before the refactoring -
we don't want bug #682 back.

src/common/protocols/legacy/legacypeer.cpp
src/core/coreauthhandler.cpp

index 08f5b15..eab7ea9 100644 (file)
@@ -48,7 +48,6 @@ void LegacyPeer::setSignalProxy(::SignalProxy *proxy)
     RemotePeer::setSignalProxy(proxy);
 
     // FIXME only in compat mode
     RemotePeer::setSignalProxy(proxy);
 
     // FIXME only in compat mode
-    socket()->flush();
     if (proxy) {
         // enable compression now if requested - the initial handshake is uncompressed in the legacy protocol!
         _useCompression = socket()->property("UseCompression").toBool();
     if (proxy) {
         // enable compression now if requested - the initial handshake is uncompressed in the legacy protocol!
         _useCompression = socket()->property("UseCompression").toBool();
@@ -302,7 +301,6 @@ void LegacyPeer::dispatch(const ClientRegistered &msg) {
     m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
 
     writeSocketData(m);
     m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
 
     writeSocketData(m);
-    socket()->flush(); // ensure that the write cache is flushed before we switch to ssl
 }
 
 
 }
 
 
index 5942676..4b2824f 100644 (file)
@@ -68,6 +68,7 @@ void CoreAuthHandler::startSsl()
 
     qDebug() << qPrintable(tr("Starting encryption for Client:"))  << _peer->description();
     connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), SLOT(onSslErrors()));
 
     qDebug() << qPrintable(tr("Starting encryption for Client:"))  << _peer->description();
     connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), SLOT(onSslErrors()));
+    sslSocket->flush(); // ensure that the write cache is flushed before we switch to ssl (bug 682)
     sslSocket->startServerEncryption();
 #endif
 }
     sslSocket->startServerEncryption();
 #endif
 }
@@ -148,5 +149,6 @@ void CoreAuthHandler::handle(const Login &msg)
     disconnect(_peer, 0, this, 0);
     _peer->setParent(0); // Core needs to take care of this one now!
 
     disconnect(_peer, 0, this, 0);
     _peer->setParent(0); // Core needs to take care of this one now!
 
+    socket()->flush(); // Make sure all data is sent before handing over the peer (and socket) to the session thread (bug 682)
     emit handshakeComplete(_peer, uid);
 }
     emit handshakeComplete(_peer, uid);
 }