rutis 0.6.0

Cordis core paradigms in idiomatic Rust: plugin assembly, fiber lifecycle, typed service registry, dependency-driven reload
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
use std::cell::RefCell;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::panic::{catch_unwind, AssertUnwindSafe, Location};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, Weak};

use crate::ctx::{Ctx, Shared};
use crate::effect::{Disposer, Effect};
use crate::error::{
    panic_error, CordisError, ServiceReadFailure, ServiceWriteError, ServiceWriteFailure,
};
use crate::fiber::{FiberInner, FiberState};
use crate::key::{ScopeId, TypeKey};
use crate::registry::{Binding, StoredValue};

/// A trusted interceptor can continue, replace a same-type value, or deny an
/// operation. The type alone cannot prove which instance created `Arc<T>`.
pub enum ServiceIntercept<T: ?Sized> {
    Continue,
    Replace(Arc<T>),
    Deny,
}

enum ErasedDecision {
    Continue,
    Replace(StoredValue),
    Deny,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum HookKind {
    Read,
    Write,
}

type HookKey = (TypeKey, Option<ScopeId>);
type HookCall = dyn Fn(&StoredValue) -> ErasedDecision + Send + Sync;

struct Hook {
    call: Arc<HookCall>,
    owner: Weak<FiberInner>,
}

#[derive(Default)]
pub(crate) struct ServiceInterceptors {
    reads: Mutex<HashMap<HookKey, Vec<Arc<Hook>>>>,
    writes: Mutex<HashMap<HookKey, Vec<Arc<Hook>>>>,
    read_count: AtomicUsize,
    write_count: AtomicUsize,
}

impl ServiceInterceptors {
    fn count(&self, kind: HookKind) -> &AtomicUsize {
        match kind {
            HookKind::Read => &self.read_count,
            HookKind::Write => &self.write_count,
        }
    }

    fn has_any(&self, kind: HookKind) -> bool {
        self.count(kind).load(Ordering::SeqCst) != 0
    }

    fn table(&self, kind: HookKind) -> &Mutex<HashMap<HookKey, Vec<Arc<Hook>>>> {
        match kind {
            HookKind::Read => &self.reads,
            HookKind::Write => &self.writes,
        }
    }

    fn insert(&self, kind: HookKind, key: HookKey, hook: Arc<Hook>) {
        self.table(kind)
            .lock()
            .unwrap()
            .entry(key)
            .or_default()
            .push(hook);
        self.count(kind).fetch_add(1, Ordering::SeqCst);
    }

    fn remove(&self, kind: HookKind, key: &HookKey, hook: &Arc<Hook>) {
        let mut table = self.table(kind).lock().unwrap();
        if let Some(list) = table.get_mut(key) {
            let before = list.len();
            list.retain(|entry| !Arc::ptr_eq(entry, hook));
            self.count(kind)
                .fetch_sub(before - list.len(), Ordering::SeqCst);
            if list.is_empty() {
                table.remove(key);
            }
        }
        if table.capacity() > 64 && table.len() * 4 < table.capacity() {
            table.shrink_to_fit();
        }
    }

    fn select(&self, kind: HookKind, key: &HookKey, actor: &Ctx) -> (Vec<Arc<Hook>>, HookFlight) {
        let _admission = actor.shared().admission.lock().unwrap();
        let mut ancestry = Vec::new();
        let mut current = actor.weak_fiber().upgrade();
        while let Some(fiber) = current {
            current = fiber.parent_fiber.as_ref().and_then(Weak::upgrade);
            ancestry.push(fiber);
        }
        let mut owners = Vec::new();
        if let Some(actor) = ancestry.first() {
            owners.push(actor.clone());
        }
        let hooks = self
            .table(kind)
            .lock()
            .unwrap()
            .get(key)
            .into_iter()
            .flat_map(|list| list.iter())
            .filter_map(|hook| {
                let owner = hook.owner.upgrade()?;
                if !owner.alive.load(Ordering::SeqCst)
                    || owner.closing.load(Ordering::SeqCst)
                    || !ancestry.iter().any(|fiber| Arc::ptr_eq(fiber, &owner))
                {
                    return None;
                }
                owners.push(owner);
                Some(hook.clone())
            })
            .collect();
        (hooks, HookFlight::new(owners))
    }

    fn run(
        &self,
        kind: HookKind,
        key: HookKey,
        actor: &Ctx,
        mut value: StoredValue,
    ) -> Result<StoredValue, HookFailure> {
        if !self.has_any(kind) {
            return Ok(value);
        }
        let (hooks, _flight) = self.select(kind, &key, actor);
        if hooks.is_empty() {
            return Ok(value);
        }
        let _guard = ReentryGuard::enter(kind, key)?;
        for hook in hooks {
            match catch_unwind(AssertUnwindSafe(|| (hook.call)(&value))) {
                Ok(ErasedDecision::Continue) => {}
                Ok(ErasedDecision::Replace(replacement)) => value = replacement,
                Ok(ErasedDecision::Deny) => return Err(HookFailure::Denied),
                Err(_) => return Err(HookFailure::Panicked),
            }
        }
        Ok(value)
    }

    #[cfg(test)]
    pub(crate) fn table_counts(&self) -> (usize, usize) {
        (
            self.reads.lock().unwrap().len(),
            self.writes.lock().unwrap().len(),
        )
    }
}

struct HookFlight(Vec<Arc<FiberInner>>);

impl HookFlight {
    fn new(mut owners: Vec<Arc<FiberInner>>) -> Self {
        owners.sort_by_key(|fiber| fiber.id);
        owners.dedup_by_key(|fiber| fiber.id);
        for fiber in &owners {
            fiber.begin_event();
        }
        Self(owners)
    }
}

impl Drop for HookFlight {
    fn drop(&mut self) {
        for fiber in &self.0 {
            fiber.finish_event();
        }
    }
}

#[derive(Clone, Copy)]
enum HookFailure {
    Denied,
    Panicked,
    Reentrant,
}

thread_local! {
    static ACTIVE_HOOKS: RefCell<Vec<(HookKind, HookKey)>> = const { RefCell::new(Vec::new()) };
}

struct ReentryGuard;

impl ReentryGuard {
    fn enter(kind: HookKind, key: HookKey) -> Result<Self, HookFailure> {
        ACTIVE_HOOKS.with(|active| {
            let mut active = active.borrow_mut();
            if active.iter().any(|entry| entry.0 == kind && entry.1 == key) {
                return Err(HookFailure::Reentrant);
            }
            active.push((kind, key));
            Ok(Self)
        })
    }
}

impl Drop for ReentryGuard {
    fn drop(&mut self) {
        ACTIVE_HOOKS.with(|active| {
            active.borrow_mut().pop();
        });
    }
}

impl Ctx {
    /// Intercept only strict reads after existing declaration, visibility and
    /// readiness checks. Explicit `get_as` remains an unhooked locator.
    pub fn intercept_require_as<T: ?Sized + Send + Sync + 'static>(
        &self,
        key: impl Into<TypeKey>,
        hook: impl Fn(Arc<T>) -> ServiceIntercept<T> + Send + Sync + 'static,
    ) -> Result<Disposer, CordisError> {
        self.register_service_interceptor(key.into(), HookKind::Read, hook)
    }

    /// Intercept a provider's update of a binding made with `provide_mut_as`.
    pub fn intercept_set_as<T: ?Sized + Send + Sync + 'static>(
        &self,
        key: impl Into<TypeKey>,
        hook: impl Fn(Arc<T>) -> ServiceIntercept<T> + Send + Sync + 'static,
    ) -> Result<Disposer, CordisError> {
        self.register_service_interceptor(key.into(), HookKind::Write, hook)
    }

    fn register_service_interceptor<T: ?Sized + Send + Sync + 'static>(
        &self,
        key: TypeKey,
        kind: HookKind,
        callback: impl Fn(Arc<T>) -> ServiceIntercept<T> + Send + Sync + 'static,
    ) -> Result<Disposer, CordisError> {
        self.registration_preflight()?;
        self.check_instance(&key)?;
        if !key.has_type::<T>() {
            return Err(CordisError::Validation {
                issues: vec![format!(
                    "interceptor type does not match {}",
                    key.describe()
                )],
            });
        }
        let scoped_key = (key.clone(), self.scope_for(&key));
        let call = move |value: &StoredValue| {
            let typed = value
                .downcast::<T>()
                .expect("interceptor key type checked at registration");
            match callback(typed) {
                ServiceIntercept::Continue => ErasedDecision::Continue,
                ServiceIntercept::Replace(value) => {
                    ErasedDecision::Replace(StoredValue::new(value))
                }
                ServiceIntercept::Deny => ErasedDecision::Deny,
            }
        };
        let hook = Arc::new(Hook {
            call: Arc::new(call),
            owner: self.weak_fiber(),
        });
        let shared = self.shared().clone();
        let label = match kind {
            HookKind::Read => format!("service read interceptor: {}", key.describe()),
            HookKind::Write => format!("service write interceptor: {}", key.describe()),
        };
        self.register_internal_effect_named(label, move |_, _, _| {
            shared
                .interceptors
                .insert(kind, scoped_key.clone(), hook.clone());
            Ok(Effect::AsyncDisposer(Box::new(move || {
                {
                    let _admission = shared.admission.lock().unwrap();
                    shared.interceptors.remove(kind, &scoped_key, &hook);
                }
                Box::pin(async move {
                    if let Some(owner) = hook.owner.upgrade() {
                        owner.wait_events().await;
                    }
                    Ok(())
                })
            })))
        })
    }

    pub(crate) fn apply_read_interceptors<T: ?Sized + Send + Sync + 'static>(
        &self,
        key: &TypeKey,
        scope: Option<&ScopeId>,
        value: Arc<T>,
    ) -> Result<Arc<T>, ServiceReadFailure> {
        if !self.shared().interceptors.has_any(HookKind::Read) {
            return Ok(value);
        }
        let stored = self
            .shared()
            .interceptors
            .run(
                HookKind::Read,
                (key.clone(), scope.cloned()),
                self,
                StoredValue::new(value),
            )
            .map_err(|failure| match failure {
                HookFailure::Denied => ServiceReadFailure::InterceptDenied,
                HookFailure::Panicked => ServiceReadFailure::InterceptPanicked,
                HookFailure::Reentrant => ServiceReadFailure::InterceptReentrant,
            })?;
        Ok(stored
            .downcast::<T>()
            .expect("interceptor preserves value type"))
    }
}

/// Generation-bound authority to update one mutable service binding.
pub struct ServiceWriter<T: ?Sized + Send + Sync + 'static> {
    key: TypeKey,
    scope: Option<ScopeId>,
    binding: Arc<Binding>,
    shared: Arc<Shared>,
    _marker: PhantomData<fn() -> Arc<T>>,
}

impl<T: ?Sized + Send + Sync + 'static> ServiceWriter<T> {
    pub(crate) fn new(
        key: TypeKey,
        scope: Option<ScopeId>,
        binding: Arc<Binding>,
        shared: Arc<Shared>,
    ) -> Self {
        Self {
            key,
            scope,
            binding,
            shared,
            _marker: PhantomData,
        }
    }

    /// Replace the value owned by the provider fiber. Existing `Arc<T>`
    /// snapshots keep their old value; future reads see the new one.
    #[track_caller]
    pub fn set(&self, caller: &Ctx, value: Arc<T>) -> Result<(), ServiceWriteError> {
        let location = Location::caller();
        let error = |reason| ServiceWriteError {
            key: self.key.clone(),
            provider: self.binding.provider_id,
            generation: self.binding.provider_gen,
            location,
            reason,
        };
        if !Arc::ptr_eq(caller.shared(), &self.shared) {
            return Err(error(ServiceWriteFailure::WrongOwner));
        }
        let provider = self
            .binding
            .provider
            .upgrade()
            .ok_or_else(|| error(ServiceWriteFailure::Stale))?;
        let actor = caller
            .weak_fiber()
            .upgrade()
            .ok_or_else(|| error(ServiceWriteFailure::WrongOwner))?;
        if !Arc::ptr_eq(&actor, &provider)
            || caller.scope_for(&self.key).as_ref() != self.scope.as_ref()
            || !caller.in_instance_key(&self.key)
        {
            return Err(error(ServiceWriteFailure::WrongOwner));
        }
        caller
            .registration_preflight()
            .map_err(|_| error(ServiceWriteFailure::Stale))?;
        let value = self
            .shared
            .interceptors
            .run(
                HookKind::Write,
                (self.key.clone(), self.scope.clone()),
                caller,
                StoredValue::new(value),
            )
            .map_err(|failure| {
                error(match failure {
                    HookFailure::Denied => ServiceWriteFailure::InterceptDenied,
                    HookFailure::Panicked => ServiceWriteFailure::InterceptPanicked,
                    HookFailure::Reentrant => ServiceWriteFailure::InterceptReentrant,
                })
            })?;
        // Locked section: admission → transition → registry bindings.
        // On success we return the old value; on failure we return the
        // *candidate* value so it is dropped outside every framework lock.
        // This prevents Drop reentry deadlocks (PR #55 P1).
        let (owned, result) = (|| -> (StoredValue, Result<(), ServiceWriteError>) {
            let _admission = self.shared.admission.lock().unwrap();
            if caller.registration_open().is_err() {
                return (value, Err(error(ServiceWriteFailure::Stale)));
            }
            let transition = provider.transition.lock().unwrap();
            if transition.generation != self.binding.provider_gen
                || !matches!(transition.state, FiberState::Loading | FiberState::Active)
            {
                return (value, Err(error(ServiceWriteFailure::Stale)));
            }
            match self.shared.registry.replace_mutable_if_current(
                &self.key,
                self.scope.as_ref(),
                &self.binding,
                value,
            ) {
                Ok(old) => (old, Ok(())),
                Err(candidate) => (candidate, Err(error(ServiceWriteFailure::Stale))),
            }
        })();
        // Drop the owned value (old on success, candidate on failure) outside
        // every lock. The last Arc may run user Drop code — panic is caught
        // and reported via the error sink.
        if let Err(panic) = catch_unwind(AssertUnwindSafe(|| drop(owned))) {
            let sink = caller.error_sink();
            let error = Arc::new(panic_error(panic));
            let _ = catch_unwind(AssertUnwindSafe(|| sink(error)));
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{BoxFuture, Plugin};

    struct HookPlugin;

    impl Plugin for HookPlugin {
        fn name(&self) -> &str {
            "hook-churn"
        }

        fn apply<'a>(&'a self, ctx: &'a Ctx) -> BoxFuture<'a, Result<Effect, CordisError>> {
            Box::pin(async move {
                let key = TypeKey::instance::<u64>(ctx.instance());
                ctx.intercept_require_as::<u64>(key.clone(), |_| ServiceIntercept::Continue)?;
                ctx.intercept_set_as::<u64>(key, |_| ServiceIntercept::Continue)?;
                Ok(Effect::Done)
            })
        }
    }

    #[tokio::test]
    async fn repeated_hook_disposal_reclaims_tables() {
        let root = Ctx::root().unwrap();
        let key = TypeKey::of::<u64>();
        for _ in 0..1000 {
            let read = root
                .intercept_require_as::<u64>(key.clone(), |_| ServiceIntercept::Continue)
                .unwrap();
            let write = root
                .intercept_set_as::<u64>(key.clone(), |_| ServiceIntercept::Continue)
                .unwrap();
            assert_eq!(root.shared().interceptors.table_counts(), (1, 1));
            read.dispose().await.unwrap();
            write.dispose().await.unwrap();
            assert_eq!(root.shared().interceptors.table_counts(), (0, 0));
        }
    }

    #[tokio::test]
    async fn thousand_child_shutdowns_reclaim_instance_hook_keys() {
        let root = Ctx::root().unwrap();
        for _ in 0..1000 {
            let child = root.plugin(HookPlugin);
            (&child).await.unwrap();
            assert_eq!(root.shared().interceptors.table_counts(), (1, 1));
            child.shutdown().await.unwrap();
            assert_eq!(root.shared().interceptors.table_counts(), (0, 0));
        }
    }
}