diff options
-rw-r--r-- | src/network/socket/qnativesocketengine.cpp | 6 | ||||
-rw-r--r-- | tests/auto/qtcpsocket/tst_qtcpsocket.cpp | 25 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/network/socket/qnativesocketengine.cpp b/src/network/socket/qnativesocketengine.cpp index a890b3b..a169ca0 100644 --- a/src/network/socket/qnativesocketengine.cpp +++ b/src/network/socket/qnativesocketengine.cpp @@ -185,6 +185,9 @@ void QNativeSocketEnginePrivate::setError(QAbstractSocket::SocketError error, Er // socket to recreate its engine after an error. Note: There's // one exception: SocketError(11) bypasses this as it's purely // a temporary internal error condition. + // Another exception is the way the waitFor*() functions set + // an error when a timeout occurs. After the call to setError() + // they reset the hasSetSocketError to false return; } if (error != QAbstractSocket::SocketError(11)) @@ -859,6 +862,7 @@ bool QNativeSocketEngine::waitForRead(int msecs, bool *timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); + d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); @@ -927,6 +931,7 @@ bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); + d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); @@ -978,6 +983,7 @@ bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWri *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); + d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); diff --git a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp index e638e287..12686bb 100644 --- a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp @@ -191,6 +191,7 @@ private slots: void increaseReadBufferSize(); void taskQtBug5799ConnectionErrorWaitForConnected(); void taskQtBug5799ConnectionErrorEventLoop(); + void taskQtBug7054TimeoutErrorResetting(); void invalidProxy_data(); void invalidProxy(); @@ -2236,6 +2237,30 @@ void tst_QTcpSocket::taskQtBug5799ConnectionErrorEventLoop() QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit()); } +void tst_QTcpSocket::taskQtBug7054TimeoutErrorResetting() +{ + QTcpSocket *socket = newSocket(); + + socket->connectToHost(QtNetworkSettings::serverName(), 443); + QVERIFY(socket->waitForConnected(5*1000)); + QVERIFY(socket->error() == QAbstractSocket::UnknownSocketError); + + // We connected to the HTTPS port. Wait two seconds to receive data. We will receive + // nothing because we would need to start the SSL handshake + QVERIFY(!socket->waitForReadyRead(2*1000)); + QVERIFY(socket->error() == QAbstractSocket::SocketTimeoutError); + + // Now write some crap to make the server disconnect us. 4 lines are enough. + socket->write("a\r\nb\r\nc\r\nd\r\n"); + socket->waitForBytesWritten(2*1000); + + // we try to waitForReadyRead another time, but this time instead of a timeout we + // should get a better error since the server disconnected us + QVERIFY(!socket->waitForReadyRead(2*1000)); + // It must NOT be the SocketTimeoutError that had been set before + QVERIFY(socket->error() == QAbstractSocket::RemoteHostClosedError); +} + void tst_QTcpSocket::invalidProxy_data() { QTest::addColumn<int>("type"); |