A completion is told to the trackers once and at once, and a check that changes what is left is told at once. libtorrent 0.16.27's TrackerController had two gaps that a private tracker sees as missing seeding or a missing completion (found 2026-10-31 from a user's report: the tracker's history showed no completion for a download finished in rtorrent, and the seed time counted as not seeding until a manual re-announce): - With pieces.hash.on_completion on, the controller is off while the finished data are read again, so confirm_finished() only queues "completed". The resume after the check turns the controller on without a start event, and do_timeout() then holds the queued event until the next regular announce, up to the tracker's interval (30-60 minutes). A stop in that time (a move of the finished files, a check, a restart, a package upgrade) clears mask_send, and a manual announce (d.tracker_announce) sends a plain update whose answer clears it: the tracker never hears "completed". Measured with the shipped binary and a logging tracker: the event never arrived after a re-announce, nor after d.stop/d.start. - The resume after a hash check of a running torrent (d.check_hash, ruTorrent's recheck) starts the download with start_skip_tracker. When the check found the data complete, the tracker still had the last announce's "left" (a leecher) until the next regular announce. Measured on a NAS: a torrent completed by a check of data put in place by hand announced nothing, and its next announce was due 30 minutes later (the tracker's interval). The change: - flag_completed_owed: set with "completed" and cleared only by a tracker's answer to a "completed" announce. A stop, a start or a plain update in between keeps it; the next answered announce of the download is followed by "completed" to every tracker in use (one per tracker, so a tracker that counts every event counts one). Data that are no longer complete when it could go (a check found pieces missing) owe nothing. - do_timeout() sends an owed completion at once to every tracker in use that is free; a tracker inside its failure backoff gets it when the backoff ends. The active path of send_completed_event() already sends at once; this makes the queued path do the same. - TrackerList remembers what the last announce said was left; Download::start() with start_skip_tracker calls TrackerController::send_update_if_left_changed(), which sends an update when that differs from what is left now and no event is on its way. A check that changes nothing sends nothing. - Not covered: the owed flag lives in memory, so a restart of rtorrent between a completion and its answer (a round trip, seconds) still loses it. Verified by 5 cppunit tests in tests/libtorrent-tracker-completed.tests.patch (each fails with its part of the change taken out), upstream's whole suite with tools/libtorrent-test.sh, and tools/announce-test.sh against the built binary (a seeder and a leecher on the build VM and the logging tracker tools/announce-catcher.py). diff --git a/src/torrent/download.cc b/src/torrent/download.cc index 50c760f..80ea95d 100644 --- a/src/torrent/download.cc +++ b/src/torrent/download.cc @@ -120,6 +120,8 @@ Download::start(int flags) { if (!(flags & start_skip_tracker)) m_ptr->main()->tracker_controller().send_start_event(); + else + m_ptr->main()->tracker_controller().send_update_if_left_changed(); } void diff --git a/src/torrent/tracker/wrappers.cc b/src/torrent/tracker/wrappers.cc index 555acd9..6f15f5b 100644 --- a/src/torrent/tracker/wrappers.cc +++ b/src/torrent/tracker/wrappers.cc @@ -191,6 +191,11 @@ TrackerControllerWrapper::send_update_event() { m_ptr->send_update_event(); } +void +TrackerControllerWrapper::send_update_if_left_changed() { + m_ptr->send_update_if_left_changed(); +} + void TrackerControllerWrapper::start_requesting() { m_ptr->start_requesting(); diff --git a/src/torrent/tracker/wrappers.h b/src/torrent/tracker/wrappers.h index d670333..ce417ee 100644 --- a/src/torrent/tracker/wrappers.h +++ b/src/torrent/tracker/wrappers.h @@ -84,6 +84,7 @@ protected: void send_stop_event(); void send_completed_event(); void send_update_event(); + void send_update_if_left_changed(); void start_requesting(); void stop_requesting(); diff --git a/src/tracker/tracker_controller.cc b/src/tracker/tracker_controller.cc index 9d76d0f..6add675 100644 --- a/src/tracker/tracker_controller.cc +++ b/src/tracker/tracker_controller.cc @@ -213,7 +213,7 @@ TrackerController::send_completed_event() { } m_flags &= ~mask_send; - m_flags |= flag_send_completed; + m_flags |= flag_send_completed | flag_completed_owed; if (!(m_flags & flag_active) || !m_tracker_list->has_in_use()) { LT_LOG_TRACKER_EVENTS("sending completed event : queued", 0); @@ -257,6 +257,62 @@ TrackerController::send_update_event() { } } +void +TrackerController::send_update_if_left_changed() { + if (!(m_flags & flag_active) || !m_tracker_list->has_usable()) + return; + + // An event on its way (a start, a stop, an owed completion) carries what is left; a download no tracker was told + // about yet has its first announce ahead of it. + if ((m_flags & mask_send & ~flag_send_update) || m_tracker_list->left_told() < 0) + return; + + if (static_cast(m_tracker_list->left_told()) == m_tracker_list->left_now()) + return; + + LT_LOG_TRACKER_EVENTS("left changed while no tracker was told : told:%" PRId64 " now:%" PRIu64, + m_tracker_list->left_told(), m_tracker_list->left_now()); + + send_update_event(); +} + +bool +TrackerController::send_owed_completed() { + bool busy_or_sent = false; + uint32_t next_timeout = ~uint32_t(); + + for (auto tracker : *m_tracker_list) { + if (!tracker.is_in_use()) + continue; + + // An announce already on its way: its answer is followed by the completion (receive_success). + if (tracker.is_requesting_not_scrape()) { + busy_or_sent = true; + continue; + } + + auto state = tracker.state(); + + if (state.failed_counter() != 0) { + auto wait = utils::next_timeout_seconds(state.failed_time_next(), this_thread::cached_seconds()); + + if (wait != 0) { + next_timeout = std::min(next_timeout, wait); + continue; + } + } + + m_tracker_list->send_event(tracker, tracker::TrackerState::EVENT_COMPLETED); + busy_or_sent = true; + } + + if (!busy_or_sent && next_timeout != ~uint32_t()) + update_timeout(next_timeout); + + LT_LOG_TRACKER_EVENTS("sending owed completed event : %s", busy_or_sent ? "requesting" : "waiting for a tracker"); + return busy_or_sent; +} + // Currently being used by send_event, fixme. void TrackerController::close() { @@ -421,6 +477,19 @@ TrackerController::do_timeout() { tracker::TrackerState::event_enum send_event = current_send_event(); + // A completion queued while the controller was off (the check after the last piece, pieces.hash.on_completion) + // is news for the trackers now. Left to the branches below it would wait for the next regular announce, up to + // the tracker's interval, and a stop or a manual announce in that time used to throw it away (rtorrent16 patch). + if (send_event == tracker::TrackerState::EVENT_COMPLETED && (m_flags & flag_completed_owed) && + m_tracker_list->has_in_use()) { + send_owed_completed(); + + if (m_slot_timeout) + m_slot_timeout(); + + return; + } + if ((m_flags & (flag_promiscuous_mode | flag_requesting))) { uint32_t next_timeout = ~uint32_t(); @@ -518,10 +587,38 @@ TrackerController::receive_success(const tracker::Tracker& tracker, TrackerContr if (!(m_flags & flag_active)) return m_slot_success(l); + bool completed_answered; + + tracker.lock_and_call_state([&](const tracker::TrackerState& state) { + completed_answered = state.latest_event() == tracker::TrackerState::EVENT_COMPLETED; + }); + // if () { m_flags &= ~(mask_send | flag_promiscuous_mode | flag_failure_mode); // } + if (completed_answered) + m_flags &= ~flag_completed_owed; + + // A stop, a start or a manual announce came between the completion and its answer: this answer was for that + // announce, and the completion goes now. Data that are no longer complete owe nothing; their next completion + // owes it again (rtorrent16 patch). + if ((m_flags & flag_completed_owed)) { + if (m_tracker_list->left_now() != 0) { + LT_LOG_TRACKER_EVENTS("owed completed event : dropped, the data are not complete", 0); + m_flags &= ~flag_completed_owed; + + } else { + LT_LOG_TRACKER_EVENTS("owed completed event : sending after the answer", 0); + m_flags |= flag_send_completed; + + if (send_owed_completed()) + return m_slot_success(l); + + m_flags &= ~flag_send_completed; + } + } + // If we still have active trackers, skip the timeout. // Calculate the next timeout according to a list of in-use diff --git a/src/tracker/tracker_controller.h b/src/tracker/tracker_controller.h index 1d36e8f..9c5eb3a 100644 --- a/src/tracker/tracker_controller.h +++ b/src/tracker/tracker_controller.h @@ -31,6 +31,10 @@ public: static constexpr int flag_failure_mode = 0x40; static constexpr int flag_promiscuous_mode = 0x80; + // A completion no tracker has answered yet. It outlives mask_send: a stop, a start or a plain update in between + // does not throw it away; the next answered announce of this download is followed by it (rtorrent16 patch). + static constexpr int flag_completed_owed = 0x100; + static constexpr int mask_send = flag_send_update | flag_send_start | flag_send_stop | flag_send_completed; static constexpr int enable_dont_reset_stats = 0x1; @@ -44,6 +48,7 @@ public: bool is_requesting() const { return m_flags & flag_requesting; } bool is_failure_mode() const { return m_flags & flag_failure_mode; } bool is_promiscuous_mode() const { return m_flags & flag_promiscuous_mode; } + bool is_completed_owed() const { return m_flags & flag_completed_owed; } bool is_timeout_queued() const; bool is_scrape_queued() const; @@ -63,6 +68,11 @@ public: void send_completed_event(); void send_update_event(); + // After a start that tells the trackers nothing (start_skip_tracker: the resume after a hash check): an update + // now when a check changed what is left since they were last told, so a torrent a check completed is a seed to + // them at once rather than at the next regular announce (rtorrent16 patch). + void send_update_if_left_changed(); + void close(); void enable(int enable_flags = 0); @@ -93,6 +103,10 @@ private: void do_timeout(); void do_scrape(); + // Sends the owed completion to every tracker in use that is free and not inside its failure backoff; true when + // one was sent or one is still answering (rtorrent16 patch). + bool send_owed_completed(); + void update_timeout(uint32_t seconds_to_next); inline tracker::TrackerState::event_enum current_send_event() const; diff --git a/src/tracker/tracker_list.cc b/src/tracker/tracker_list.cc index ffa4bc2..de3ec38 100644 --- a/src/tracker/tracker_list.cc +++ b/src/tracker/tracker_list.cc @@ -138,9 +138,16 @@ TrackerList::send_event(tracker::Tracker& tracker, tracker::TrackerState::event_ params.completed_adjusted = m_info->completed_adjusted(); params.download_left = m_info->slot_left()(); + m_left_told = params.download_left; + tracker_thread::manager()->send_event(tracker, params, event); } +uint64_t +TrackerList::left_now() const { + return m_info->slot_left()(); +} + void TrackerList::send_scrape(tracker::Tracker& tracker) { if (!tracker.is_valid()) diff --git a/src/tracker/tracker_list.h b/src/tracker/tracker_list.h index 60ad568..4a838a9 100644 --- a/src/tracker/tracker_list.h +++ b/src/tracker/tracker_list.h @@ -76,6 +76,11 @@ public: uint32_t key() const { return m_key; } int32_t numwant() const { return m_numwant; } + + // What the last announce of this download said was left (-1 before the first), and what is left now + // (rtorrent16 patch, TrackerController::send_update_if_left_changed). + int64_t left_told() const { return m_left_told; } + uint64_t left_now() const; void set_numwant(int32_t n) { m_numwant = n; } iterator find(const tracker::Tracker& tb) { return std::find(begin(), end(), tb); } @@ -128,6 +133,7 @@ private: // TODO: Key should be part of download static info. uint32_t m_key{0}; std::atomic m_numwant{-1}; + int64_t m_left_told{-1}; std::function m_slot_success; std::function m_slot_failed;