Skip to content

Commit

Permalink
Fix torrent stop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Zitrax committed Dec 6, 2024
1 parent da609cd commit 5141ce4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,18 +927,13 @@ void Torrent::schedule_retry_peers() {
// FIXME: Remove this function
void Torrent::run() {
logger()->debug("Run loop start");
while (!all_of(m_peers.begin(), m_peers.end(),
[](auto& p) { return p->io_service().stopped(); })) {
// logger()->debug("Run loop");
const auto is_stopped = [](auto& p) { return p->io_service().stopped(); };
while (!m_stopped && (!done() || !ranges::all_of(m_peers, is_stopped))) {
std::size_t ran = 0;
for (auto& p : m_peers) {
ran += p->io_service().poll_one();
}
// TODO: Investigate why these were needed originally. They can
// not be called like commented out here at least since it
// will spam calls in every loop iteration of the run loop.
// retry_pieces();
// retry_peers();
ran += m_io_context.poll_one();
// If no handlers ran, then sleep.
if (!ran) {
#ifdef WIN32
Expand All @@ -958,6 +953,7 @@ void Torrent::stop() {
peer->stop();
}
tracker_request(TrackerEvent::STOPPED);
m_stopped = true;
}

Torrent* Torrent::get(const Sha1& info_hash) {
Expand Down Expand Up @@ -1018,6 +1014,10 @@ void Torrent::read_peers_binary_form(
}

void Torrent::retry_pieces() {
if (m_stopped) {
return;
}

const ScopeGuard scope_guard([this]() { schedule_retry_pieces(); });

logger()->debug("Checking pieces for retry");
Expand Down Expand Up @@ -1075,6 +1075,10 @@ bool Torrent::add_peer(shared_ptr<Peer> peer) {
}

void Torrent::retry_peers() {
if (m_stopped) {
return;
}

const ScopeGuard scope_guard([this]() { schedule_retry_peers(); });

logger()->debug("Checking peers for retry");
Expand Down
1 change: 1 addition & 0 deletions src/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ class Torrent {
HttpGet m_http_get;
asio::steady_timer m_retry_pieces_timer;
asio::steady_timer m_retry_peers_timer;
bool m_stopped = false;

// Piece housekeeping
mutable std::mutex m_mutex{};
Expand Down

0 comments on commit 5141ce4

Please sign in to comment.