nautilus-common 0.64.0

Common functionality and machinery for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Checked allocation access for ordered callback dispatch.
//!
//! Actor registry guards and component lifecycle operations do not acquire these guards.
//! Component lifecycle operations use separate ID-based tracking. Callers must prevent
//! overlapping access through raw handles, actor registry guards, or component lifecycle
//! operations; allocation-level exclusion applies only between these checked guards.
//!
//! Production dispatch does not use these primitives. Activation requires the callback and
//! runtime boundaries described in `docs/developer_guide/callback_dispatch.md`.

#![allow(
    dead_code,
    reason = "activation requires ordered callback dispatch and safe runtime drains"
)]

use std::{
    any::{Any, TypeId},
    cell::{RefCell, UnsafeCell},
    marker::PhantomData,
    ops::{Deref, DerefMut},
    rc::Rc,
};

use ahash::AHashSet;
use ustr::Ustr;

use super::{Actor, registry::get_actor};

thread_local! {
    static BORROWED: Rc<RefCell<AHashSet<*const ()>>> = Rc::default();
}

pub(super) fn is_active() -> bool {
    BORROWED
        .try_with(|borrowed| !borrowed.borrow().is_empty())
        .unwrap_or(true)
}

/// Owns an allocation and excludes other checked accesses to that allocation until it drops.
///
/// Retaining the `Rc` prevents address reuse while access is held.
#[derive(Debug)]
pub(crate) struct AllocationGuard<T: ?Sized> {
    allocation: Rc<UnsafeCell<T>>,
    borrowed: Rc<RefCell<AHashSet<*const ()>>>,
}

impl<T: ?Sized> AllocationGuard<T> {
    /// Returns a guard, or `None` when the allocation is busy or thread-local tracking has ended.
    pub(crate) fn acquire(allocation: Rc<UnsafeCell<T>>) -> Option<Self> {
        let address = Rc::as_ptr(&allocation).cast::<()>();
        BORROWED
            .try_with(|borrowed| {
                if !borrowed.borrow_mut().insert(address) {
                    return None;
                }

                Some(Self {
                    allocation,
                    borrowed: Rc::clone(borrowed),
                })
            })
            .ok()
            .flatten()
    }
}

impl<T: ?Sized> Deref for AllocationGuard<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        // SAFETY: The guard retains the allocation and excludes competing checked access.
        // Callers must exclude overlapping access through paths that do not acquire these guards.
        unsafe { &*self.allocation.get() }
    }
}

impl<T: ?Sized> DerefMut for AllocationGuard<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SAFETY: The guard retains the allocation and excludes competing checked access.
        // Callers must exclude overlapping access through paths that do not acquire these guards.
        unsafe { &mut *self.allocation.get() }
    }
}

impl<T: ?Sized> Drop for AllocationGuard<T> {
    fn drop(&mut self) {
        // Release before dropping the allocation, whose destructor may acquire other guards
        self.borrowed
            .borrow_mut()
            .remove(&Rc::as_ptr(&self.allocation).cast::<()>());
    }
}

/// Owns checked access to an actor whose concrete type is verified under the allocation guard.
///
/// Verification uses `Any` directly because an actor can override `as_any`.
#[derive(Debug)]
pub(crate) struct ActorGuard<T: Actor> {
    allocation: AllocationGuard<dyn Actor>,
    marker: PhantomData<T>,
}

impl<T: Actor> ActorGuard<T> {
    /// Acquires access before type inspection, so busy access takes precedence over a wrong type.
    pub(crate) fn acquire(actor: Rc<UnsafeCell<dyn Actor>>) -> Result<Self, ActorAccessError> {
        let allocation = AllocationGuard::acquire(actor).ok_or(ActorAccessError::Busy)?;
        let actual_type = Any::type_id(&*allocation);
        let expected_type = TypeId::of::<T>();
        if actual_type != expected_type {
            return Err(ActorAccessError::WrongType {
                expected_type,
                actual_type,
            });
        }

        Ok(Self {
            allocation,
            marker: PhantomData,
        })
    }
}

impl<T: Actor> Deref for ActorGuard<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        // SAFETY: The allocation guard is held and the concrete type was verified on acquisition.
        // Callers must exclude overlapping access through paths that do not acquire these guards.
        unsafe { &*self.allocation.allocation.get().cast::<T>() }
    }
}

impl<T: Actor> DerefMut for ActorGuard<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SAFETY: The allocation guard is held and the concrete type was verified on acquisition.
        // Callers must exclude overlapping access through paths that do not acquire these guards.
        unsafe { &mut *self.allocation.allocation.get().cast::<T>() }
    }
}

pub(crate) fn try_get_actor<T: Actor>(id: &Ustr) -> Result<ActorGuard<T>, ActorAccessError> {
    ActorGuard::acquire(get_actor(id).ok_or(ActorAccessError::Missing)?)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ActorAccessError {
    Missing,
    // Includes unavailable tracking during thread-local teardown
    Busy,
    WrongType {
        expected_type: TypeId,
        actual_type: TypeId,
    },
}

#[cfg(test)]
mod tests {
    use std::{cell::Cell, panic::AssertUnwindSafe};

    use nautilus_model::identifiers::{ComponentId, TraderId};
    use rstest::rstest;

    use super::*;
    use crate::{
        actor::registry::{
            clear_actor_registry, deregister_actor, register_actor, with_actor_registry,
        },
        cache::Cache,
        clock::Clock,
        component::{Component, register_component_actor},
        enums::{ComponentState, ComponentTrigger},
    };

    #[derive(Debug)]
    struct TestActor {
        id: Ustr,
        value: i32,
    }

    impl Actor for TestActor {
        fn id(&self) -> Ustr {
            self.id
        }

        fn handle(&mut self, _msg: &dyn Any) {}

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    impl Component for TestActor {
        fn component_id(&self) -> ComponentId {
            ComponentId::new(self.id.as_str())
        }

        fn state(&self) -> ComponentState {
            ComponentState::Ready
        }

        fn transition_state(&mut self, _trigger: ComponentTrigger) -> anyhow::Result<()> {
            Ok(())
        }

        fn register(
            &mut self,
            _trader_id: TraderId,
            _clock: Rc<RefCell<dyn Clock>>,
            _cache: Rc<RefCell<Cache>>,
        ) -> anyhow::Result<()> {
            Ok(())
        }
    }

    #[derive(Debug)]
    struct OtherActor;

    impl Actor for OtherActor {
        fn id(&self) -> Ustr {
            Ustr::from("other")
        }

        fn handle(&mut self, _msg: &dyn Any) {}

        fn as_any(&self) -> &dyn Any {
            &0_u8
        }
    }

    #[rstest]
    fn test_actor_type_uses_allocation_identity() {
        register_actor(OtherActor);
        let actor = try_get_actor::<OtherActor>(&Ustr::from("other")).unwrap();
        assert_eq!(actor.id(), Ustr::from("other"));
    }

    #[rstest]
    fn test_actor_access_distinguishes_missing_wrong_type_and_busy() {
        clear_actor_registry();
        let id = Ustr::from("checked-errors");
        assert_eq!(
            try_get_actor::<TestActor>(&id).unwrap_err(),
            ActorAccessError::Missing
        );
        register_actor(TestActor { id, value: 11 });
        assert_eq!(
            try_get_actor::<OtherActor>(&id).unwrap_err(),
            ActorAccessError::WrongType {
                expected_type: TypeId::of::<OtherActor>(),
                actual_type: TypeId::of::<TestActor>(),
            }
        );
        let mut actor = try_get_actor::<TestActor>(&id).unwrap();
        reject_nested_access(&mut actor);
        assert_eq!(actor.value, 29);
    }

    fn reject_nested_access(actor: &mut TestActor) {
        actor.value = 17;
        assert_eq!(
            try_get_actor::<TestActor>(&actor.id).unwrap_err(),
            ActorAccessError::Busy
        );
        assert_eq!(
            try_get_actor::<OtherActor>(&actor.id).unwrap_err(),
            ActorAccessError::Busy
        );
        actor.value = 29;
    }

    #[rstest]
    fn test_actor_and_component_views_share_access() {
        let id = Ustr::from("checked-component");
        let allocation = register_component_actor(TestActor { id, value: 31 });
        let component: Rc<UnsafeCell<dyn Component>> = allocation;
        let actor = try_get_actor::<TestActor>(&id).unwrap();
        assert!(AllocationGuard::acquire(component.clone()).is_none());
        drop(actor);
        let mut component_guard = AllocationGuard::acquire(component).unwrap();
        reject_nested_component_access(&mut *component_guard, id);
        assert_eq!(component_guard.state(), ComponentState::Ready);
        drop(component_guard);
        assert_eq!(try_get_actor::<TestActor>(&id).unwrap().value, 31);
    }

    #[allow(
        clippy::needless_pass_by_ref_mut,
        reason = "the protected mutable reference must remain live across the rejected lookup"
    )]
    fn reject_nested_component_access(component: &mut dyn Component, id: Ustr) {
        assert_eq!(component.component_id().inner(), id);
        assert_eq!(
            try_get_actor::<TestActor>(&id).unwrap_err(),
            ActorAccessError::Busy
        );
        assert_eq!(component.component_id().inner(), id);
    }

    #[rstest]
    fn test_removal_replacement_and_reregistration_preserve_access() {
        let id = Ustr::from("checked-registration");
        let alias = Ustr::from("checked-alias");
        let allocation = register_actor(TestActor { id, value: 37 });
        let mut actor = try_get_actor::<TestActor>(&id).unwrap();
        deregister_actor(&id);
        assert_eq!(
            try_get_actor::<TestActor>(&id).unwrap_err(),
            ActorAccessError::Missing
        );
        register_actor(TestActor { id, value: 41 });
        assert_eq!(try_get_actor::<TestActor>(&id).unwrap().value, 41);
        with_actor_registry(|registry| {
            registry.insert(id, allocation.clone());
            registry.insert(alias, allocation.clone());
        });

        assert_eq!(
            try_get_actor::<TestActor>(&id).unwrap_err(),
            ActorAccessError::Busy
        );
        assert_eq!(
            try_get_actor::<TestActor>(&alias).unwrap_err(),
            ActorAccessError::Busy
        );
        actor.value = 43;
        drop(actor);
        assert_eq!(try_get_actor::<TestActor>(&id).unwrap().value, 43);
    }

    #[rstest]
    fn test_guard_retains_allocation_after_registry_clear() {
        let id = Ustr::from("checked-lifetime");
        let allocation = register_actor(TestActor { id, value: 47 });
        let weak = Rc::downgrade(&allocation);
        let actor = try_get_actor::<TestActor>(&id).unwrap();
        drop(allocation);
        clear_actor_registry();
        assert_eq!(weak.strong_count(), 1);
        assert_eq!(actor.value, 47);
        drop(actor);
        assert_eq!(weak.strong_count(), 0);
    }

    #[rstest]
    fn test_unwind_releases_access() {
        let id = Ustr::from("checked-unwind");
        register_actor(TestActor { id, value: 53 });

        let result = std::panic::catch_unwind(|| {
            let mut actor = try_get_actor::<TestActor>(&id).unwrap();
            actor.value = 59;
            panic!("unwind checked access");
        });

        assert_eq!(
            result.unwrap_err().downcast_ref::<&str>(),
            Some(&"unwind checked access")
        );
        assert_eq!(try_get_actor::<TestActor>(&id).unwrap().value, 59);
    }

    #[rstest]
    fn test_allocation_guard_mutation_and_unwind() {
        let allocation = Rc::new(UnsafeCell::new(61));

        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
            let mut guard = AllocationGuard::acquire(allocation.clone()).unwrap();
            *guard = 67;
            assert!(AllocationGuard::acquire(allocation.clone()).is_none());
            panic!("unwind allocation access");
        }));

        assert_eq!(
            result.unwrap_err().downcast_ref::<&str>(),
            Some(&"unwind allocation access")
        );
        assert_eq!(*AllocationGuard::acquire(allocation).unwrap(), 67);
    }

    #[derive(Debug)]
    struct ReentrantDrop {
        allocation: Rc<UnsafeCell<i32>>,
        observed: Rc<Cell<i32>>,
    }

    impl Drop for ReentrantDrop {
        fn drop(&mut self) {
            let guard = AllocationGuard::acquire(self.allocation.clone()).unwrap();
            self.observed.set(*guard);
        }
    }

    #[rstest]
    fn test_allocation_destructor_can_acquire_access() {
        let observed = Rc::new(Cell::new(0));

        let allocation = Rc::new(UnsafeCell::new(ReentrantDrop {
            allocation: Rc::new(UnsafeCell::new(71)),
            observed: observed.clone(),
        }));

        let guard = AllocationGuard::acquire(allocation).unwrap();
        drop(guard);
        assert_eq!(observed.get(), 71);
    }
}