fix(queue): discard stale aria2 dispatches

This commit is contained in:
NimBold
2026-07-04 17:45:33 +03:30
parent f726b058f7
commit e5348d3213
5 changed files with 156 additions and 6 deletions
+8 -1
View File
@@ -2887,7 +2887,7 @@ async fn pause_download(
log::info!("pause_download called for id: {}", id);
let active_kind = state.queue_manager.active_kind(&id).await;
state.queue_manager.remove_from_pending(&id).await;
let removed_pending = state.queue_manager.remove_from_pending(&id).await;
let gid = state.queue_manager.aria2_gid_for_download(&id);
if let Some(gid) = gid.as_deref().filter(|gid| !gid.starts_with("native:")) {
@@ -2933,6 +2933,10 @@ async fn pause_download(
return Ok(());
}
if matches!(active_kind, Some(crate::queue::TaskKind::Aria2)) {
state.queue_manager.cancel_aria2_retries(&id).await;
}
let (tx, rx) = tokio::sync::oneshot::channel();
if matches!(active_kind, Some(crate::queue::TaskKind::Media)) {
state
@@ -2953,6 +2957,9 @@ async fn pause_download(
if !matches!(active_kind, Some(crate::queue::TaskKind::Media)) {
state.queue_manager.release_permit(&id).await;
}
if removed_pending || matches!(active_kind, Some(crate::queue::TaskKind::Aria2)) {
state.queue_manager.release_registered_id(&id).await;
}
use tauri::Emitter;
let _ = app_handle.emit(
"download-state",
+43 -4
View File
@@ -168,6 +168,10 @@ impl<R: tauri::Runtime> QueueManager<R> {
self.registered_ids.lock().await.remove(id);
}
async fn is_registered(&self, id: &str) -> bool {
self.registered_ids.lock().await.contains(id)
}
/// Enqueue a task. Checks the centralized `registered_ids` for deduplication.
pub async fn push(&self, task: QueuedTask) -> Result<(), String> {
let id = task.id.clone();
@@ -390,7 +394,30 @@ impl<R: tauri::Runtime> QueueManager<R> {
.insert(id.clone(), task.payload.clone());
self.aria2_retry_strikes.lock().await.remove(&id);
match self.spawner.add_uri(&id, &task.payload).await {
Ok(gid) => self.remember_gid(id.clone(), gid).await,
Ok(gid) => {
let cancelled = self.aria2_retry_cancelled.lock().await.contains(&id);
if cancelled || !self.is_registered(&id).await {
log::info!(
"aria2 dispatch cancellation [{}]: removing late gid {}",
id,
gid
);
if !gid.starts_with("native:") {
if let Err(error) = self.spawner.remove_uri(&gid).await {
log::warn!(
"aria2 dispatch cancellation [{}]: failed to remove late gid {}: {}",
id,
gid,
error
);
}
}
self.clear_aria2_retry_state(&id).await;
self.release_permit(&id).await;
return;
}
self.remember_gid(id.clone(), gid).await;
}
Err(error) => {
self.clear_aria2_retry_state(&id).await;
self.emit_failed(&id, error);
@@ -488,7 +515,9 @@ impl<R: tauri::Runtime> QueueManager<R> {
PendingOutcome::Error(error) => {
if error.to_ascii_lowercase().contains("checksum") {
log::warn!("Checksum error detected for {}, cleaning up assets", id);
if let Ok(primary_path) = crate::download_ownership::primary_path_for_id(&self.app_handle, id) {
if let Ok(primary_path) =
crate::download_ownership::primary_path_for_id(&self.app_handle, id)
{
if let Some(path) = primary_path.as_deref() {
let _ = crate::remove_download_assets(path, &self.app_handle).await;
}
@@ -957,7 +986,12 @@ async fn probe_bounded_range_support(
.redirect(reqwest::redirect::Policy::limited(5))
.timeout(std::time::Duration::from_secs(10));
if let Some(proxy) = payload.proxy.as_deref().map(str::trim).filter(|value| !value.is_empty()) {
if let Some(proxy) = payload
.proxy
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
{
if proxy.eq_ignore_ascii_case("none") {
builder = builder.no_proxy();
} else {
@@ -1131,7 +1165,12 @@ impl SidecarSpawner for ProductionSpawner {
if !header_list.is_empty() {
options.insert("header".to_string(), serde_json::json!(header_list));
}
if let Some(prox) = payload.proxy.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
if let Some(prox) = payload
.proxy
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
{
if prox.eq_ignore_ascii_case("none") {
options.insert("all-proxy".to_string(), serde_json::json!(""));
} else {