diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 29b1a1b..0621b9a 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -333,9 +333,12 @@ The TUI owns what the contract leaves to clients: subscriptions are due; feeds are checked at most a few at a time, a success waits an hour, and failures back off exponentially (honouring `Retry-After`). Errors never clear the stored catalog. -- **Runtime boundary.** Every store call, fetch and parse runs on the blocking - pool or as a task and reports back through `AppEvent`s; `update::podcasts` - holds the pure state transitions. Store open failures (newer schema, +- **Runtime boundary.** Every store call, fetch and parse of the app runs on + the blocking pool or as a task and reports back through `AppEvent`s; + `update::podcasts` holds the pure state transitions. The device-sync + stream handlers call the store (snapshot export and merges) inside + `block_in_place`, so a store busy with a large feed refresh does not hold + a Tokio worker's other tasks; they are never called on the UI thread. Store open failures (newer schema, foreign file) disable only the Podcasts tab and leave the file untouched. - **Sync log.** Local actions (subscribe, unsubscribe, mark played, progress) draw their operation stamps from the device-sync log through diff --git a/src/devices.rs b/src/devices.rs index 56e52a2..0e964f8 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -2328,7 +2328,17 @@ impl DeviceSync { /// the podcast snapshot sections. Podcast merges are order-independent; /// operations go first so a changed progress register reaches the UI as /// such (it is shown in place) instead of as a generic change. - fn apply_received(&self, mut snapshot: SyncSnapshot, ops: Vec) -> Result<()> { + fn apply_received(&self, snapshot: SyncSnapshot, ops: Vec) -> Result<()> { + // Called from the async stream handlers; podcast store merges can + // wait on a feed refresh holding the store. + off_worker(|| self.apply_received_blocking(snapshot, ops)) + } + + fn apply_received_blocking( + &self, + mut snapshot: SyncSnapshot, + ops: Vec, + ) -> Result<()> { let podcast = SyncSnapshot { podcast_subscriptions: std::mem::take(&mut snapshot.podcast_subscriptions), podcast_shares: std::mem::take(&mut snapshot.podcast_shares), @@ -2593,10 +2603,12 @@ impl DeviceSync { let mut snapshot = self.snapshot()?; if music_dht::device_sync::supports_podcasts(peer_version) && let Some(store) = self.podcast_store() - && let Err(err) = store.fill_snapshot( - &mut snapshot, - music_dht::podcast::MAX_PODCAST_SNAPSHOT_BYTES, - ) + && let Err(err) = off_worker(|| { + store.fill_snapshot( + &mut snapshot, + music_dht::podcast::MAX_PODCAST_SNAPSHOT_BYTES, + ) + }) { // Music sync goes on; podcast state comes with a later sync. tracing::warn!("podcast snapshot export failed: {err:#}"); @@ -4213,6 +4225,21 @@ mod wire_compat_tests; #[cfg(test)] mod podcast_sync_tests; +/// Runs podcast store work from an async device-sync handler without +/// stalling a Tokio worker: the store may be busy with a feed refresh (one +/// transaction, up to about half a second for a very large feed). On the +/// multi-thread runtime the worker hands its other tasks over while `work` +/// runs (`block_in_place`); elsewhere (blocking pool, tests without a +/// runtime, a current-thread runtime) it simply runs. +fn off_worker(work: impl FnOnce() -> T) -> T { + match tokio::runtime::Handle::try_current() { + Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => { + tokio::task::block_in_place(work) + } + _ => work(), + } +} + fn playback_clock() -> u64 { static START: std::sync::OnceLock = std::sync::OnceLock::new(); START diff --git a/src/devices/podcast_sync_tests.rs b/src/devices/podcast_sync_tests.rs index e10f386..a106b1a 100644 --- a/src/devices/podcast_sync_tests.rs +++ b/src/devices/podcast_sync_tests.rs @@ -950,3 +950,33 @@ fn podcast_payloads_are_not_written_to_the_debug_log() { // Music payloads are still logged in full. assert!(logged.contains("\"title\":\"Song\""), "{logged}"); } + +/// Podcast store work done by the sync handlers does not hold a Tokio +/// worker: another task keeps running meanwhile, even with one worker. +#[test] +fn podcast_store_work_in_sync_handlers_leaves_the_worker_free() { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .enable_time() + .build() + .unwrap(); + runtime.block_on(async { + let started = std::time::Instant::now(); + let busy = tokio::spawn(async { + off_worker(|| std::thread::sleep(Duration::from_millis(600))); + std::time::Instant::now() + }); + tokio::time::sleep(Duration::from_millis(50)).await; + let other = tokio::spawn(async { std::time::Instant::now() }); + let other_ran = other.await.unwrap(); + let busy_done = busy.await.unwrap(); + assert!(other_ran < busy_done, "the other task waited for the store"); + assert!(other_ran.duration_since(started) < Duration::from_millis(400)); + }); + // Without a runtime (or on the blocking pool) it simply runs. + assert_eq!(off_worker(|| 7), 7); + let current = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + assert_eq!(current.block_on(async { off_worker(|| 8) }), 8); +}