Skip to content

Commit

Permalink
Add an option to set BitTorrent session shutdown timeout
Browse files Browse the repository at this point in the history
PR #20797.
  • Loading branch information
glassez committed May 7, 2024
1 parent 79eb7b8 commit 2b728b3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ namespace BitTorrent
virtual void setPerformanceWarningEnabled(bool enable) = 0;
virtual int saveResumeDataInterval() const = 0;
virtual void setSaveResumeDataInterval(int value) = 0;
virtual int shutdownTimeout() const = 0;
virtual void setShutdownTimeout(int value) = 0;
virtual int port() const = 0;
virtual void setPort(int port) = 0;
virtual bool isSSLEnabled() const = 0;
Expand Down
33 changes: 32 additions & 1 deletion src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <libtorrent/session_status.hpp>
#include <libtorrent/torrent_info.hpp>

#include <QDeadlineTimer>
#include <QDebug>
#include <QDir>
#include <QHostAddress>
Expand Down Expand Up @@ -480,6 +481,7 @@ SessionImpl::SessionImpl(QObject *parent)
, m_isBandwidthSchedulerEnabled(BITTORRENT_SESSION_KEY(u"BandwidthSchedulerEnabled"_s), false)
, m_isPerformanceWarningEnabled(BITTORRENT_SESSION_KEY(u"PerformanceWarning"_s), false)
, m_saveResumeDataInterval(BITTORRENT_SESSION_KEY(u"SaveResumeDataInterval"_s), 60)
, m_shutdownTimeout(BITTORRENT_SESSION_KEY(u"ShutdownTimeout"_s), -1)
, m_port(BITTORRENT_SESSION_KEY(u"Port"_s), -1)
, m_sslEnabled(BITTORRENT_SESSION_KEY(u"SSL/Enabled"_s), false)
, m_sslPort(BITTORRENT_SESSION_KEY(u"SSL/Port"_s), -1)
Expand Down Expand Up @@ -602,6 +604,9 @@ SessionImpl::~SessionImpl()
{
m_nativeSession->pause();

const qint64 timeout = (m_shutdownTimeout >= 0) ? (m_shutdownTimeout * 1000) : -1;
const QDeadlineTimer shutdownDeadlineTimer {timeout};

if (m_torrentsQueueChanged)
{
m_nativeSession->post_torrent_updates({});
Expand All @@ -628,8 +633,24 @@ SessionImpl::~SessionImpl()
m_asyncWorker->clear();
m_asyncWorker->waitForDone();

qDebug("Deleting libtorrent session...");
auto *nativeSessionProxy = new lt::session_proxy(m_nativeSession->abort());
delete m_nativeSession;

qDebug("Deleting resume data storage...");
delete m_resumeDataStorage;
LogMsg(tr("Saving resume data completed."));

auto *sessionTerminateThread = QThread::create([nativeSessionProxy]()
{
qDebug("Deleting libtorrent session...");
delete nativeSessionProxy;
});
connect(sessionTerminateThread, &QThread::finished, sessionTerminateThread, &QObject::deleteLater);
sessionTerminateThread->start();
if (sessionTerminateThread->wait(shutdownDeadlineTimer))
LogMsg(tr("BitTorrent session successfully finished."));
else
LogMsg(tr("Session shutdown timed out."));
}

QString SessionImpl::getDHTBootstrapNodes() const
Expand Down Expand Up @@ -3476,6 +3497,16 @@ void SessionImpl::setSaveResumeDataInterval(const int value)
}
}

int SessionImpl::shutdownTimeout() const
{
return m_shutdownTimeout;
}

void SessionImpl::setShutdownTimeout(const int value)
{
m_shutdownTimeout = value;
}

int SessionImpl::port() const
{
return m_port;
Expand Down
3 changes: 3 additions & 0 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ namespace BitTorrent
void setPerformanceWarningEnabled(bool enable) override;
int saveResumeDataInterval() const override;
void setSaveResumeDataInterval(int value) override;
int shutdownTimeout() const override;
void setShutdownTimeout(int value) override;
int port() const override;
void setPort(int port) override;
bool isSSLEnabled() const override;
Expand Down Expand Up @@ -688,6 +690,7 @@ namespace BitTorrent
CachedSettingValue<bool> m_isBandwidthSchedulerEnabled;
CachedSettingValue<bool> m_isPerformanceWarningEnabled;
CachedSettingValue<int> m_saveResumeDataInterval;
CachedSettingValue<int> m_shutdownTimeout;
CachedSettingValue<int> m_port;
CachedSettingValue<bool> m_sslEnabled;
CachedSettingValue<int> m_sslPort;
Expand Down
15 changes: 14 additions & 1 deletion src/gui/advancedsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace
#endif // Q_OS_MACOS || Q_OS_WIN
PYTHON_EXECUTABLE_PATH,
START_SESSION_PAUSED,
SESSION_SHUTDOWN_TIMEOUT,

// libtorrent section
LIBTORRENT_HEADER,
Expand Down Expand Up @@ -334,6 +335,8 @@ void AdvancedSettings::saveAdvancedSettings() const
pref->setPythonExecutablePath(Path(m_pythonExecutablePath.text().trimmed()));
// Start session paused
session->setStartPaused(m_checkBoxStartSessionPaused.isChecked());
// Session shutdown timeout
session->setShutdownTimeout(m_spinBoxSessionShutdownTimeout.value());
// Choking algorithm
session->setChokingAlgorithm(m_comboBoxChokingAlgorithm.currentData().value<BitTorrent::ChokingAlgorithm>());
// Seed choking algorithm
Expand Down Expand Up @@ -848,7 +851,16 @@ void AdvancedSettings::loadAdvancedSettings()
addRow(PYTHON_EXECUTABLE_PATH, tr("Python executable path (may require restart)"), &m_pythonExecutablePath);
// Start session paused
m_checkBoxStartSessionPaused.setChecked(session->isStartPaused());
addRow(START_SESSION_PAUSED, tr("Start session in paused state"), &m_checkBoxStartSessionPaused);
addRow(START_SESSION_PAUSED, tr("Start BitTorrent session in paused state"), &m_checkBoxStartSessionPaused);
// Session shutdown timeout
m_spinBoxSessionShutdownTimeout.setMinimum(-1);
m_spinBoxSessionShutdownTimeout.setMaximum(std::numeric_limits<int>::max());
m_spinBoxSessionShutdownTimeout.setValue(session->shutdownTimeout());
m_spinBoxSessionShutdownTimeout.setSuffix(tr(" sec", " seconds"));
m_spinBoxSessionShutdownTimeout.setSpecialValueText(tr("-1 (unlimited)"));
m_spinBoxSessionShutdownTimeout.setToolTip(u"Sets the timeout for the session to be shut down gracefully, at which point it will be forcibly terminated.<br>Note that this does not apply to the saving resume data time."_s);
addRow(SESSION_SHUTDOWN_TIMEOUT, tr("BitTorrent session shutdown timeout [-1: unlimited]"), &m_spinBoxSessionShutdownTimeout);

// Choking algorithm
m_comboBoxChokingAlgorithm.addItem(tr("Fixed slots"), QVariant::fromValue(BitTorrent::ChokingAlgorithm::FixedSlots));
m_comboBoxChokingAlgorithm.addItem(tr("Upload rate based"), QVariant::fromValue(BitTorrent::ChokingAlgorithm::RateBased));
Expand Down Expand Up @@ -946,6 +958,7 @@ void AdvancedSettings::addRow(const int row, const QString &text, T *widget)
{
auto *label = new QLabel(text);
label->setOpenExternalLinks(true);
label->setToolTip(widget->toolTip());

setCellWidget(row, PROPERTY, label);
setCellWidget(row, VALUE, widget);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/advancedsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private slots:
m_spinBoxOutgoingPortsMin, m_spinBoxOutgoingPortsMax, m_spinBoxUPnPLeaseDuration, m_spinBoxPeerToS,
m_spinBoxListRefresh, m_spinBoxTrackerPort, m_spinBoxSendBufferWatermark, m_spinBoxSendBufferLowWatermark,
m_spinBoxSendBufferWatermarkFactor, m_spinBoxConnectionSpeed, m_spinBoxSocketSendBufferSize, m_spinBoxSocketReceiveBufferSize, m_spinBoxSocketBacklogSize,
m_spinBoxMaxConcurrentHTTPAnnounces, m_spinBoxStopTrackerTimeout,
m_spinBoxMaxConcurrentHTTPAnnounces, m_spinBoxStopTrackerTimeout, m_spinBoxSessionShutdownTimeout,
m_spinBoxSavePathHistoryLength, m_spinBoxPeerTurnover, m_spinBoxPeerTurnoverCutoff, m_spinBoxPeerTurnoverInterval, m_spinBoxRequestQueueSize;
QCheckBox m_checkBoxOsCache, m_checkBoxRecheckCompleted, m_checkBoxResolveCountries, m_checkBoxResolveHosts,
m_checkBoxProgramNotifications, m_checkBoxTorrentAddedNotifications, m_checkBoxReannounceWhenAddressChanged, m_checkBoxTrackerFavicon, m_checkBoxTrackerStatus,
Expand Down

0 comments on commit 2b728b3

Please sign in to comment.