jmap-server 0.6.0

Backend-agnostic JMAP server framework (RFC 8620): parsing, ResultReference resolution, and Dispatcher
Documentation
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
//! [`ScopedState`]: shared in-memory backend state that can open a
//! draft-ietf-jmap-conditional-02 [`SetScope`] (docs/design/conditional.md
//! §7).
//!
//! **Demonstration quality.** This is what the in-repo memory reference
//! backends use to implement [`ConditionalBackend::begin_set`]: one gate
//! per backend (or shared between several), a whole-store clone as the
//! undo log, and final-state checks run at commit. It is correct, but it
//! serializes every scoped method behind one lock and copies the whole
//! store per atomic call. A production backend uses its storage engine's
//! transactions instead.
//!
//! [`ConditionalBackend::begin_set`]: crate::ConditionalBackend::begin_set

use std::fmt;
use std::future::ready;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use tokio::sync::OwnedMutexGuard;

use crate::conditional::{
    CommitError, CommitFuture, DeferredFailure, RollbackFuture, SetMode, SetScope, SetScopeId,
    SetScopeRequest, current_set_scope,
};

/// The lock a [`ScopedState`] waits on while another task's [`SetScope`]
/// is open.
///
/// Each [`ScopedState::new`] makes its own gate. Backends that call each
/// other inside one method (a binding crate over two stores, the testjig)
/// share one through [`ScopedState::with_gate`]: with separate gates, two
/// scopes opened in opposite orders deadlock. A task running in the scope
/// that holds the gate passes it, on every state that shares it.
pub struct ScopeGate {
    /// Held by the open scope for its whole life.
    lock: Arc<tokio::sync::Mutex<()>>,
    /// The open scope and its mode, while one is open.
    owner: Mutex<Option<(SetScopeId, SetMode)>>,
}

impl ScopeGate {
    /// A gate with no scope open.
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            lock: Arc::new(tokio::sync::Mutex::new(())),
            owner: Mutex::new(None),
        })
    }

    /// The open scope and its mode, if any.
    fn owner(&self) -> Option<(SetScopeId, SetMode)> {
        *self.owner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn set_owner(&self, owner: Option<(SetScopeId, SetMode)>) {
        *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = owner;
    }

    /// The mode of the open scope, when the running task is in it.
    fn owned_by_current(&self) -> Option<SetMode> {
        let current = current_set_scope()?;
        self.owner()
            .and_then(|(id, mode)| (id == current).then_some(mode))
    }
}

impl fmt::Debug for ScopeGate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ScopeGate")
            .field("owner", &self.owner())
            .finish_non_exhaustive()
    }
}

/// Access to a [`ScopedState`]'s value, from [`ScopedState::lock`] or
/// [`ScopedState::lock_unscoped`].
///
/// A `std::sync::MutexGuard` underneath: do not hold it across an
/// `.await`.
pub struct ScopedGuard<'a, T>(MutexGuard<'a, T>);

impl<T> Deref for ScopedGuard<'_, T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> DerefMut for ScopedGuard<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T: fmt::Debug> fmt::Debug for ScopedGuard<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// A final-state check run when an atomic scope commits: given the state
/// before the scope opened and the state it would commit, the changes that
/// break a cross-object constraint (§4.2). Empty means commit.
type Validate<T> = Box<dyn FnOnce(&T, &T) -> Vec<DeferredFailure> + Send>;

/// A memory backend's state behind a mutex, gated by a [`ScopeGate`] so a
/// conditional-02 [`SetScope`] can isolate it and roll it back
/// (docs/design/conditional.md §7).
///
/// **Demonstration quality.** It is correct, but it serializes every scoped
/// method behind one gate and clones the whole value per atomic call. A
/// production backend uses its storage engine's transactions instead.
///
/// Replaces `Arc<Mutex<Inner>>`. Cloning shares the state and the gate.
///
/// - [`lock`](Self::lock) is for async backend methods, reads included: it
///   waits while another task's scope is open, so no caller sees a change
///   an atomic method may still undo. A task in the open scope passes.
/// - [`lock_unscoped`](Self::lock_unscoped) is for sync code and seeding
///   helpers; it never waits on the gate.
/// - [`open`](Self::open) is how `begin_set` opens a scope.
///
/// With no scope ever opened, as in a deployment that does not opt in to
/// conditional-02, the gate is always free and `lock` is a plain mutex
/// lock.
pub struct ScopedState<T> {
    value: Arc<Mutex<T>>,
    gate: Arc<ScopeGate>,
}

impl<T> Clone for ScopedState<T> {
    fn clone(&self) -> Self {
        Self {
            value: Arc::clone(&self.value),
            gate: Arc::clone(&self.gate),
        }
    }
}

impl<T: Default> Default for ScopedState<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: fmt::Debug> fmt::Debug for ScopedState<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ScopedState")
            .field("value", &self.value)
            .field("gate", &self.gate)
            .finish()
    }
}

impl<T> ScopedState<T> {
    /// `value`, behind a gate of its own.
    pub fn new(value: T) -> Self {
        Self::with_gate(value, ScopeGate::new())
    }

    /// `value`, behind `gate`, which other states may share.
    pub fn with_gate(value: T, gate: Arc<ScopeGate>) -> Self {
        Self {
            value: Arc::new(Mutex::new(value)),
            gate,
        }
    }

    /// The gate, to share with another state through
    /// [`with_gate`](Self::with_gate).
    pub fn gate(&self) -> &Arc<ScopeGate> {
        &self.gate
    }

    /// Lock the value for an async backend method.
    ///
    /// Waits while a scope opened by another task holds the gate; passes
    /// at once when the running task is in the open scope
    /// ([`current_set_scope`]), or no scope is open. A poisoned mutex is
    /// taken anyway: the value is plain data.
    pub async fn lock(&self) -> ScopedGuard<'_, T> {
        if self.gate.owned_by_current().is_none() {
            // Wait for any open scope to end, and take the value before
            // releasing the gate: released first, a scope could open and
            // snapshot in between, and this guard's writes would then land
            // inside that scope (and be lost if it rolls back). Taken
            // first, a scope opening next waits for this guard to snapshot.
            let _gate = self.gate.lock.lock().await;
            return self.lock_unscoped();
        }
        self.lock_unscoped()
    }

    /// Lock the value without waiting on the gate, for sync code and
    /// seeding helpers that never run alongside a scoped method.
    pub fn lock_unscoped(&self) -> ScopedGuard<'_, T> {
        ScopedGuard(self.value.lock().unwrap_or_else(PoisonError::into_inner))
    }

    /// Whether the running task is in an open atomic scope on this
    /// state's gate.
    ///
    /// Per-object methods skip their cross-object checks (unique names,
    /// cycles, single default, quotas) when it is true: the scope's
    /// `validate` runs them against the final state at commit, so a swap
    /// that passes through a conflict still commits (§4.2).
    pub fn deferring(&self) -> bool {
        self.gate.owned_by_current() == Some(SetMode::Atomic)
    }
}

impl<T: Clone + Send + 'static> ScopedState<T> {
    /// Open the [`SetScope`] for `request`, for
    /// [`ConditionalBackend::begin_set`](crate::ConditionalBackend::begin_set).
    ///
    /// Waits for the gate and records `request.scope` as its owner, so
    /// calls made in that scope pass [`lock`](Self::lock) and every other
    /// task waits until the scope ends. If `request.scope` already holds
    /// the gate (a second state sharing it, opened for the same method),
    /// it does not wait.
    ///
    /// [`SetMode::Atomic`] snapshots the value. Rollback, or dropping the
    /// scope, restores the snapshot, so the state string and change log
    /// held in `T` are as before (§4.2). Commit first runs
    /// `validate(&before, &after)`; any failures it returns restore the
    /// snapshot and reject the commit with [`CommitError::Rejected`].
    ///
    /// [`SetMode::Isolated`] takes no snapshot and never calls `validate`:
    /// each change stands on its own (§6.1), and the dispatcher writes
    /// nothing before the handler runs. Ending the scope only releases
    /// the gate.
    pub async fn open<V>(&self, request: SetScopeRequest<'_>, validate: V) -> Box<dyn SetScope>
    where
        V: FnOnce(&T, &T) -> Vec<DeferredFailure> + Send + 'static,
    {
        let held = self.gate.owner().is_some_and(|(id, _)| id == request.scope);
        let permit = if held {
            None
        } else {
            let permit = Arc::clone(&self.gate.lock).lock_owned().await;
            self.gate.set_owner(Some((request.scope, request.mode)));
            Some(permit)
        };
        let atomic = request.mode == SetMode::Atomic;
        let snapshot = atomic.then(|| self.lock_unscoped().clone());
        Box::new(StateScope {
            value: Arc::clone(&self.value),
            gate: Arc::clone(&self.gate),
            permit,
            snapshot,
            validate: atomic.then(|| Box::new(validate) as Validate<T>),
        })
    }
}

/// The [`SetScope`] [`ScopedState::open`] returns.
struct StateScope<T> {
    value: Arc<Mutex<T>>,
    gate: Arc<ScopeGate>,
    /// The gate, when this scope took it (not when another state of the
    /// same scope did).
    permit: Option<OwnedMutexGuard<()>>,
    /// The value before the scope, for an atomic scope not yet ended.
    snapshot: Option<T>,
    validate: Option<Validate<T>>,
}

impl<T> StateScope<T> {
    /// Put the snapshot back, if there is one.
    fn restore(&mut self) {
        if let Some(before) = self.snapshot.take() {
            *self.value.lock().unwrap_or_else(PoisonError::into_inner) = before;
        }
    }
}

impl<T: Send + 'static> SetScope for StateScope<T> {
    fn commit(mut self: Box<Self>) -> CommitFuture {
        let failures = match (self.snapshot.as_ref(), self.validate.take()) {
            (Some(before), Some(validate)) => {
                let after = self.value.lock().unwrap_or_else(PoisonError::into_inner);
                validate(before, &after)
            }
            _ => Vec::new(),
        };
        let result = if failures.is_empty() {
            self.snapshot = None;
            Ok(())
        } else {
            self.restore();
            Err(CommitError::Rejected(failures))
        };
        drop(self);
        Box::pin(ready(result))
    }

    fn rollback(self: Box<Self>) -> RollbackFuture {
        // Drop restores the snapshot and releases the gate.
        drop(self);
        Box::pin(ready(()))
    }
}

impl<T> Drop for StateScope<T> {
    fn drop(&mut self) {
        // A scope not committed rolls back (the SetScope contract), before
        // the gate is released below so no waiter sees the undone writes.
        self.restore();
        if let Some(permit) = self.permit.take() {
            self.gate.set_owner(None);
            drop(permit);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::future::Future;
    use std::pin::pin;
    use std::task::{Context, Poll, Waker};

    use jmap_types::{Id, SetError};
    use serde_json::Value;

    use super::*;
    use crate::SetErrorType;
    use crate::conditional::{SetOp, in_set_scope};

    /// A memory store with a state counter and change log, like the
    /// memory backends' `Inner`.
    #[derive(Debug, Clone, Default, PartialEq)]
    struct Store {
        names: BTreeMap<String, String>,
        state: u64,
        log: Vec<String>,
    }

    impl Store {
        fn write(&mut self, id: &str, name: &str) {
            self.names.insert(id.to_owned(), name.to_owned());
            self.state += 1;
            self.log.push(id.to_owned());
        }
    }

    fn seeded() -> ScopedState<Store> {
        let state = ScopedState::<Store>::default();
        state.lock_unscoped().write("a", "one");
        state
    }

    /// `fut`'s output, which must be ready on the first poll: the call
    /// did not wait.
    fn now<F: Future>(fut: F) -> F::Output {
        let mut cx = Context::from_waker(Waker::noop());
        match pin!(fut).poll(&mut cx) {
            Poll::Ready(out) => out,
            Poll::Pending => panic!("waited, but nothing should block"),
        }
    }

    /// Whether `fut` would wait on its first poll.
    fn waits<F: Future>(fut: F) -> bool {
        let mut cx = Context::from_waker(Waker::noop());
        pin!(fut).poll(&mut cx).is_pending()
    }

    fn request(mode: SetMode) -> SetScopeRequest<'static> {
        SetScopeRequest::new("Probe/set", "Probe", None, mode, &Value::Null)
    }

    fn no_checks(_: &Store, _: &Store) -> Vec<DeferredFailure> {
        Vec::new()
    }

    #[test]
    fn lock_never_waits_with_no_scope_open() {
        let state = seeded();
        assert_eq!(now(state.lock()).names["a"], "one");
        // Nor once a scope has come and gone.
        let scope = now(state.open(request(SetMode::Atomic), no_checks));
        now(scope.commit()).unwrap();
        now(state.lock()).write("b", "two");
        assert_eq!(state.lock_unscoped().names.len(), 2);
    }

    #[tokio::test]
    async fn unscoped_read_and_write_wait_for_another_tasks_scope() {
        let state = seeded();
        let req = request(SetMode::Atomic);
        let scope = state.open(req, no_checks).await;
        in_set_scope(req.scope, async { state.lock().await.write("a", "scoped") }).await;

        let reader = state.clone();
        let read = tokio::spawn(async move { reader.lock().await.names["a"].clone() });
        let writer = state.clone();
        let write = tokio::spawn(async move { writer.lock().await.write("b", "outside") });
        for _ in 0..10 {
            tokio::task::yield_now().await;
        }
        assert!(!read.is_finished(), "a read must not see the open scope");
        assert!(!write.is_finished(), "a write must wait for the open scope");

        scope.rollback().await;
        // The reader never saw "scoped"; the writer landed after rollback.
        assert_eq!(read.await.unwrap(), "one");
        write.await.unwrap();
        let after = state.lock_unscoped();
        assert_eq!(after.names["a"], "one");
        assert_eq!(after.names["b"], "outside");
        assert_eq!(after.log, ["a", "b"]);
    }

    /// Unscoped writes racing scopes that roll back are never lost: an
    /// unscoped `lock` holds the gate until it has the value, so no scope
    /// can snapshot between the two and restore over the write.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn unscoped_writes_racing_rolled_back_scopes_are_kept() {
        const WRITES: u64 = 2000;
        let state = ScopedState::new(0_u64);
        let writers: Vec<_> = (0..2)
            .map(|_| {
                let state = state.clone();
                tokio::spawn(async move {
                    for _ in 0..WRITES {
                        *state.lock().await += 1;
                        tokio::task::yield_now().await;
                    }
                })
            })
            .collect();
        let scoper = {
            let state = state.clone();
            tokio::spawn(async move {
                for _ in 0..WRITES {
                    let req = request(SetMode::Atomic);
                    let scope = state.open(req, |_: &u64, _: &u64| Vec::new()).await;
                    in_set_scope(req.scope, async { *state.lock().await += 1_000_000 }).await;
                    scope.rollback().await;
                }
            })
        };
        for w in writers {
            w.await.unwrap();
        }
        scoper.await.unwrap();
        assert_eq!(*state.lock_unscoped(), 2 * WRITES);
    }

    #[test]
    fn scope_owner_reenters_without_waiting() {
        let state = seeded();
        let req = request(SetMode::Isolated);
        let _scope = now(state.open(req, no_checks));
        let len = now(in_set_scope(req.scope, async {
            state.lock().await.write("b", "two");
            state.lock().await.write("c", "three");
            state.lock().await.names.len()
        }));
        assert_eq!(len, 3);
        // Outside the scope, the same lock waits.
        assert!(waits(state.lock()));
    }

    #[test]
    fn states_sharing_a_gate_do_not_deadlock() {
        let a = seeded();
        let b = ScopedState::with_gate(Store::default(), Arc::clone(a.gate()));
        let req = request(SetMode::Atomic);
        let scope_a = now(a.open(req, no_checks));
        // Opening the second state for the same scope does not wait on
        // the gate the first already holds.
        let scope_b = now(b.open(req, no_checks));
        now(in_set_scope(req.scope, async {
            a.lock().await.write("a", "changed");
            b.lock().await.write("x", "new");
            a.lock().await.write("a", "again");
        }));
        assert!(waits(b.lock()), "b is gated by a's scope");

        // b's scope rolls back b only and keeps the gate held.
        now(scope_b.rollback());
        assert_eq!(*b.lock_unscoped(), Store::default());
        assert!(waits(a.lock()));
        now(scope_a.commit()).unwrap();
        assert_eq!(now(a.lock()).names["a"], "again");
        assert_eq!(*now(b.lock()), Store::default());
    }

    #[test]
    fn rollback_restores_state_and_change_log() {
        let state = seeded();
        let before = state.lock_unscoped().clone();
        let req = request(SetMode::Atomic);
        let scope = now(state.open(req, no_checks));
        now(in_set_scope(req.scope, async {
            let mut s = state.lock().await;
            s.write("a", "renamed");
            s.write("b", "created");
        }));
        assert_eq!(state.lock_unscoped().state, 3);
        now(scope.rollback());
        assert_eq!(*state.lock_unscoped(), before);
        assert_eq!(before.state, 1);
        assert_eq!(before.log, ["a"]);
    }

    #[test]
    fn dropping_a_scope_rolls_back_and_frees_the_gate() {
        let state = seeded();
        let before = state.lock_unscoped().clone();
        let req = request(SetMode::Atomic);
        let scope = now(state.open(req, no_checks));
        now(in_set_scope(req.scope, async {
            state.lock().await.write("b", "two")
        }));
        drop(scope);
        assert_eq!(*now(state.lock()), before);
    }

    #[test]
    fn commit_keeps_changes_validated_against_the_final_state() {
        let state = seeded();
        let req = request(SetMode::Atomic);
        let scope = now(state.open(req, |before: &Store, after: &Store| {
            assert_eq!(before.names["a"], "one");
            assert_eq!(after.names["a"], "two");
            Vec::new()
        }));
        now(in_set_scope(req.scope, async {
            state.lock().await.write("a", "two")
        }));
        now(scope.commit()).unwrap();
        let after = state.lock_unscoped();
        assert_eq!((after.names["a"].as_str(), after.state), ("two", 2));
    }

    #[test]
    fn rejected_commit_restores_and_reports_the_failures() {
        let state = seeded();
        let before = state.lock_unscoped().clone();
        let req = request(SetMode::Atomic);
        // Final-state uniqueness: no two objects share a name.
        let scope = now(state.open(req, |_: &Store, after: &Store| {
            let mut seen = BTreeMap::new();
            after
                .names
                .iter()
                .filter(|(_, name)| seen.insert(name.as_str(), ()).is_some())
                .map(|(id, _)| {
                    DeferredFailure::new(
                        SetOp::Update(Id::new_validated(id).unwrap()),
                        SetError::new(SetErrorType::AlreadyExists),
                    )
                })
                .collect()
        }));
        now(in_set_scope(req.scope, async {
            state.lock().await.write("b", "one")
        }));
        let Err(CommitError::Rejected(failures)) = now(scope.commit()) else {
            panic!("a duplicate name must reject the commit");
        };
        assert_eq!(failures.len(), 1);
        assert_eq!(
            failures[0].op,
            SetOp::Update(Id::new_validated("b").unwrap())
        );
        assert_eq!(
            *now(state.lock()),
            before,
            "rejected: rolled back, gate free"
        );
    }

    #[test]
    fn isolated_scope_keeps_its_writes_and_skips_validate() {
        let state = seeded();
        let req = request(SetMode::Isolated);
        let scope = now(
            state.open(req, |_: &Store, _: &Store| -> Vec<DeferredFailure> {
                panic!("an isolated scope has no final-state check")
            }),
        );
        now(in_set_scope(req.scope, async {
            assert!(!state.deferring());
            state.lock().await.write("b", "two");
        }));
        now(scope.commit()).unwrap();
        assert_eq!(now(state.lock()).names.len(), 2);

        // Rolling back an isolated scope keeps what it wrote: each change
        // stood on its own (§6.1).
        let req = request(SetMode::Isolated);
        let scope = now(state.open(req, no_checks));
        now(in_set_scope(req.scope, async {
            state.lock().await.write("c", "three")
        }));
        now(scope.rollback());
        assert_eq!(now(state.lock()).names.len(), 3);
    }

    #[test]
    fn deferring_only_inside_the_running_atomic_scope() {
        let state = seeded();
        assert!(!state.deferring());
        let req = request(SetMode::Atomic);
        let scope = now(state.open(req, no_checks));
        assert!(!state.deferring(), "not in the scope");
        assert!(now(in_set_scope(req.scope, async { state.deferring() })));
        let other = SetScopeId::next();
        assert!(!now(in_set_scope(other, async { state.deferring() })));
        drop(scope);
        assert!(!now(in_set_scope(req.scope, async { state.deferring() })));
    }
}