bg bugfix

This commit is contained in:
yves committed 2026-11-20 23:48:30 +01:00
1 parent 7eace57528
commit c30fddb1ef
14 files changed
+3809 -12

No files matched your search

+15 -4
View File
@@ -93,10 +93,12 @@ impl ClientHandle {
}
}
/// Wait for `d`, or until asked to stop. Returns `true` when stopping.
async fn pause(d: Duration, stop: &mut watch::Receiver<bool>) -> bool {
/// Wait for `d`, until the client state changes, or until asked to stop. Returns
/// `true` when stopping.
async fn pause(d: Duration, stop: &mut watch::Receiver<bool>, changes: &mut watch::Receiver<u64>) -> bool {
tokio::select! {
_ = tokio::time::sleep(d) => *stop.borrow(),
_ = changes.changed() => *stop.borrow(),
_ = stop.changed() => true,
}
}
@@ -104,6 +106,7 @@ async fn pause(d: Duration, stop: &mut watch::Receiver<bool>) -> bool {
async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop: watch::Receiver<bool>) {
let mut wait = opts.long_poll;
let mut backoff = opts.backoff;
let mut changes = client.lock().await.changes.subscribe();
while !*stop.borrow() {
// 1. Due deposits (and expiries).
let (plan, transport) = {
@@ -130,6 +133,8 @@ async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop:
// for the next queued deposit to be retried.
let plan = {
let mut c = client.lock().await;
// Everything up to here (including our own saves) is reflected in the plan.
changes.borrow_and_update();
let w = match c.next_retry_in() {
Some(d) => wait.min(d.max(Duration::from_secs(1))),
None => wait,
@@ -139,13 +144,19 @@ async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop:
if plan.is_empty() {
// No contact yet: just keep the queue moving.
let d = client.lock().await.next_retry_in().unwrap_or(opts.long_poll);
if pause(d.clamp(Duration::from_secs(1), opts.long_poll), &mut stop).await {
if pause(d.clamp(Duration::from_secs(1), opts.long_poll), &mut stop, &mut changes).await {
break;
}
continue;
}
let results = tokio::select! {
r = execute_pull(&transport, &plan) => r,
// The state changed under us (new contact, unlocked space, message to send):
// restart with an up-to-date request. Abandoned acks are simply resent.
_ = changes.changed() => {
wait = Duration::ZERO;
continue;
}
_ = stop.changed() => break,
};
@@ -172,7 +183,7 @@ async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop:
Err(e) => {
drop(c);
tracing::warn!("background pull failed: {e}; retrying in {backoff:?}");
if pause(backoff, &mut stop).await {
if pause(backoff, &mut stop, &mut changes).await {
break;
}
backoff = (backoff * 2).min(opts.max_backoff);