diff --git a/src/libs/ssh/sshconnectionmanager.cpp b/src/libs/ssh/sshconnectionmanager.cpp index da55a5a911a..72bf95c8944 100644 --- a/src/libs/ssh/sshconnectionmanager.cpp +++ b/src/libs/ssh/sshconnectionmanager.cpp @@ -38,9 +38,23 @@ #include #include #include +#include namespace QSsh { namespace Internal { +class UnaquiredConnection { +public: + UnaquiredConnection(SshConnection *conn) : connection(conn), scheduledForRemoval(false) {} + + SshConnection *connection; + bool scheduledForRemoval; +}; +bool operator==(const UnaquiredConnection &c1, const UnaquiredConnection &c2) { + return c1.connection == c2.connection; +} +bool operator!=(const UnaquiredConnection &c1, const UnaquiredConnection &c2) { + return !(c1 == c2); +} class SshConnectionManager : public QObject { @@ -50,13 +64,16 @@ public: SshConnectionManager() { moveToThread(QCoreApplication::instance()->thread()); + connect(&m_removalTimer, &QTimer::timeout, + this, &SshConnectionManager::removeInactiveConnections); + m_removalTimer.start(150000); // For a total timeout of five minutes. } ~SshConnectionManager() { - foreach (SshConnection * const connection, m_unacquiredConnections) { - disconnect(connection, 0, this, 0); - delete connection; + foreach (const UnaquiredConnection &connection, m_unacquiredConnections) { + disconnect(connection.connection, 0, this, 0); + delete connection.connection; } QSSH_ASSERT(m_acquiredConnections.isEmpty()); @@ -82,8 +99,9 @@ public: return connection; } - // Checked cached open connections: - foreach (SshConnection * const connection, m_unacquiredConnections) { + // Check cached open connections: + foreach (const UnaquiredConnection &c, m_unacquiredConnections) { + SshConnection * const connection = c.connection; if (connection->state() != SshConnection::Connected || connection->connectionParameters() != sshParams) continue; @@ -97,7 +115,7 @@ public: Q_ARG(QObject *, QThread::currentThread())); } - m_unacquiredConnections.removeOne(connection); + m_unacquiredConnections.removeOne(c); m_acquiredConnections.append(connection); return connection; } @@ -125,21 +143,21 @@ public: || connection->state() != SshConnection::Connected) { doDelete = true; } else { - QSSH_ASSERT_AND_RETURN(!m_unacquiredConnections.contains(connection)); + QSSH_ASSERT_AND_RETURN(!m_unacquiredConnections.contains(UnaquiredConnection(connection))); // It can happen that two or more connections with the same parameters were acquired // if the clients were running in different threads. Only keep one of them in // such a case. bool haveConnection = false; - foreach (SshConnection * const conn, m_unacquiredConnections) { - if (conn->connectionParameters() == connection->connectionParameters()) { + foreach (const UnaquiredConnection &c, m_unacquiredConnections) { + if (c.connection->connectionParameters() == connection->connectionParameters()) { haveConnection = true; break; } } if (!haveConnection) { connection->closeAllChannels(); // Clean up after neglectful clients. - m_unacquiredConnections.append(connection); + m_unacquiredConnections.append(UnaquiredConnection(connection)); } else { doDelete = true; } @@ -157,7 +175,7 @@ public: QMutexLocker locker(&m_listMutex); for (int i = 0; i < m_unacquiredConnections.count(); ++i) { - SshConnection * const connection = m_unacquiredConnections.at(i); + SshConnection * const connection = m_unacquiredConnections.at(i).connection; if (connection->connectionParameters() == sshParams) { disconnect(connection, 0, this, 0); delete connection; @@ -189,23 +207,39 @@ private slots: if (!currentConnection) return; - if (m_unacquiredConnections.removeOne(currentConnection)) { + if (m_unacquiredConnections.removeOne(UnaquiredConnection(currentConnection))) { disconnect(currentConnection, 0, this, 0); currentConnection->deleteLater(); } } + void removeInactiveConnections() + { + QMutexLocker locker(&m_listMutex); + for (int i = m_unacquiredConnections.count() - 1; i >= 0; --i) { + UnaquiredConnection &c = m_unacquiredConnections[i]; + if (c.scheduledForRemoval) { + disconnect(c.connection, 0, this, 0); + c.connection->deleteLater(); + m_unacquiredConnections.removeAt(i); + } else { + c.scheduledForRemoval = true; + } + } + } + private: // We expect the number of concurrently open connections to be small. // If that turns out to not be the case, we can still use a data // structure with faster access. - QList m_unacquiredConnections; + QList m_unacquiredConnections; // Can contain the same connection more than once; this acts as a reference count. QList m_acquiredConnections; QList m_deprecatedConnections; QMutex m_listMutex; + QTimer m_removalTimer; }; } // namespace Internal