use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::{debug, info, warn};
use super::download_event_hooks::{DownloadEvent, DownloadEventHooks};
use super::engine_command::{
EngineCommand, EngineCommandReceiver, EngineCommandTryRecvError, TaskResult,
};
use super::task_spawner::{CommandDependencies, spawn_download_task};
use crate::dns::dns_cache::DnsCache;
use crate::error::{Aria2Error, RecoverableError};
use crate::filesystem::file_allocation_man::FileAllocationMan;
use crate::ftp::FtpConnectionPool;
use crate::network::ConnectionContext;
use crate::rate_limiter::RateLimiter;
use crate::request::request_group::{DownloadResultCode, DownloadStatus, GroupId, HaltReason};
use crate::request::request_group_man::RequestGroupMan;
use crate::selector::server_stat_man::ServerStatMan;
use crate::session::auto_save_coordinator::AutoSaveCoordinator;
use crate::util::rwlock_ext::RwLockRecover;
const MAX_STOPPED_RESULTS: usize = 1000;
const SHUTDOWN_WAIT: Duration = Duration::from_secs(5);
const FORCE_SHUTDOWN_WAIT: Duration = Duration::from_secs(1);
fn should_mark_failed_connection(error: &Aria2Error) -> bool {
matches!(
error,
Aria2Error::Network(_)
| Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure { .. } | RecoverableError::Timeout
)
)
}
async fn mark_failed_connection(
dns_cache: &tokio::sync::Mutex<DnsCache>,
error: &Aria2Error,
context: &ConnectionContext,
use_async_dns: bool,
) {
if !use_async_dns || !should_mark_failed_connection(error) {
return;
}
let mut dns = dns_cache.lock().await;
dns.mark_bad_context(context);
if !dns.has_good_address(&context.endpoint) {
dns.remove_cached(context.endpoint.hostname(), context.endpoint.port());
}
}
enum ProcessedTaskResult {
Success,
Failed(Aria2Error),
Cancelled,
}
pub struct EngineLoopContext {
pub group_man: Arc<RequestGroupMan>,
pub ftp_pool: Arc<FtpConnectionPool>,
pub dns_cache: Arc<tokio::sync::Mutex<DnsCache>>,
pub auto_save: Option<Arc<tokio::sync::Mutex<AutoSaveCoordinator>>>,
pub auto_save_dirty_signal: Option<Arc<std::sync::atomic::AtomicBool>>,
pub event_hooks: Arc<DownloadEventHooks>,
pub file_alloc_man: Arc<tokio::sync::RwLock<FileAllocationMan>>,
pub keep_alive: bool,
pub server_stat_man: Arc<ServerStatMan>,
pub server_stat_max_age: Option<Duration>,
pub server_stat_save_path: Option<PathBuf>,
pub server_stat_save_interval: Option<Duration>,
pub server_stat_next_save: Option<Instant>,
pub global_limiter: Option<RateLimiter>,
#[cfg(feature = "bittorrent")]
pub public_tracker_catalog:
Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>,
#[cfg(feature = "bittorrent")]
pub bt_registry: Arc<std::sync::RwLock<crate::engine::bt_registry::BtRegistry>>,
#[cfg(feature = "bittorrent")]
pub bt_listener: Arc<crate::engine::bt_peer_listener::BtPeerListenerManager>,
#[cfg(feature = "bittorrent")]
pub lpd_manager: Arc<crate::engine::lpd_manager::LpdManager>,
}
type CommandGeneration = u64;
struct RunningDownload {
_handle: JoinHandle<()>,
shutdown: Option<CancellationToken>,
generation: CommandGeneration,
last_activity: Instant,
timeout: Option<Duration>,
}
fn mark_session_dirty(ctx: &EngineLoopContext) {
if let Some(signal) = &ctx.auto_save_dirty_signal {
signal.store(true, std::sync::atomic::Ordering::Release);
}
}
pub async fn run_engine_loop(
ctx: EngineLoopContext,
cmd_rx: mpsc::UnboundedReceiver<EngineCommand>,
shutdown_rx: tokio::sync::oneshot::Receiver<()>,
) {
run_engine_loop_with_receiver(
ctx,
EngineCommandReceiver::from_unbounded(cmd_rx),
shutdown_rx,
)
.await;
}
pub(crate) async fn run_engine_loop_with_receiver(
mut ctx: EngineLoopContext,
mut cmd_rx: EngineCommandReceiver,
mut shutdown_rx: tokio::sync::oneshot::Receiver<()>,
) {
info!("Engine loop started (event-driven dispatch)");
let mut running_downloads: Vec<(GroupId, RunningDownload)> = Vec::new();
let mut completed_generations: HashSet<CommandGeneration> = HashSet::new();
let mut next_generation: CommandGeneration = 1;
let mut halt_requested = false;
let mut force_halt_requested = false;
let mut shutdown_received = false;
let mut command_closed = false;
let mut completion_closed = false;
let mut first_pass = true;
let mut schedule_on_next_pass = false;
let (completion_tx, mut completion_rx) =
mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
loop {
let commands_processed = process_engine_commands(
&mut ctx,
&mut cmd_rx,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
if first_pass {
promote_reserved_groups(
&ctx,
&mut running_downloads,
halt_requested,
force_halt_requested,
&mut next_generation,
&completion_tx,
);
}
let completions_processed = process_task_completions(
&ctx,
&mut completion_rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let demoted_gids = { ctx.group_man.remove_stopped_groups(Some(&ctx.event_hooks)) };
if !demoted_gids.is_empty() {
debug!("Demoted {} groups to stopped", demoted_gids.len());
mark_session_dirty(&ctx);
run_event_cleanup(&ctx).await;
}
let needs_follow_up_promotion = (!first_pass && commands_processed)
|| completions_processed
|| !demoted_gids.is_empty()
|| schedule_on_next_pass;
if needs_follow_up_promotion {
promote_reserved_groups(
&ctx,
&mut running_downloads,
halt_requested,
force_halt_requested,
&mut next_generation,
&completion_tx,
);
}
first_pass = false;
schedule_on_next_pass = false;
let all_done = ctx.group_man.download_finished() && running_downloads.is_empty();
let graceful_done = halt_requested && running_downloads.is_empty();
let force_done =
force_halt_requested && running_downloads.is_empty() && completion_rx.is_empty();
if force_done || graceful_done || (all_done && !ctx.keep_alive) {
if force_halt_requested {
info!("Force halt completed, shutting down engine");
} else if graceful_done {
info!("Graceful halt completed, engine shutting down");
} else {
info!("All downloads completed, engine shutting down");
}
break;
}
let maintenance_wait =
wait_for_deadline(next_maintenance_deadline(&ctx, &running_downloads).await);
tokio::pin!(maintenance_wait);
tokio::select! {
command = cmd_rx.recv(), if !command_closed => {
match command {
Ok(command) => {
let mut prefetched = PrefetchedEngineCommand {
first: Some(command),
receiver: &mut cmd_rx,
};
schedule_on_next_pass |= process_engine_commands(
&mut ctx,
&mut prefetched,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
}
Err(EngineCommandTryRecvError::Closed) => command_closed = true,
Err(EngineCommandTryRecvError::Empty) => unreachable!(
"async engine command receive cannot return empty"
),
}
}
completion = completion_rx.recv(), if !completion_closed => {
match completion {
Some(completion) => {
let mut prefetched = PrefetchedCompletion {
first: Some(completion),
receiver: &mut completion_rx,
};
schedule_on_next_pass |= process_task_completions(
&ctx,
&mut prefetched,
&mut running_downloads,
&mut completed_generations,
)
.await;
}
None => completion_closed = true,
}
}
_ = &mut maintenance_wait => {
run_deadline_maintenance(&mut ctx, &mut running_downloads).await;
}
Ok(_) = &mut shutdown_rx, if !shutdown_received => {
shutdown_received = true;
info!("Shutdown signal received");
ctx.group_man
.halt_all(crate::request::request_group::HaltReason::ShutdownSignal);
halt_requested = true;
}
}
}
on_end_of_run(&ctx, &mut running_downloads).await;
info!("Engine loop exited");
}
fn promote_reserved_groups(
ctx: &EngineLoopContext,
running_downloads: &mut Vec<(GroupId, RunningDownload)>,
halt_requested: bool,
force_halt_requested: bool,
next_generation: &mut CommandGeneration,
completion_tx: &mpsc::UnboundedSender<(GroupId, CommandGeneration, TaskResult)>,
) {
let promoted = if halt_requested || force_halt_requested {
Vec::new()
} else {
ctx.group_man.fill_from_reserver()
};
for group in &promoted {
let gid = group.recover().gid();
group.recover().clear_connection_contexts();
let generation = *next_generation;
*next_generation = next_generation.wrapping_add(1);
match spawn_download_task(
Arc::clone(group),
CommandDependencies {
dns_cache: Arc::clone(&ctx.dns_cache),
global_limiter: ctx.global_limiter.clone(),
#[cfg(feature = "bittorrent")]
public_tracker_catalog: Arc::clone(&ctx.public_tracker_catalog),
#[cfg(feature = "bittorrent")]
bt_registry: Arc::clone(&ctx.bt_registry),
#[cfg(feature = "bittorrent")]
bt_listener: Arc::clone(&ctx.bt_listener),
#[cfg(feature = "bittorrent")]
lpd_manager: Arc::clone(&ctx.lpd_manager),
},
generation,
completion_tx.clone(),
) {
Some((handle, shutdown_tx)) => {
let timeout = group.recover().timeout();
running_downloads.push((
gid,
RunningDownload {
_handle: handle,
shutdown: Some(shutdown_tx),
generation,
last_activity: Instant::now(),
timeout,
},
));
debug!(
gid = gid.value(),
"Spawned download task for promoted group"
);
ctx.event_hooks
.fire_event(DownloadEvent::Start, &group.recover());
}
None => {
warn!(
gid = gid.value(),
"Failed to spawn download task for promoted group"
);
ctx.group_man
.fail_spawned_group(gid, "Failed to spawn download task");
}
}
}
if !promoted.is_empty() {
debug!("Promoted {} groups from reserved to active", promoted.len());
mark_session_dirty(ctx);
}
}
trait EngineCommandQueue {
fn try_command(&mut self) -> Result<EngineCommand, EngineCommandTryRecvError>;
}
impl EngineCommandQueue for EngineCommandReceiver {
fn try_command(&mut self) -> Result<EngineCommand, EngineCommandTryRecvError> {
self.try_recv()
}
}
struct PrefetchedEngineCommand<'a, R> {
first: Option<EngineCommand>,
receiver: &'a mut R,
}
impl<R: EngineCommandQueue> EngineCommandQueue for PrefetchedEngineCommand<'_, R> {
fn try_command(&mut self) -> Result<EngineCommand, EngineCommandTryRecvError> {
self.first
.take()
.map_or_else(|| self.receiver.try_command(), Ok)
}
}
async fn process_engine_commands<R: EngineCommandQueue>(
ctx: &mut EngineLoopContext,
cmd_rx: &mut R,
running_downloads: &mut Vec<(GroupId, RunningDownload)>,
halt_requested: &mut bool,
force_halt_requested: &mut bool,
completion_tx: &mpsc::UnboundedSender<(GroupId, CommandGeneration, TaskResult)>,
) -> bool {
let mut processed = false;
while let Ok(cmd) = cmd_rx.try_command() {
processed = true;
mark_session_dirty(ctx);
match cmd {
EngineCommand::AddDownload { group } => {
let man = &ctx.group_man;
let gid = group.recover().gid();
man.add_group_arc(group);
info!(gid = gid.value(), "Added download to reserved queue");
}
#[cfg(all(feature = "metalink", feature = "bittorrent"))]
EngineCommand::AddMetalinkGraph { graph } => {
let man = &ctx.group_man;
match man.add_metalink_graph(graph) {
Ok((metadata_gid, payload_gid)) => info!(
metadata_gid = metadata_gid.value(),
payload_gid = payload_gid.value(),
"Added Metalink graph to reserved queue"
),
Err(error) => warn!(%error, "Failed to add Metalink graph"),
}
}
EngineCommand::RemoveDownload { gid } => {
let man = &ctx.group_man;
if let Err(e) = man.remove_group(gid) {
warn!(gid = gid.value(), error = %e, "Failed to remove download");
continue;
}
}
EngineCommand::ForceRemoveDownload { gid } => {
let man = &ctx.group_man;
if let Err(e) = man.force_remove_group(gid) {
warn!(gid = gid.value(), error = %e, "Failed to force-remove download");
continue;
}
}
EngineCommand::Pause { gid } => {
let man = &ctx.group_man;
let should_apply = man.find_group(gid).is_some_and(|group| {
matches!(
group.recover().status(),
DownloadStatus::Active | DownloadStatus::Waiting
)
});
if should_apply && let Err(e) = man.pause_group(gid) {
warn!(gid = gid.value(), error = %e, "Failed to pause download");
}
}
EngineCommand::ForcePause { gid } => {
let man = &ctx.group_man;
let should_apply = man.find_group(gid).is_some_and(|group| {
matches!(
group.recover().status(),
DownloadStatus::Active | DownloadStatus::Waiting
)
});
if should_apply && let Err(e) = man.force_pause_group(gid) {
warn!(gid = gid.value(), error = %e, "Failed to force-pause download");
}
}
EngineCommand::Unpause { gid } => {
let man = &ctx.group_man;
let should_apply = man
.find_group(gid)
.is_some_and(|group| group.recover().status().is_paused());
if should_apply && let Err(e) = man.unpause_group(gid) {
warn!(gid = gid.value(), error = %e, "Failed to unpause download");
}
}
EngineCommand::TaskCompleted { gid, result: _ } => {
debug!(
gid = gid.value(),
"Received TaskCompleted via engine command channel (external signal)"
);
}
EngineCommand::PauseAll => {
let man = &ctx.group_man;
man.pause_all();
}
EngineCommand::ForcePauseAll => {
let man = &ctx.group_man;
man.force_pause_all();
}
EngineCommand::UnpauseAll => {
let man = &ctx.group_man;
man.unpause_all();
}
EngineCommand::HaltAll { reason } => {
let man = &ctx.group_man;
man.halt_all(reason);
*halt_requested = true;
}
EngineCommand::ForceHaltAll { reason } => {
let man = &ctx.group_man;
man.force_halt_all(reason);
let removed = man.force_remove_reserved();
if removed > 0 {
mark_session_dirty(ctx);
}
*force_halt_requested = true;
cancel_running_file_allocations(ctx, running_downloads).await;
for (gid, running) in running_downloads.iter_mut() {
let generation = running.generation;
let completed = request_shutdown_and_wait(running, FORCE_SHUTDOWN_WAIT).await;
if !completed {
let _ = completion_tx.send((*gid, generation, TaskResult::Cancelled));
}
}
cancel_running_file_allocations(ctx, running_downloads).await;
running_downloads.clear();
}
EngineCommand::SetMaxConcurrent { max } => {
let man = &ctx.group_man;
let old_max = man.max_concurrent();
man.set_max_concurrent(max);
info!(
"Max concurrent downloads set to {}",
if max == 0 {
"unlimited".to_string()
} else {
max.to_string()
}
);
if max > 0 && (old_max == 0 || (max as usize) < old_max) {
let paused = man.reduce_to_limit();
if paused > 0 {
info!(
paused,
"Paused excess active downloads after max-concurrent reduction"
);
}
}
}
EngineCommand::SetGlobalRateLimit {
download_limit,
upload_limit,
} => {
let limiter = ctx
.global_limiter
.get_or_insert_with(RateLimiter::unlimited);
limiter.set_download_rate(download_limit);
limiter.set_upload_rate(upload_limit);
let man = &ctx.group_man;
man.set_global_speed_limit(download_limit, upload_limit);
info!(
download_limit = ?download_limit,
upload_limit = ?upload_limit,
"Global speed limits updated"
);
}
#[cfg(feature = "bittorrent")]
EngineCommand::SetPublicTrackerSources { sources } => {
let mut config = ctx.public_tracker_catalog.config().await;
config.sources = sources
.split([',', '\n'])
.map(str::trim)
.filter(|source| !source.is_empty())
.map(str::to_string)
.collect();
ctx.public_tracker_catalog.set_config(config).await;
info!("Public tracker sources updated at runtime");
}
#[cfg(feature = "bittorrent")]
EngineCommand::SetPublicTrackerUpdateInterval { seconds } => {
let mut config = ctx.public_tracker_catalog.config().await;
config.update_interval = Duration::from_secs(seconds.max(1));
ctx.public_tracker_catalog.set_config(config).await;
info!(seconds, "Public tracker update interval changed at runtime");
}
#[cfg(feature = "bittorrent")]
EngineCommand::SetPublicTrackersEnabled { enabled } => {
let mut config = ctx.public_tracker_catalog.config().await;
config.enabled = enabled;
ctx.public_tracker_catalog.set_config(config).await;
info!(
enabled,
"Public tracker catalog enabled state changed at runtime"
);
}
}
}
processed
}
fn map_error_code(error: &Aria2Error) -> DownloadResultCode {
match error {
Aria2Error::Recoverable(RecoverableError::Timeout) => DownloadResultCode::TimeOut,
Aria2Error::Checksum(_) => DownloadResultCode::ChecksumError,
Aria2Error::JsonParse(_) => DownloadResultCode::JsonParseError,
Aria2Error::MetalinkParse(_) => DownloadResultCode::MetalinkParseError,
Aria2Error::BencodeParse(_) => DownloadResultCode::BencodeParseError,
Aria2Error::BittorrentParse(_) => DownloadResultCode::BittorrentParseError,
Aria2Error::MagnetParse(_) => DownloadResultCode::MagnetParseError,
Aria2Error::Recoverable(RecoverableError::CannotResume) => DownloadResultCode::CannotResume,
Aria2Error::FtpProtocol(_) => DownloadResultCode::FtpProtocolError,
Aria2Error::HttpProtocol(_) => DownloadResultCode::HttpProtocolError,
Aria2Error::Recoverable(RecoverableError::FtpProtocolError { .. }) => {
DownloadResultCode::FtpProtocolError
}
Aria2Error::Recoverable(RecoverableError::HttpProtocolError { .. }) => {
DownloadResultCode::HttpProtocolError
}
Aria2Error::Recoverable(RecoverableError::ResourceNotFound) => {
DownloadResultCode::ResourceNotFound
}
Aria2Error::Recoverable(RecoverableError::MaxFileNotFound) => {
DownloadResultCode::MaxFileNotFound
}
Aria2Error::Recoverable(RecoverableError::HttpAuthFailed { .. }) => {
DownloadResultCode::HttpAuthFailed
}
Aria2Error::Recoverable(RecoverableError::HttpTooManyRedirects { .. }) => {
DownloadResultCode::HttpTooManyRedirects
}
Aria2Error::Recoverable(RecoverableError::ServerError { code })
if *code == 401 || *code == 407 =>
{
DownloadResultCode::HttpAuthFailed
}
Aria2Error::Recoverable(RecoverableError::ServerError { code })
if matches!(*code, 502..=504) =>
{
DownloadResultCode::HttpServiceUnavailable
}
Aria2Error::Recoverable(RecoverableError::ServerError { code }) if *code == 404 => {
DownloadResultCode::ResourceNotFound
}
Aria2Error::Recoverable(RecoverableError::ServerError { code }) if *code == 500 => {
DownloadResultCode::HttpProtocolError
}
Aria2Error::Recoverable(RecoverableError::ServerError { code }) if *code == 503 => {
DownloadResultCode::HttpServiceUnavailable
}
Aria2Error::Recoverable(RecoverableError::ServerError { .. }) => {
DownloadResultCode::NetworkProblem
}
Aria2Error::Network(_) => DownloadResultCode::NetworkProblem,
Aria2Error::FileOpen(_) => DownloadResultCode::FileOpenError,
Aria2Error::FileCreate(_) => DownloadResultCode::FileCreateError,
Aria2Error::FileIo(_) => DownloadResultCode::FileIoError,
Aria2Error::DirCreate(_) => DownloadResultCode::DirCreateError,
Aria2Error::NameResolve(_) => DownloadResultCode::NameResolveError,
Aria2Error::Io(_) => DownloadResultCode::FileIoError,
Aria2Error::InvalidArgument(_) => DownloadResultCode::OptionError,
Aria2Error::Parse(_) => DownloadResultCode::UnknownError,
Aria2Error::Fatal(crate::error::FatalError::Config(_)) => DownloadResultCode::OptionError,
Aria2Error::Fatal(crate::error::FatalError::DiskSpaceExhausted) => {
DownloadResultCode::NotEnoughDiskSpace
}
Aria2Error::Recoverable(_) => DownloadResultCode::NetworkProblem,
_ => DownloadResultCode::UnknownError,
}
}
trait CompletionQueue {
fn try_completion(&mut self) -> Result<(GroupId, CommandGeneration, TaskResult), ()>;
}
impl CompletionQueue for mpsc::Receiver<(GroupId, CommandGeneration, TaskResult)> {
fn try_completion(&mut self) -> Result<(GroupId, CommandGeneration, TaskResult), ()> {
mpsc::Receiver::try_recv(self).map_err(|_| ())
}
}
impl CompletionQueue for mpsc::UnboundedReceiver<(GroupId, CommandGeneration, TaskResult)> {
fn try_completion(&mut self) -> Result<(GroupId, CommandGeneration, TaskResult), ()> {
mpsc::UnboundedReceiver::try_recv(self).map_err(|_| ())
}
}
struct PrefetchedCompletion<'a, R> {
first: Option<(GroupId, CommandGeneration, TaskResult)>,
receiver: &'a mut R,
}
impl<R: CompletionQueue> CompletionQueue for PrefetchedCompletion<'_, R> {
fn try_completion(&mut self) -> Result<(GroupId, CommandGeneration, TaskResult), ()> {
self.first
.take()
.map_or_else(|| self.receiver.try_completion(), Ok)
}
}
async fn process_task_completions<R: CompletionQueue>(
ctx: &EngineLoopContext,
completion_rx: &mut R,
running_downloads: &mut Vec<(GroupId, RunningDownload)>,
completed_generations: &mut HashSet<CommandGeneration>,
) -> bool {
let mut processed = false;
while let Ok((gid, generation, result)) = completion_rx.try_completion() {
if !completed_generations.insert(generation) {
debug!(
gid = gid.value(),
generation, "Ignoring duplicate task completion"
);
continue;
}
processed = true;
mark_session_dirty(ctx);
running_downloads.retain(|(id, running)| *id != gid || running.generation != generation);
let man = &ctx.group_man;
if let Some(group) = man.find_group(gid) {
let prev = group.recover().dec_commands();
let last_command = prev == 1;
debug!(
gid = gid.value(),
prev, last_command, "Task completed, decremented num_commands"
);
let result = match result {
TaskResult::FailedWithContext {
error,
connection_context,
} => {
let use_async_dns = group.recover().options().async_dns;
mark_failed_connection(
&ctx.dns_cache,
&error,
&connection_context,
use_async_dns,
)
.await;
ProcessedTaskResult::Failed(error)
}
TaskResult::Success => ProcessedTaskResult::Success,
TaskResult::Failed(error) => ProcessedTaskResult::Failed(error),
TaskResult::Cancelled => ProcessedTaskResult::Cancelled,
};
match result {
ProcessedTaskResult::Success if last_command => {
let had_failure = group
.recover()
.command_failure
.load(std::sync::atomic::Ordering::Acquire);
if had_failure {
let message = group.recover().get_last_error_message();
let code = group.recover().get_last_error_code();
group.recover().mark_error_with_code(code, message);
} else {
let group_state = group.recover();
let was_pause_requested =
group_state.is_pause_requested() || group_state.is_paused_flag();
let halt_reason = group_state.get_halt_reason();
drop(group_state);
if matches!(halt_reason, HaltReason::None) && was_pause_requested {
group.recover().mark_paused();
} else {
match halt_reason {
crate::request::request_group::HaltReason::UserRequest => {
group.recover().mark_removed();
}
crate::request::request_group::HaltReason::Timeout => {
group.recover().mark_timeout();
}
crate::request::request_group::HaltReason::ShutdownSignal => {}
crate::request::request_group::HaltReason::None => {
group.recover_mut().mark_complete();
}
}
}
}
}
ProcessedTaskResult::Success => {
}
ProcessedTaskResult::Failed(e) if !last_command => {
let message = e.to_string();
let code = map_error_code(&e);
group.recover().set_last_error(code, message);
group
.recover()
.command_failure
.store(true, std::sync::atomic::Ordering::Release);
}
ProcessedTaskResult::Failed(e) => {
let group_state = group.recover();
let was_pause_requested =
group_state.is_pause_requested() || group_state.is_paused_flag();
let is_pause_error = matches!(
&e,
Aria2Error::DownloadFailed(msg) if msg == "Download paused"
);
let halt_reason = group_state.get_halt_reason();
drop(group_state);
match halt_reason {
HaltReason::UserRequest => group.recover().mark_removed(),
HaltReason::Timeout => group.recover().mark_timeout(),
HaltReason::ShutdownSignal => {
}
HaltReason::None if was_pause_requested => {
group.recover_mut().mark_paused();
}
HaltReason::None if is_pause_error => {
let status = group.recover().status();
if !matches!(
status,
DownloadStatus::Complete
| DownloadStatus::Error(_)
| DownloadStatus::Removed
) {
group.recover().mark_waiting();
}
}
HaltReason::None => group
.recover()
.mark_error_with_code(map_error_code(&e), e.to_string()),
}
group
.recover()
.command_failure
.store(false, std::sync::atomic::Ordering::Release);
}
ProcessedTaskResult::Cancelled => {
let group_state = group.recover();
let was_pause_requested =
group_state.is_pause_requested() || group_state.is_paused_flag();
let halt_reason = group_state.get_halt_reason();
drop(group_state);
if matches!(halt_reason, HaltReason::UserRequest | HaltReason::Timeout) {
if last_command {
match halt_reason {
HaltReason::UserRequest => group.recover().mark_removed(),
HaltReason::Timeout => group.recover().mark_timeout(),
_ => unreachable!(),
}
} else {
group
.recover()
.command_failure
.store(true, std::sync::atomic::Ordering::Release);
}
} else if was_pause_requested {
group.recover_mut().mark_paused();
} else if !last_command {
group
.recover()
.command_failure
.store(true, std::sync::atomic::Ordering::Release);
}
}
}
}
}
processed
}
async fn next_maintenance_deadline(
ctx: &EngineLoopContext,
running_downloads: &[(GroupId, RunningDownload)],
) -> Option<Instant> {
let timeout_deadline = running_downloads
.iter()
.filter_map(|(_, running)| {
running
.timeout
.and_then(|timeout| running.last_activity.checked_add(timeout))
})
.min();
let has_pending_downloads = !ctx.group_man.download_finished() || !running_downloads.is_empty();
let save_deadline = if let Some(auto_save) = &ctx.auto_save {
let save = auto_save.lock().await;
save.next_deadline(has_pending_downloads)
} else {
None
};
[timeout_deadline, save_deadline]
.into_iter()
.flatten()
.chain(ctx.server_stat_next_save)
.min()
}
async fn wait_for_deadline(deadline: Option<Instant>) {
match deadline {
Some(deadline) => {
tokio::time::sleep_until(tokio::time::Instant::from_std(deadline)).await;
}
None => std::future::pending::<()>().await,
}
}
async fn run_deadline_maintenance(
ctx: &mut EngineLoopContext,
running_downloads: &mut [(GroupId, RunningDownload)],
) {
let now = Instant::now();
let mut timed_out = Vec::new();
for (gid, rd) in running_downloads.iter_mut() {
let last_network_activity = ctx
.group_man
.find_group(*gid)
.map(|group| group.recover().last_network_activity());
if let Some(last_network_activity) = last_network_activity
&& last_network_activity > rd.last_activity
{
rd.last_activity = last_network_activity;
}
if let Some(timeout) = rd.timeout
&& now.duration_since(rd.last_activity) >= timeout
{
rd.timeout = None;
timed_out.push(*gid);
}
}
if !timed_out.is_empty() {
let man = &ctx.group_man;
for gid in timed_out {
if let Some(group) = man.get_group(gid) {
let request_context = group.recover().latest_connection_context();
let uris = group.recover().get_all_uris();
if let Some(uri) = uris.first()
&& let Ok(parsed) = reqwest::Url::parse(uri)
&& let Some(host) = parsed.host_str()
{
let protocol = parsed.scheme().to_ascii_lowercase();
ctx.server_stat_man
.mark_failure_with_protocol(host, &protocol, 408);
if let Some(context) = request_context
&& group.recover().options().async_dns
{
let mut dns = ctx.dns_cache.lock().await;
dns.mark_bad_context(&context);
if !dns.has_good_address(&context.endpoint) {
dns.remove_cached(context.endpoint.hostname(), context.endpoint.port());
}
}
}
}
if man.timeout_group(gid) {
warn!(
gid = gid.value(),
"Download task timed out, requesting graceful halt"
);
}
}
}
let has_pending_downloads = !ctx.group_man.download_finished() || !running_downloads.is_empty();
if let Some(ref auto_save) = ctx.auto_save {
let mut save = auto_save.lock().await;
save.run_due(has_pending_downloads).await;
}
if let (Some(path), Some(interval), Some(deadline)) = (
ctx.server_stat_save_path.as_ref(),
ctx.server_stat_save_interval,
ctx.server_stat_next_save,
) && now >= deadline
{
ctx.server_stat_next_save = Some(now + interval);
match ctx.server_stat_man.save_to_file_async(path).await {
Ok(count) => debug!(count, path = %path.display(), "Saved server statistics"),
Err(error) => warn!(%error, path = %path.display(), "Failed to save server statistics"),
}
}
}
async fn run_event_cleanup(ctx: &EngineLoopContext) {
let pruned = ctx.group_man.prune_stopped_results(MAX_STOPPED_RESULTS);
if pruned > 0 {
debug!("Pruned {} excess stopped results", pruned);
}
let stale_stats = ctx
.server_stat_max_age
.map(|max_age| ctx.server_stat_man.remove_stale(max_age))
.unwrap_or(0);
if stale_stats > 0 {
debug!("Removed {} stale server statistics", stale_stats);
}
let evicted = ctx.ftp_pool.cleanup_stale_count().await;
if evicted > 0 {
debug!("Evicted {} stale FTP connections", evicted);
}
}
async fn request_shutdown_and_wait(running: &mut RunningDownload, wait: Duration) -> bool {
if let Some(shutdown) = running.shutdown.take() {
shutdown.cancel();
}
let completed = match tokio::time::timeout(wait, &mut running._handle).await {
Ok(Ok(())) => true,
Ok(Err(error)) => {
warn!(%error, "Download task panicked during shutdown");
false
}
Err(_) => {
warn!("Download task shutdown timed out");
running._handle.abort();
false
}
};
if !completed {
running._handle.abort();
}
completed
}
async fn cancel_running_file_allocations(
ctx: &EngineLoopContext,
running_downloads: &[(GroupId, RunningDownload)],
) {
for (gid, _) in running_downloads {
let cancelled =
crate::filesystem::file_allocation_man::cancel_gid(&ctx.file_alloc_man, gid.value())
.await;
if cancelled > 0 {
debug!(
gid = gid.value(),
cancelled, "Cancelled file allocations for running task"
);
}
}
}
async fn on_end_of_run(
ctx: &EngineLoopContext,
running_downloads: &mut Vec<(GroupId, RunningDownload)>,
) {
info!("Engine loop cleanup: removing stopped groups and saving state");
let allocation_gids: Vec<u64> = running_downloads
.iter()
.map(|(gid, _)| gid.value())
.collect();
for gid in allocation_gids {
let cancelled =
crate::filesystem::file_allocation_man::cancel_gid(&ctx.file_alloc_man, gid).await;
if cancelled > 0 {
debug!(
gid,
cancelled, "Cancelled file allocations during engine cleanup"
);
}
}
let demoted = {
let man = &ctx.group_man;
man.remove_stopped_groups(Some(&ctx.event_hooks))
};
if !demoted.is_empty() {
info!("Demoted {} final groups on shutdown", demoted.len());
}
for (gid, mut rd) in running_downloads.drain(..) {
let completed = request_shutdown_and_wait(&mut rd, SHUTDOWN_WAIT).await;
debug!(
gid = gid.value(),
completed, "Finished running task shutdown"
);
}
#[cfg(feature = "bittorrent")]
{
ctx.lpd_manager.stop_background_announce();
ctx.lpd_manager.stop_receive_loop().await;
}
if let Some(ref auto_save) = ctx.auto_save {
let mut save = auto_save.lock().await;
save.force_save().await;
}
if let Some(path) = &ctx.server_stat_save_path {
match ctx.server_stat_man.save_to_file_async(path).await {
Ok(count) => {
debug!(count, path = %path.display(), "Saved server statistics on shutdown")
}
Err(error) => {
warn!(%error, path = %path.display(), "Failed to save server statistics on shutdown")
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::request::request_group::{DownloadOptions, DownloadStatus, HaltReason};
fn test_ctx(keep_alive: bool) -> EngineLoopContext {
EngineLoopContext {
group_man: Arc::new(RequestGroupMan::new()),
ftp_pool: Arc::new(FtpConnectionPool::new(1)),
dns_cache: Arc::new(tokio::sync::Mutex::new(DnsCache::new())),
auto_save: None,
auto_save_dirty_signal: None,
event_hooks: Arc::new(DownloadEventHooks::new()),
file_alloc_man: Arc::new(tokio::sync::RwLock::new(FileAllocationMan::new())),
keep_alive,
server_stat_man: ServerStatMan::shared().clone(),
server_stat_max_age: Some(Duration::from_secs(24 * 60 * 60)),
server_stat_save_path: None,
server_stat_save_interval: None,
server_stat_next_save: None,
global_limiter: None,
#[cfg(feature = "bittorrent")]
public_tracker_catalog: Arc::new(
aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList::new(),
),
#[cfg(feature = "bittorrent")]
bt_registry: Arc::new(std::sync::RwLock::new(
crate::engine::bt_registry::BtRegistry::new(),
)),
#[cfg(feature = "bittorrent")]
bt_listener: Arc::new(crate::engine::bt_peer_listener::BtPeerListenerManager::new()),
#[cfg(feature = "bittorrent")]
lpd_manager: Arc::new(crate::engine::lpd_manager::LpdManager::new()),
}
}
async fn run_until_exit(
ctx: EngineLoopContext,
cmd_rx: mpsc::UnboundedReceiver<EngineCommand>,
shutdown_rx: tokio::sync::oneshot::Receiver<()>,
budget: Duration,
) {
let loop_fut = run_engine_loop(ctx, cmd_rx, shutdown_rx);
tokio::time::timeout(budget, loop_fut)
.await
.expect("engine loop failed to terminate after halt");
}
#[tokio::test]
async fn graceful_halt_exits_even_in_keep_alive_mode() {
let (tx, rx) = mpsc::unbounded_channel();
let (_sd_tx, sd_rx) = tokio::sync::oneshot::channel();
tx.send(EngineCommand::HaltAll {
reason: HaltReason::ShutdownSignal,
})
.unwrap();
run_until_exit(test_ctx(true), rx, sd_rx, Duration::from_secs(5)).await;
}
#[tokio::test]
async fn force_halt_exits_in_keep_alive_mode() {
let (tx, rx) = mpsc::unbounded_channel();
let (_sd_tx, sd_rx) = tokio::sync::oneshot::channel();
tx.send(EngineCommand::ForceHaltAll {
reason: HaltReason::ShutdownSignal,
})
.unwrap();
run_until_exit(test_ctx(true), rx, sd_rx, Duration::from_secs(5)).await;
}
#[tokio::test]
async fn force_halt_removes_reserved_groups_before_exit() {
let ctx = test_ctx(true);
let gid = ctx
.group_man
.add_group(
vec!["http://example.com/queued-before-force-halt.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
let group_man = Arc::clone(&ctx.group_man);
let (tx, rx) = mpsc::unbounded_channel();
let (_sd_tx, sd_rx) = tokio::sync::oneshot::channel();
tx.send(EngineCommand::ForceHaltAll {
reason: HaltReason::ShutdownSignal,
})
.unwrap();
run_until_exit(ctx, rx, sd_rx, Duration::from_secs(5)).await;
assert_eq!(group_man.count(), 0, "force halt must remove queued groups");
assert!(group_man.find_group(gid).is_none());
assert_eq!(group_man.stopped_results_len(), 1);
let result = group_man
.find_stopped_result(&gid.to_hex_string())
.expect("queued force-halted group should have a stopped result");
assert_eq!(result.status, DownloadStatus::Removed);
assert_eq!(result.code, DownloadResultCode::Removed);
}
#[tokio::test]
async fn force_halt_wakes_file_allocation_waiter_before_protocol_timeout() {
use crate::filesystem::file_allocation::AllocationStrategy;
use crate::filesystem::file_allocation_man::enqueue_path;
use tokio::sync::oneshot;
let mut ctx = test_ctx(true);
let file_alloc_man = Arc::new(tokio::sync::RwLock::new(FileAllocationMan::new()));
ctx.file_alloc_man = Arc::clone(&file_alloc_man);
let gid = GroupId::new(703);
let path = std::env::temp_dir().join(format!(
"aria2-force-halt-allocation-{}",
std::process::id()
));
let (done_tx, done_rx) = oneshot::channel();
let allocation_task = tokio::spawn({
let file_alloc_man = Arc::clone(&file_alloc_man);
async move {
let result = enqueue_path(
&file_alloc_man,
&path,
4096,
AllocationStrategy::Trunc,
false,
gid.value(),
)
.await;
let _ = done_tx.send(result.is_err());
}
});
tokio::time::timeout(Duration::from_secs(1), async {
loop {
if file_alloc_man.read().await.is_queued_gid(gid.value()) {
break;
}
tokio::task::yield_now().await;
}
})
.await
.expect("allocation waiter should enter the queue");
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
cmd_tx
.send(EngineCommand::ForceHaltAll {
reason: HaltReason::ShutdownSignal,
})
.unwrap();
let mut cmd_rx = EngineCommandReceiver::from_unbounded(cmd_rx);
let (completion_tx, _completion_rx) = mpsc::unbounded_channel();
let mut running_downloads = vec![(
gid,
RunningDownload {
_handle: allocation_task,
shutdown: Some(CancellationToken::new()),
generation: 1,
last_activity: Instant::now(),
timeout: None,
},
)];
let mut halt_requested = false;
let mut force_halt_requested = false;
tokio::time::timeout(
Duration::from_millis(500),
process_engine_commands(
&mut ctx,
&mut cmd_rx,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
),
)
.await
.expect("force halt must wake allocation waiters before timeout");
assert!(
done_rx
.await
.expect("allocation task should report cancellation")
);
assert!(running_downloads.is_empty());
assert!(!file_alloc_man.read().await.is_queued_gid(gid.value()));
}
#[tokio::test]
async fn force_halt_accounts_for_aborted_running_task() {
let mut ctx = test_ctx(true);
let gid = ctx
.group_man
.add_group(
vec!["http://example.com/force-halt-running.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
let group = ctx.group_man.fill_from_reserver().remove(0);
group.recover().inc_commands();
let handle = tokio::spawn(async {
std::future::pending::<()>().await;
});
let mut running_downloads = vec![(
gid,
RunningDownload {
_handle: handle,
shutdown: Some(CancellationToken::new()),
generation: 41,
last_activity: Instant::now(),
timeout: None,
},
)];
let (completion_tx, mut completion_rx) = mpsc::unbounded_channel();
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
cmd_tx
.send(EngineCommand::ForceHaltAll {
reason: HaltReason::ShutdownSignal,
})
.unwrap();
let mut cmd_rx = EngineCommandReceiver::from_unbounded(cmd_rx);
let mut halt_requested = false;
let mut force_halt_requested = false;
process_engine_commands(
&mut ctx,
&mut cmd_rx,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
let mut completed_generations = HashSet::new();
let processed = process_task_completions(
&ctx,
&mut completion_rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
assert!(processed);
assert!(running_downloads.is_empty());
assert_eq!(completed_generations.get(&41), Some(&41));
assert_eq!(
ctx.group_man
.find_group(gid)
.unwrap()
.recover()
.num_commands(),
0
);
}
#[tokio::test]
async fn shutdown_signal_exits_in_keep_alive_mode() {
let (_tx, rx) = mpsc::unbounded_channel();
let (sd_tx, sd_rx) = tokio::sync::oneshot::channel();
sd_tx.send(()).unwrap();
run_until_exit(test_ctx(true), rx, sd_rx, Duration::from_secs(5)).await;
}
#[tokio::test]
async fn shutdown_signal_preserves_active_group_for_resume() {
let ctx = test_ctx(true);
let gid = ctx
.group_man
.add_group(
vec!["http://example.com/shutdown-resume.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
ctx.group_man.fill_from_reserver();
let group_man = Arc::clone(&ctx.group_man);
let (sd_tx, sd_rx) = tokio::sync::oneshot::channel();
sd_tx.send(()).unwrap();
let (_cmd_tx, cmd_rx) = mpsc::unbounded_channel();
run_engine_loop(ctx, cmd_rx, sd_rx).await;
let group = group_man
.find_group(gid)
.expect("group should remain visible");
assert_eq!(
group.recover().get_halt_reason(),
HaltReason::ShutdownSignal
);
assert_ne!(
group.recover().status(),
DownloadStatus::Removed,
"shutdown must not turn a resumable group into a user removal"
);
assert_eq!(
group.recover().create_download_result().code,
DownloadResultCode::InProgress
);
}
#[tokio::test]
async fn keep_alive_without_halt_does_not_exit() {
let (_tx, rx) = mpsc::unbounded_channel();
let (_sd_tx, sd_rx) = tokio::sync::oneshot::channel();
let loop_fut = run_engine_loop(test_ctx(true), rx, sd_rx);
assert!(
tokio::time::timeout(Duration::from_millis(200), loop_fut)
.await
.is_err(),
"keep-alive loop exited without a halt request"
);
}
#[tokio::test]
async fn idle_loop_exits_without_keep_alive() {
let (_tx, rx) = mpsc::unbounded_channel();
let (_sd_tx, sd_rx) = tokio::sync::oneshot::channel();
run_until_exit(test_ctx(false), rx, sd_rx, Duration::from_secs(5)).await;
}
#[tokio::test]
async fn server_stat_interval_persists_through_engine_maintenance() {
let dir = tempfile::tempdir().expect("server-stat output directory");
let path = dir.path().join("server-stat.json");
let mut ctx = test_ctx(false);
ctx.server_stat_man = Arc::new(ServerStatMan::new());
ctx.server_stat_man
.update_with_protocol("interval.example", "http", 1024, false);
ctx.server_stat_save_path = Some(path.clone());
ctx.server_stat_save_interval = Some(Duration::from_secs(60));
ctx.server_stat_next_save = Some(Instant::now() - Duration::from_secs(1));
run_deadline_maintenance(&mut ctx, &mut []).await;
let content = tokio::fs::read_to_string(&path)
.await
.expect("server-stat interval must write its configured output");
assert!(content.contains("interval.example"));
assert!(
ctx.server_stat_next_save
.is_some_and(|deadline| deadline > Instant::now()),
"server-stat interval must schedule its next save"
);
}
#[tokio::test]
async fn server_stat_timeout_controls_stale_cleanup() {
let dir = tempfile::tempdir().expect("server-stat input directory");
let path = dir.path().join("stale-server-stat.json");
let file = crate::selector::server_stat_man::ServerStatFile {
version: "1.0".to_string(),
saved_at: 1,
servers: vec![crate::selector::server_stat::ServerStatSnapshot {
host: "stale.example".to_string(),
protocol: "http".to_string(),
download_speed: 1,
single_connection_avg_speed: 1,
multi_connection_avg_speed: 0,
last_updated: 1,
status: 0,
counter: 1,
last_error_time: None,
last_error_code: 0,
consecutive_failures: 0,
}],
};
tokio::fs::write(&path, serde_json::to_vec(&file).unwrap())
.await
.expect("write stale server-stat fixture");
let mut ctx = test_ctx(false);
ctx.server_stat_man = Arc::new(ServerStatMan::new());
ctx.server_stat_man
.load_from_file_async(&path)
.await
.expect("load stale server-stat fixture");
ctx.server_stat_max_age = Some(Duration::from_secs(1));
assert_eq!(ctx.server_stat_man.count(), 1);
run_event_cleanup(&ctx).await;
assert_eq!(
ctx.server_stat_man.count(),
0,
"configured server-stat timeout must remove stale entries"
);
}
#[tokio::test]
async fn state_changing_command_marks_dirty_and_persists() {
use crate::request::request_group::{GroupId, RequestGroup};
use crate::session::auto_save_coordinator::AutoSaveCoordinator;
let man = Arc::new(RequestGroupMan::new());
let dir = std::env::temp_dir();
let path = dir.join(format!("test_engine_autosave_{}.sess", std::process::id()));
let _ = tokio::fs::remove_file(&path).await;
let auto_save = Arc::new(tokio::sync::Mutex::new(AutoSaveCoordinator::new(
man.clone(),
Some((path.clone(), Duration::from_millis(0))),
None,
)));
let auto_save_dirty_signal = auto_save.lock().await.dirty_signal();
let mut ctx = EngineLoopContext {
group_man: man,
ftp_pool: Arc::new(FtpConnectionPool::new(1)),
dns_cache: Arc::new(tokio::sync::Mutex::new(DnsCache::new())),
auto_save: Some(auto_save.clone()),
auto_save_dirty_signal: Some(auto_save_dirty_signal),
event_hooks: Arc::new(DownloadEventHooks::new()),
file_alloc_man: Arc::new(tokio::sync::RwLock::new(FileAllocationMan::new())),
keep_alive: false,
server_stat_man: Arc::new(ServerStatMan::new()),
server_stat_max_age: Some(Duration::from_secs(24 * 60 * 60)),
server_stat_save_path: None,
server_stat_save_interval: None,
server_stat_next_save: None,
global_limiter: None,
#[cfg(feature = "bittorrent")]
public_tracker_catalog: Arc::new(
aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList::new(),
),
#[cfg(feature = "bittorrent")]
bt_registry: Arc::new(std::sync::RwLock::new(
crate::engine::bt_registry::BtRegistry::new(),
)),
#[cfg(feature = "bittorrent")]
bt_listener: Arc::new(crate::engine::bt_peer_listener::BtPeerListenerManager::new()),
#[cfg(feature = "bittorrent")]
lpd_manager: Arc::new(crate::engine::lpd_manager::LpdManager::new()),
};
let (tx, rx) = mpsc::unbounded_channel();
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(42),
vec!["http://example.com/engine-autosave.bin".to_string()],
DownloadOptions::default(),
)));
tx.send(EngineCommand::AddDownload { group }).unwrap();
let mut rx = EngineCommandReceiver::from_unbounded(rx);
let mut halt_requested = false;
let mut force_halt_requested = false;
let (completion_tx, _completion_rx) = mpsc::unbounded_channel();
let auto_save_guard = auto_save.lock().await;
process_engine_commands(
&mut ctx,
&mut rx,
&mut Vec::new(),
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
drop(auto_save_guard);
assert!(
auto_save.lock().await.is_session_dirty(),
"AddDownload should mark the session dirty"
);
auto_save.lock().await.save_if_dirty().await;
assert!(path.exists(), "save_if_dirty should write the session file");
let content = tokio::fs::read_to_string(&path).await.unwrap();
assert!(
content.contains("http://example.com/engine-autosave.bin"),
"session file should contain the added URI"
);
let _ = tokio::fs::remove_file(&path).await;
}
#[tokio::test]
async fn global_rate_limit_command_updates_shared_limiter_and_snapshot() {
let mut ctx = test_ctx(false);
let (tx, rx) = mpsc::unbounded_channel();
tx.send(EngineCommand::SetGlobalRateLimit {
download_limit: Some(2_000),
upload_limit: Some(1_000),
})
.unwrap();
let mut rx = EngineCommandReceiver::from_unbounded(rx);
let mut running_downloads = Vec::new();
let mut halt_requested = false;
let mut force_halt_requested = false;
let (completion_tx, _completion_rx) = mpsc::unbounded_channel();
process_engine_commands(
&mut ctx,
&mut rx,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
let limiter = ctx
.global_limiter
.as_ref()
.expect("runtime updates should create the shared limiter")
.clone();
let config = limiter.config().await;
assert_eq!(config.download_rate(), Some(2_000));
assert_eq!(config.upload_rate(), Some(1_000));
let man = &ctx.group_man;
assert_eq!(man.global_download_limit(), Some(2_000));
assert_eq!(man.global_upload_limit(), Some(1_000));
}
#[tokio::test]
async fn paused_task_failure_keeps_group_paused() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
man.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap()
};
{
let man = &ctx.group_man;
let promoted = man.fill_from_reserver();
assert_eq!(promoted.len(), 1);
assert!(man.find_group(gid).is_some());
}
{
let man = &ctx.group_man;
man.pause_group(gid).unwrap();
let g = man.find_group(gid).unwrap();
assert!(g.recover().status().is_paused());
g.recover().inc_commands();
}
let (completion_tx, mut completion_rx) =
mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
completion_tx
.send((
gid,
1,
TaskResult::Failed(Aria2Error::DownloadFailed("Download paused".into())),
))
.unwrap();
let mut running_downloads = Vec::new();
let mut completed_generations = HashSet::new();
process_task_completions(
&ctx,
&mut completion_rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let status = {
let man = &ctx.group_man;
man.find_group(gid).unwrap().recover().status()
};
assert_eq!(
status,
DownloadStatus::Paused,
"a pause-induced task failure must keep the group Paused (resumable), not Error"
);
}
#[tokio::test]
async fn paused_task_success_keeps_group_paused() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
let gid = man
.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
man.fill_from_reserver();
man.pause_group(gid).unwrap();
man.find_group(gid).unwrap().recover().inc_commands();
gid
};
let (tx, mut rx) = mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
tx.send((gid, 1, TaskResult::Success)).unwrap();
let mut running_downloads = Vec::new();
let mut completed_generations = HashSet::new();
process_task_completions(
&ctx,
&mut rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let status = ctx.group_man.find_group(gid).unwrap().recover().status();
assert_eq!(
status,
DownloadStatus::Paused,
"a clean command completion must not make a paused group terminal"
);
}
#[tokio::test]
async fn failed_network_task_marks_only_the_observed_dns_peer_bad() {
let ctx = test_ctx(false);
let gid = ctx
.group_man
.add_group(
vec!["http://localhost/dns-peer.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
let group = ctx.group_man.find_group(gid).unwrap();
group.recover().inc_commands();
let cached = ctx
.dns_cache
.lock()
.await
.resolve("localhost", 80)
.await
.expect("localhost should resolve for the DNS cache fixture");
let observed = cached[0];
group
.recover()
.set_connection_context(ConnectionContext::new("localhost", 80, observed));
let (tx, mut rx) = mpsc::unbounded_channel();
tx.send((
gid,
1,
TaskResult::FailedWithContext {
error: Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: "connection reset".into(),
}),
connection_context: ConnectionContext::new("localhost", 80, observed),
},
))
.unwrap();
process_task_completions(&ctx, &mut rx, &mut Vec::new(), &mut HashSet::new()).await;
let remaining = ctx
.dns_cache
.lock()
.await
.resolve_no_network("localhost", 80);
if let Ok(addresses) = remaining {
assert!(
!addresses.contains(&observed),
"the peer that actually failed must not remain a good candidate"
);
}
}
#[tokio::test]
async fn async_dns_disabled_does_not_modify_shared_dns_cache_on_failure() {
let ctx = test_ctx(false);
let gid = ctx
.group_man
.add_group(
vec!["http://localhost/dns-peer-disabled.bin".to_string()],
DownloadOptions {
async_dns: false,
..DownloadOptions::default()
},
)
.unwrap();
let group = ctx.group_man.find_group(gid).unwrap();
group.recover().inc_commands();
let cached = ctx
.dns_cache
.lock()
.await
.resolve("localhost", 80)
.await
.expect("localhost should resolve for the DNS cache fixture");
let observed = cached[0];
group
.recover()
.set_connection_context(ConnectionContext::new("localhost", 80, observed));
let (tx, mut rx) = mpsc::unbounded_channel();
tx.send((
gid,
1,
TaskResult::FailedWithContext {
error: Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: "connection reset".into(),
}),
connection_context: ConnectionContext::new("localhost", 80, observed),
},
))
.unwrap();
process_task_completions(&ctx, &mut rx, &mut Vec::new(), &mut HashSet::new()).await;
let remaining = ctx
.dns_cache
.lock()
.await
.resolve_no_network("localhost", 80)
.expect("async-dns=false must leave the cached address available");
assert!(
remaining.contains(&observed),
"async-dns=false must not mark a failed connection bad in the shared cache"
);
}
#[tokio::test]
async fn user_removal_wins_over_paused_status_on_cancelled_task() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
let gid = man
.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
man.fill_from_reserver();
let group = man.find_group(gid).unwrap();
group.recover_mut().pause().unwrap();
group.recover().inc_commands();
group.recover().request_force_halt(HaltReason::UserRequest);
gid
};
let (completion_tx, mut completion_rx) =
mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
completion_tx.send((gid, 1, TaskResult::Cancelled)).unwrap();
let mut running_downloads = Vec::new();
let mut completed_generations = HashSet::new();
process_task_completions(
&ctx,
&mut completion_rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let status = ctx.group_man.find_group(gid).unwrap().recover().status();
assert_eq!(status, DownloadStatus::Removed);
}
#[tokio::test]
async fn duplicate_completion_decrements_command_once() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
let gid = man
.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
man.fill_from_reserver();
man.find_group(gid).unwrap().recover().inc_commands();
gid
};
let (tx, mut rx) = mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
tx.send((
gid,
1,
TaskResult::Failed(Aria2Error::Network("failed".into())),
))
.unwrap();
tx.send((
gid,
1,
TaskResult::Failed(Aria2Error::Network("duplicate".into())),
))
.unwrap();
let mut running_downloads: Vec<(GroupId, RunningDownload)> = Vec::new();
let mut completed_generations: HashSet<CommandGeneration> = HashSet::new();
process_task_completions(
&ctx,
&mut rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let man = &ctx.group_man;
let group = man.find_group(gid).unwrap();
assert_eq!(group.recover().num_commands(), 0);
assert_eq!(completed_generations.len(), 1);
}
#[tokio::test]
async fn same_gid_commands_have_independent_completion_generations() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
let gid = man
.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
man.fill_from_reserver();
let group = man.find_group(gid).unwrap();
group.recover().inc_commands();
group.recover().inc_commands();
gid
};
let (tx, mut rx) = mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
tx.send((
gid,
1,
TaskResult::Failed(Aria2Error::Network("first command".into())),
))
.unwrap();
tx.send((
gid,
2,
TaskResult::Failed(Aria2Error::Network("second command".into())),
))
.unwrap();
let mut running_downloads = Vec::new();
let mut completed_generations = HashSet::new();
process_task_completions(
&ctx,
&mut rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let man = &ctx.group_man;
assert_eq!(man.find_group(gid).unwrap().recover().num_commands(), 0);
assert_eq!(completed_generations.len(), 2);
}
#[tokio::test]
async fn non_final_command_failure_waits_for_final_completion() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
let gid = man
.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap();
man.fill_from_reserver();
let group = man.find_group(gid).unwrap();
group.recover().inc_commands();
group.recover().inc_commands();
gid
};
let (tx, mut rx) = mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
tx.send((
gid,
1,
TaskResult::Failed(Aria2Error::Network("first command failed".into())),
))
.unwrap();
let mut running_downloads = Vec::new();
let mut completed_generations = HashSet::new();
process_task_completions(
&ctx,
&mut rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
{
let man = &ctx.group_man;
let group = man.find_group(gid).unwrap();
assert_eq!(group.recover().num_commands(), 1);
assert!(matches!(group.recover().status(), DownloadStatus::Active));
}
tx.send((gid, 2, TaskResult::Success)).unwrap();
process_task_completions(
&ctx,
&mut rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let man = &ctx.group_man;
let group = man.find_group(gid).unwrap();
assert_eq!(group.recover().num_commands(), 0);
assert!(matches!(group.recover().status(), DownloadStatus::Error(_)));
}
#[test]
fn error_code_mapping_preserves_aria2_semantics() {
assert_eq!(
map_error_code(&Aria2Error::Recoverable(RecoverableError::Timeout)),
DownloadResultCode::TimeOut
);
assert_eq!(
map_error_code(&Aria2Error::Recoverable(RecoverableError::CannotResume)),
DownloadResultCode::CannotResume
);
assert_eq!(
map_error_code(&Aria2Error::Recoverable(RecoverableError::ServerError {
code: 404
})),
DownloadResultCode::ResourceNotFound
);
assert_eq!(
map_error_code(&Aria2Error::Recoverable(RecoverableError::ServerError {
code: 500
})),
DownloadResultCode::HttpProtocolError
);
for code in [502, 503, 504] {
assert_eq!(
map_error_code(&Aria2Error::Recoverable(RecoverableError::ServerError {
code
})),
DownloadResultCode::HttpServiceUnavailable
);
}
for code in [401, 407] {
assert_eq!(
map_error_code(&Aria2Error::Recoverable(RecoverableError::ServerError {
code
})),
DownloadResultCode::HttpAuthFailed
);
}
assert_eq!(
map_error_code(&Aria2Error::Recoverable(
RecoverableError::HttpTooManyRedirects { count: 20 }
)),
DownloadResultCode::HttpTooManyRedirects
);
assert_eq!(
map_error_code(&Aria2Error::Checksum("bad digest".into())),
DownloadResultCode::ChecksumError
);
assert_eq!(
map_error_code(&Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: "550".into(),
}
)),
DownloadResultCode::FtpProtocolError
);
assert_eq!(
map_error_code(&Aria2Error::Io("disk write".into())),
DownloadResultCode::FileIoError
);
assert_eq!(
map_error_code(&Aria2Error::Fatal(
crate::error::FatalError::DiskSpaceExhausted
)),
DownloadResultCode::NotEnoughDiskSpace
);
assert_eq!(
map_error_code(&Aria2Error::HttpProtocol("bad status".into())),
DownloadResultCode::HttpProtocolError
);
assert_eq!(
map_error_code(&Aria2Error::FtpProtocol("bad PASV".into())),
DownloadResultCode::FtpProtocolError
);
assert_eq!(
map_error_code(&Aria2Error::DirCreate("permission denied".into())),
DownloadResultCode::DirCreateError
);
assert_eq!(
map_error_code(&Aria2Error::FileOpen("cannot open".into())),
DownloadResultCode::FileOpenError
);
}
#[tokio::test]
async fn genuine_task_failure_still_marks_error() {
let ctx = test_ctx(false);
let gid = {
let man = &ctx.group_man;
man.add_group(
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)
.unwrap()
};
{
let man = &ctx.group_man;
let promoted = man.fill_from_reserver();
assert_eq!(promoted.len(), 1);
let g = man.find_group(gid).unwrap();
g.recover().inc_commands();
}
let (completion_tx, mut completion_rx) =
mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
completion_tx
.send((
gid,
1,
TaskResult::Failed(Aria2Error::Network("connection refused".into())),
))
.unwrap();
let mut running_downloads = Vec::new();
let mut completed_generations = HashSet::new();
process_task_completions(
&ctx,
&mut completion_rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let status = {
let man = &ctx.group_man;
man.find_group(gid).unwrap().recover().status()
};
assert!(
matches!(status, DownloadStatus::Error(_)),
"a genuine network failure must still record an Error, got {:?}",
status
);
}
#[tokio::test]
async fn engine_cleanup_cancels_only_its_running_gids() {
use crate::filesystem::file_allocation::AllocationStrategy;
use crate::filesystem::file_allocation_man::{FileAllocationEntry, FileAllocationProtocol};
use tokio::sync::oneshot;
let mut ctx = test_ctx(false);
let file_alloc_man = Arc::new(tokio::sync::RwLock::new(FileAllocationMan::new()));
let (target_tx, target_rx) = oneshot::channel();
let (other_tx, mut other_rx) = oneshot::channel();
{
let mut man = file_alloc_man.write().await;
man.push_entry(FileAllocationEntry::single(
701,
std::path::PathBuf::from("/tmp/engine-cleanup-target"),
100,
AllocationStrategy::Trunc,
false,
FileAllocationProtocol::Http,
target_tx,
));
man.push_entry(FileAllocationEntry::single(
702,
std::path::PathBuf::from("/tmp/engine-cleanup-other"),
100,
AllocationStrategy::Trunc,
false,
FileAllocationProtocol::Http,
other_tx,
));
}
ctx.file_alloc_man = Arc::clone(&file_alloc_man);
let handle = tokio::spawn(async {});
let mut running_downloads = vec![(
GroupId::new(701),
RunningDownload {
_handle: handle,
shutdown: Some(CancellationToken::new()),
generation: 1,
last_activity: Instant::now(),
timeout: None,
},
)];
on_end_of_run(&ctx, &mut running_downloads).await;
assert!(target_rx.await.unwrap().is_err());
assert!(other_rx.try_recv().is_err());
file_alloc_man.write().await.cancel_all();
assert!(other_rx.await.unwrap().is_err());
}
}