Keep podcast store work in sync handlers off Tokio workers

The device-sync stream handlers exported and merged podcast snapshot
sections and operations synchronously on a Tokio worker. While a large
feed refresh held the store (up to about half a second), the worker's
other tasks (feed checks, streams, art) waited. Run that work in
block_in_place on the multi-thread runtime, and say so in ARCHITECTURE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-19 21:52:05 +02:00
1 parent e0da2ddf2b
commit 268ea915da
3 files changed
+68 -8

No files matched your search

+6 -3
View File
@@ -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
+32 -5
View File
@@ -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<SyncOpWire>) -> Result<()> {
fn apply_received(&self, snapshot: SyncSnapshot, ops: Vec<SyncOpWire>) -> 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<SyncOpWire>,
) -> 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<T>(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::time::Instant> = std::sync::OnceLock::new();
START
+30
View File
@@ -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);
}