harn-vm 0.10.168

Async bytecode virtual machine for the Harn programming language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Process-wide registry of observers for committed session-metadata changes.
//!
//! File-backed store hooks and WAL polls invalidate the same current-row view.
//! One drain delivers accepted changes in order, outside the state lock.
//! Callbacks can change subscriptions or commit subsequent metadata without
//! taking that lock recursively. In-memory hooks publish their supplied rows
//! directly.

use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock, RwLockWriteGuard};

use harn_session_store::{SessionMeta, SharedSessionChangeObserver};

use super::session_wal_watch;

/// Process-wide observers notified when session metadata is committed.
///
/// Deliberately process-scoped, not thread-local like the redaction policy
/// beside it: a store handle is opened on whatever thread needs it, and an
/// update commits on whatever executor thread the caller happens to be on. A
/// thread-local sink would be installed on one thread and silently miss every
/// write made from another, which reads as "notifications do not work" rather
/// than as a wiring mistake.
///
/// A list rather than one slot, because more than one surface can care about
/// the same store and a single slot makes the second registration silently
/// evict the first.
static OBSERVERS: RwLock<Vec<(u64, SharedSessionChangeObserver)>> = RwLock::new(Vec::new());
static NEXT_SUBSCRIPTION: AtomicU64 = AtomicU64::new(1);
struct RememberedSession {
    database: PathBuf,
    session_id: String,
    meta: Option<SessionMeta>,
    published: bool,
    local_pending: bool,
    retry: bool,
}

enum Notification {
    Metadata(Box<SessionMeta>),
    #[cfg(test)]
    Barrier(std::sync::mpsc::Sender<()>),
}

struct NotificationState {
    sessions: Vec<RememberedSession>,
    pending: VecDeque<Notification>,
    draining: bool,
}

static NOTIFICATIONS: RwLock<NotificationState> = RwLock::new(NotificationState {
    sessions: Vec::new(),
    pending: VecDeque::new(),
    draining: false,
});

fn observers() -> RwLockWriteGuard<'static, Vec<(u64, SharedSessionChangeObserver)>> {
    OBSERVERS
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

fn notification_state() -> RwLockWriteGuard<'static, NotificationState> {
    NOTIFICATIONS
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// Live registration for [`subscribe`]. Unregisters on drop so
/// a surface that goes away cannot keep receiving, and so a test cannot leak a
/// sink into the next one sharing the process.
#[must_use = "dropping the subscription immediately unregisters the observer"]
pub struct SessionChangeSubscription {
    id: u64,
}

impl Drop for SessionChangeSubscription {
    fn drop(&mut self) {
        {
            let mut observers = observers();
            observers.retain(|(id, _)| *id != self.id);
            if observers.is_empty() {
                let mut state = notification_state();
                state.sessions.clear();
                state.pending.clear();
            }
        }
        session_wal_watch::sync_watchers();
    }
}

/// Register an observer of current session metadata from stores this VM opens
/// and from other processes writing the same file.
///
/// Every canonical store opened here carries the registered observers, so a
/// surface does not have to be handed the specific handle a writer happened to
/// use.
///
/// File-backed notifications project the current committed row. Intermediate
/// rows may coalesce when hooks arrive late or the WAL watcher sees several
/// commits together. An unchanged row is not published again. Accepted rows
/// are delivered in order. Direct in-memory hooks publish their supplied rows
/// without this file-backed coalescing.
pub fn subscribe(observer: SharedSessionChangeObserver) -> SessionChangeSubscription {
    let id = NEXT_SUBSCRIPTION.fetch_add(1, Ordering::Relaxed);
    observers().push((id, observer));
    session_wal_watch::sync_watchers();
    SessionChangeSubscription { id }
}

/// Claim a canonical store file so a live subscription can watch it.
///
/// The returned registration belongs to the store handle that opened the file.
/// While it lives the path is watchable; when the last handle for that path
/// drops, so does the watcher.
#[must_use = "dropping the registration stops the path being watched"]
pub(crate) fn watch_store(path: &Path) -> session_wal_watch::StoreWatchRegistration {
    let registration = session_wal_watch::register_store_path(path);
    session_wal_watch::sync_watchers();
    registration
}

pub(super) fn subscriber_count() -> usize {
    OBSERVERS
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .len()
}

#[derive(Clone, Copy)]
enum Observation {
    Baseline,
    Foreign,
    Local,
}

/// A watcher baseline must not replace a snapshot already accepted for delivery.
pub(super) fn seed_session(path: &Path, session_id: &str) {
    invalidate(path, session_id, Observation::Baseline);
}

/// Fans one committed change out to every live subscriber.
pub(super) fn dispatch(path: &Path, meta: &SessionMeta) {
    invalidate(path, &meta.id, Observation::Local);
}

/// A watcher's first encounter establishes its baseline, not an update.
pub(super) fn dispatch_foreign(path: &Path, session_id: &str) {
    invalidate(path, session_id, Observation::Foreign);
}

fn invalidate(path: &Path, session_id: &str, observation: Observation) {
    let mut state = notification_state();
    let index = state
        .sessions
        .iter()
        .position(|seen| seen.database == path && seen.session_id == session_id);
    if matches!(observation, Observation::Baseline)
        && index.is_some_and(|index| state.sessions[index].meta.is_some())
    {
        return;
    }
    let index = index.unwrap_or_else(|| {
        state.sessions.push(RememberedSession {
            database: path.to_owned(),
            session_id: session_id.to_owned(),
            meta: None,
            published: false,
            local_pending: false,
            retry: false,
        });
        state.sessions.len() - 1
    });
    // Keep unresolved hook authority in this same entry. The existing watcher
    // retries it even if SQLite's data_version does not move again.
    state.sessions[index].local_pending |= matches!(observation, Observation::Local);
    state.sessions[index].retry = true;
    // Resolve under the acceptance lock. Neither a delayed store hook nor a
    // WAL snapshot captured before a newer commit can put an older row back.
    let Ok(reader) = harn_session_store::wal_watch::open_watch_reader(path) else {
        return;
    };
    let Ok(meta) = harn_session_store::wal_watch::describe_session(&reader, session_id) else {
        return;
    };
    drop(reader);
    let previous = &mut state.sessions[index];
    let changed = previous.meta.as_ref().is_some_and(|seen| seen != &meta);
    let should_publish = changed || (previous.local_pending && !previous.published);
    previous.meta = Some(meta.clone());
    previous.published |= should_publish;
    previous.local_pending = false;
    previous.retry = false;
    if !should_publish {
        return;
    }
    state
        .pending
        .push_back(Notification::Metadata(Box::new(meta)));
    start_drain(state);
}

pub(super) fn retry_pending(path: &Path) {
    let sessions: Vec<String> = notification_state()
        .sessions
        .iter()
        .filter(|seen| seen.database == path && seen.retry)
        .map(|seen| seen.session_id.clone())
        .collect();
    for session_id in sessions {
        invalidate(path, &session_id, Observation::Foreign);
    }
}

fn start_drain(mut state: RwLockWriteGuard<'static, NotificationState>) {
    if state.draining {
        return;
    }
    state.draining = true;
    drop(state);
    let mut ownership = DrainOwnership(true);
    loop {
        let notification = {
            let mut state = notification_state();
            match state.pending.pop_front() {
                Some(notification) => notification,
                None => {
                    state.draining = false;
                    ownership.0 = false;
                    return;
                }
            }
        };
        match notification {
            Notification::Metadata(meta) => publish(&meta),
            #[cfg(test)]
            Notification::Barrier(ack) => {
                let _ = ack.send(());
            }
        }
    }
}

/// A panicking observer must not leave future changes without a drain owner.
struct DrainOwnership(bool);

impl Drop for DrainOwnership {
    fn drop(&mut self) {
        if self.0 {
            notification_state().draining = false;
        }
    }
}

#[cfg(test)]
pub(super) fn acknowledge_delivery(ack: std::sync::mpsc::Sender<()>) {
    let mut state = notification_state();
    state.pending.push_back(Notification::Barrier(ack));
    start_drain(state);
}

fn publish(meta: &SessionMeta) {
    let observers: Vec<SharedSessionChangeObserver> = OBSERVERS
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .iter()
        .map(|(_, observer)| Arc::clone(observer))
        .collect();
    // Copy out before dispatching: an observer is allowed to subscribe or
    // unsubscribe in response, which would deadlock against a held guard.
    for observer in observers {
        observer.session_updated(meta);
    }
}

struct SessionChangeFanout {
    database: Option<PathBuf>,
}

impl harn_session_store::SessionChangeObserver for SessionChangeFanout {
    fn session_updated(&self, meta: &SessionMeta) {
        if let Some(path) = self.database.as_deref() {
            dispatch(path, meta);
        } else {
            // In-memory imports have no file watcher and no shared database
            // identity, so they do not participate in file deduplication.
            publish(meta);
        }
    }
}

pub(crate) fn current_observer(database: Option<&Path>) -> Option<SharedSessionChangeObserver> {
    if subscriber_count() == 0 {
        return None;
    }
    Some(Arc::new(SessionChangeFanout {
        database: database.map(Path::to_owned),
    }))
}

#[cfg(test)]
pub(crate) mod test_support {
    use std::sync::OnceLock;

    use tokio::sync::{Mutex, MutexGuard};

    /// Exclusive access to the process-wide change bus for one test.
    ///
    /// The observer list, the remembered titles and the running watchers are
    /// one process singleton, the way the process environment is. Two cases
    /// that subscribe at the same time each receive the other's committed
    /// titles, because a subscriber is registered against the process and not
    /// against a store: the double-publish case then read a sibling's rename
    /// as its own republish (harn#7960). One lock, held for the life of the
    /// subscription, is what makes each case see only its own traffic.
    ///
    /// An async mutex rather than a `std` one because every case that needs it
    /// holds it across an await, which is exactly what a blocking guard must
    /// not do. It also has no poisoning to recover: the lock guards no
    /// invariant of its own, so a panicking holder leaves nothing behind.
    #[must_use = "the bus is shared for as long as the guard lives"]
    pub(crate) async fn exclusive_bus() -> MutexGuard<'static, ()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(())).lock().await
    }
}

#[cfg(test)]
mod tests {
    use std::sync::mpsc;

    use harn_clock::test_support::recv_within;
    use harn_session_store::{
        CreateSession, SessionStore, SqliteSessionStore, StoreHooks, UpdateSession,
    };

    use super::*;

    async fn observed_store(
        observer: SharedSessionChangeObserver,
    ) -> (
        tempfile::TempDir,
        SqliteSessionStore,
        SessionChangeSubscription,
        SessionMeta,
    ) {
        let root = tempfile::tempdir().expect("workspace");
        let canonical = crate::open_canonical_store(root.path()).expect("canonical store");
        let before = canonical
            .create(CreateSession {
                id: Some("ordered".into()),
                ..Default::default()
            })
            .await
            .expect("create session");
        let path = canonical.path().to_owned();
        drop(canonical);
        let subscription = subscribe(observer);
        let writer = SqliteSessionStore::open_with_hooks(
            &path,
            StoreHooks {
                change_observer: current_observer(Some(&path)),
                ..Default::default()
            },
        )
        .expect("writer using the canonical change fanout");
        (root, writer, subscription, before)
    }

    struct Recording(mpsc::Sender<Option<String>>);

    impl harn_session_store::SessionChangeObserver for Recording {
        fn session_updated(&self, meta: &SessionMeta) {
            self.0
                .send(meta.title.clone())
                .expect("notification receiver");
        }
    }

    #[tokio::test]
    async fn a_delayed_foreign_snapshot_cannot_replace_a_committed_title() {
        let _bus = test_support::exclusive_bus().await;
        let (tx, rx) = mpsc::channel();
        let (_root, writer, _subscription, before) = observed_store(Arc::new(Recording(tx))).await;
        assert_eq!(before.title, None, "the saved snapshot predates the rename");
        writer
            .update(
                "ordered",
                UpdateSession {
                    title: Some("committed title".into()),
                    ..Default::default()
                },
            )
            .await
            .expect("commit title");
        assert_eq!(
            recv_within("committed title", &rx).as_deref(),
            Some("committed title")
        );

        // A WAL reader can finish delivering a row read before the local hook.
        dispatch_foreign(writer.path(), &before.id);
        assert_eq!(
            rx.try_recv(),
            Err(mpsc::TryRecvError::Empty),
            "the delayed snapshot must not revert the client to a titleless row"
        );
    }

    #[tokio::test]
    async fn a_delayed_local_hook_coalesces_after_a_newer_title_is_delivered() {
        let _bus = test_support::exclusive_bus().await;
        let (tx, rx) = mpsc::channel();
        let (_root, writer, _subscription, _) = observed_store(Arc::new(Recording(tx))).await;
        let (entered, paused) = mpsc::channel();
        let (resume, barrier) = mpsc::channel();
        struct DelayedHook {
            entered: mpsc::Sender<()>,
            resume: std::sync::Mutex<mpsc::Receiver<()>>,
            fanout: SharedSessionChangeObserver,
        }
        impl harn_session_store::SessionChangeObserver for DelayedHook {
            fn session_updated(&self, meta: &SessionMeta) {
                self.entered
                    .send(())
                    .expect("first commit reached its hook");
                recv_within("release delayed local hook", &self.resume.lock().unwrap());
                self.fanout.session_updated(meta);
            }
        }
        let delayed = SqliteSessionStore::open_with_hooks(
            writer.path(),
            StoreHooks {
                change_observer: Some(Arc::new(DelayedHook {
                    entered,
                    resume: std::sync::Mutex::new(barrier),
                    fanout: writer.hooks().change_observer.as_ref().unwrap().clone(),
                })),
                ..Default::default()
            },
        )
        .expect("writer with a delayed post-commit hook");
        let runtime = tokio::runtime::Handle::current();
        let first = crate::runtime_stack::spawn(move || {
            runtime.block_on(delayed.update(
                "ordered",
                UpdateSession {
                    title: Some("first".into()),
                    ..Default::default()
                },
            ))
        });
        recv_within("first committed before the newer update", &paused);
        writer
            .update(
                "ordered",
                UpdateSession {
                    title: Some("second".into()),
                    ..Default::default()
                },
            )
            .await
            .expect("newer commit");
        assert_eq!(
            recv_within("newer title reached the subscriber", &rx).as_deref(),
            Some("second")
        );
        resume.send(()).expect("release the earlier hook");
        let first_row = first.join().expect("first writer").expect("first commit");
        assert_eq!(first_row.title.as_deref(), Some("first"));
        assert_eq!(
            rx.try_recv(),
            Err(mpsc::TryRecvError::Empty),
            "the late hook must publish neither the stale row nor a duplicate newer row"
        );
    }

    struct PausedFirst {
        entered: mpsc::Sender<()>,
        resume: std::sync::Mutex<mpsc::Receiver<()>>,
        delivered: mpsc::Sender<Option<String>>,
    }

    impl harn_session_store::SessionChangeObserver for PausedFirst {
        fn session_updated(&self, meta: &SessionMeta) {
            if meta.title.as_deref() == Some("first") {
                self.entered.send(()).expect("publication barrier");
                recv_within("resume first publication", &self.resume.lock().unwrap());
            }
            self.delivered
                .send(meta.title.clone())
                .expect("notification receiver");
        }
    }

    #[tokio::test]
    async fn committed_notifications_are_delivered_in_acceptance_order() {
        let _bus = test_support::exclusive_bus().await;
        let (entered, paused) = mpsc::channel();
        let (resume, barrier) = mpsc::channel();
        let (delivered, rx) = mpsc::channel();
        let (_root, writer, _subscription, _) = observed_store(Arc::new(PausedFirst {
            entered,
            resume: std::sync::Mutex::new(barrier),
            delivered,
        }))
        .await;
        let first_writer = writer.clone();
        let runtime = tokio::runtime::Handle::current();
        let first = crate::runtime_stack::spawn(move || {
            runtime.block_on(first_writer.update(
                "ordered",
                UpdateSession {
                    title: Some("first".into()),
                    ..Default::default()
                },
            ))
        });
        recv_within("first notification reached the observer", &paused);
        writer
            .update(
                "ordered",
                UpdateSession {
                    title: Some("second".into()),
                    ..Default::default()
                },
            )
            .await
            .expect("second commit");
        resume.send(()).expect("release first notification");
        first
            .join()
            .expect("first writer thread")
            .expect("first commit");

        assert_eq!(
            recv_within("first delivered title", &rx).as_deref(),
            Some("first")
        );
        assert_eq!(
            recv_within("second delivered title", &rx).as_deref(),
            Some("second")
        );
        assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty));
    }

    #[tokio::test]
    async fn a_watch_baseline_cannot_swallow_the_local_commit_notification() {
        let _bus = test_support::exclusive_bus().await;
        let (tx, rx) = mpsc::channel();
        let (_root, writer, _subscription, _) = observed_store(Arc::new(Recording(tx))).await;
        let fanout = writer.hooks().change_observer.as_ref().unwrap();
        // Commit without the hook, as if a watcher establishes its baseline
        // in the interval before the real writer invokes that hook.
        let delayed = SqliteSessionStore::open(writer.path()).unwrap();
        let committed = delayed
            .update(
                "ordered",
                UpdateSession {
                    title: Some("committed before baseline".into()),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        seed_session(writer.path(), "ordered");
        assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty));
        fanout.session_updated(&committed);
        assert_eq!(
            recv_within("local commit after baseline", &rx).as_deref(),
            Some("committed before baseline")
        );
        dispatch_foreign(writer.path(), "ordered");
        assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty));
    }

    struct ReplacingObserver {
        original: std::sync::Mutex<Option<SessionChangeSubscription>>,
        replacement: std::sync::Mutex<Option<SessionChangeSubscription>>,
        original_delivered: mpsc::Sender<()>,
        replacement_delivered: mpsc::Sender<Option<String>>,
    }

    impl harn_session_store::SessionChangeObserver for ReplacingObserver {
        fn session_updated(&self, _meta: &SessionMeta) {
            *self.replacement.lock().unwrap() = Some(subscribe(Arc::new(Recording(
                self.replacement_delivered.clone(),
            ))));
            drop(self.original.lock().unwrap().take());
            self.original_delivered.send(()).unwrap();
        }
    }

    #[tokio::test]
    async fn an_observer_can_replace_its_subscription_during_delivery() {
        let _bus = test_support::exclusive_bus().await;
        let (original_delivered, original_rx) = mpsc::channel();
        let (replacement_delivered, replacement_rx) = mpsc::channel();
        let observer = Arc::new(ReplacingObserver {
            original: std::sync::Mutex::new(None),
            replacement: std::sync::Mutex::new(None),
            original_delivered,
            replacement_delivered,
        });
        let (_root, writer, subscription, _) = observed_store(observer.clone()).await;
        *observer.original.lock().unwrap() = Some(subscription);
        for title in ["first", "second"] {
            writer
                .update(
                    "ordered",
                    UpdateSession {
                        title: Some(title.into()),
                        ..Default::default()
                    },
                )
                .await
                .expect("title commit");
            if title == "first" {
                recv_within("observer replaced its subscription", &original_rx);
                assert_eq!(replacement_rx.try_recv(), Err(mpsc::TryRecvError::Empty));
            }
        }
        assert_eq!(
            recv_within(
                "replacement observer receives the next commit",
                &replacement_rx
            )
            .as_deref(),
            Some("second")
        );
        assert_eq!(replacement_rx.try_recv(), Err(mpsc::TryRecvError::Empty));
    }

    #[tokio::test]
    async fn an_observer_can_commit_another_metadata_update_during_delivery() {
        let _bus = test_support::exclusive_bus().await;
        struct RenameAgain {
            writer: std::sync::Mutex<Option<SqliteSessionStore>>,
            delivered: mpsc::Sender<Option<String>>,
        }
        impl harn_session_store::SessionChangeObserver for RenameAgain {
            fn session_updated(&self, meta: &SessionMeta) {
                self.delivered.send(meta.title.clone()).unwrap();
                if meta.title.as_deref() == Some("first") {
                    let writer = self.writer.lock().unwrap().as_ref().unwrap().clone();
                    futures::executor::block_on(writer.update(
                        "ordered",
                        UpdateSession {
                            title: Some("second".into()),
                            ..Default::default()
                        },
                    ))
                    .expect("callback commits the next metadata update");
                }
            }
        }
        let (delivered, rx) = mpsc::channel();
        let observer = Arc::new(RenameAgain {
            writer: std::sync::Mutex::new(None),
            delivered,
        });
        let (_root, writer, _subscription, _) = observed_store(observer.clone()).await;
        *observer.writer.lock().unwrap() = Some(writer.clone());
        writer
            .update(
                "ordered",
                UpdateSession {
                    title: Some("first".into()),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(
            recv_within("first callback update", &rx).as_deref(),
            Some("first")
        );
        assert_eq!(
            recv_within("reentrant callback update", &rx).as_deref(),
            Some("second")
        );
        assert_eq!(
            writer.describe("ordered").await.unwrap().title.as_deref(),
            Some("second")
        );
        assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty));
    }
}