beamr 0.15.3

A Rust runtime with the BEAM's execution model, targeting Gleam
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
mod delivery;

use super::types::{
    Generation, Interest, ReadinessBuildError, ReadinessError, ReadinessToken, errno,
};
use crate::atom::Atom;
use crate::scheduler::SharedState;
use mio::unix::SourceFd;
use mio::{Events, Poll, Registry, Waker};
use std::os::fd::RawFd;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex, Weak};
use std::thread::JoinHandle;

pub(super) const READINESS_POLL_THREAD_PREFIX: &str = "beamr-readiness";
const WAKER_TOKEN: mio::Token = mio::Token(usize::MAX);

static NEXT_CONSUMER_ID: AtomicU64 = AtomicU64::new(1);

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub(in crate::scheduler) struct ServiceConsumerId(u64);

impl ServiceConsumerId {
    pub(in crate::scheduler) fn mint() -> Self {
        Self(NEXT_CONSUMER_ID.fetch_add(1, Ordering::Relaxed))
    }
}

/// Identity copied into every registration so a shared poller routes home.
#[derive(Clone)]
pub(in crate::scheduler) struct RouteHome {
    pub(in crate::scheduler) scheduler: Weak<SharedState>,
    pub(in crate::scheduler) consumer: ServiceConsumerId,
}

#[derive(Copy, Clone, Eq, PartialEq)]
enum RecordState {
    Live,
    Draining,
}

struct Registration {
    fd: RawFd,
    generation: Generation,
    pid: u64,
    marker: Atom,
    armed: Interest,
    route: RouteHome,
    state: RecordState,
}

#[derive(Default)]
struct Slot {
    generation: u64,
    record: Option<Registration>,
}

#[derive(Default)]
struct RegistrationTable {
    slots: Vec<Slot>,
}

impl RegistrationTable {
    fn vacant_slot(&mut self) -> usize {
        if let Some(index) = self.slots.iter().position(|slot| slot.record.is_none()) {
            index
        } else {
            self.slots.push(Slot::default());
            self.slots.len() - 1
        }
    }
}

/// The one poller, cloned registry, waker, and registration table.
pub(super) struct ReadinessCore {
    poll: Mutex<Poll>,
    registry: Registry,
    waker: Waker,
    table: Mutex<RegistrationTable>,
    poll_epoch: AtomicU64,
    epoch_lock: Mutex<()>,
    epoch_changed: Condvar,
    stopping: AtomicBool,
    failed: AtomicBool,
    thread: Mutex<Option<JoinHandle<()>>>,
    initial_route: Mutex<Option<RouteHome>>,
    #[cfg(test)]
    panic_in_delivery: AtomicBool,
}

impl ReadinessCore {
    pub(super) fn build(
        initial_route: Option<RouteHome>,
    ) -> Result<Arc<Self>, ReadinessBuildError> {
        let poll = Poll::new().map_err(|error| ReadinessBuildError::PollSetUnavailable {
            errno: errno(&error),
        })?;
        let registry = poll.registry().try_clone().map_err(|error| {
            ReadinessBuildError::PollSetUnavailable {
                errno: errno(&error),
            }
        })?;
        let waker = Waker::new(poll.registry(), WAKER_TOKEN).map_err(|error| {
            ReadinessBuildError::PollSetUnavailable {
                errno: errno(&error),
            }
        })?;
        let core = Arc::new(Self {
            poll: Mutex::new(poll),
            registry,
            waker,
            table: Mutex::new(RegistrationTable::default()),
            poll_epoch: AtomicU64::new(0),
            epoch_lock: Mutex::new(()),
            epoch_changed: Condvar::new(),
            stopping: AtomicBool::new(false),
            failed: AtomicBool::new(false),
            thread: Mutex::new(None),
            initial_route: Mutex::new(initial_route),
            #[cfg(test)]
            panic_in_delivery: AtomicBool::new(false),
        });
        let thread_core = Arc::clone(&core);
        let handle = std::thread::Builder::new()
            .name(format!("{READINESS_POLL_THREAD_PREFIX}-poll"))
            .spawn(move || {
                let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    thread_core.poll_loop()
                }));
                if !thread_core.stopping.load(Ordering::Acquire)
                    && (outcome.is_err() || outcome.is_ok_and(|result| result.is_err()))
                {
                    thread_core.failed.store(true, Ordering::Release);
                    thread_core.notify_epoch_waiters();
                }
            })
            .map_err(|error| ReadinessBuildError::PollSetUnavailable {
                errno: errno(&error),
            })?;
        match core.thread.lock() {
            Ok(mut slot) => *slot = Some(handle),
            Err(poisoned) => {
                core.failed.store(true, Ordering::Release);
                let mut slot = poisoned.into_inner();
                *slot = Some(handle);
            }
        }
        Ok(core)
    }

    pub(super) fn take_initial_route(&self) -> Option<RouteHome> {
        match self.initial_route.lock() {
            Ok(mut route) => route.take(),
            Err(_) => None,
        }
    }

    pub(super) fn register(
        &self,
        route: RouteHome,
        fd: RawFd,
        interest: Interest,
        pid: u64,
        marker: Atom,
    ) -> Result<ReadinessToken, ReadinessError> {
        self.check_available()?;
        let mut table = self.lock_table_for_mutation()?;
        let index = table.vacant_slot();
        let slot = &mut table.slots[index];
        slot.generation = slot.generation.wrapping_add(1).max(1);
        let token = ReadinessToken {
            slot: index as u32,
            generation: Generation(slot.generation),
        };
        let mut source = SourceFd(&fd);
        self.registry
            .register(&mut source, token.mio_token(), interest.as_mio())
            .map_err(|error| ReadinessError::Register {
                errno: errno(&error),
            })?;
        slot.record = Some(Registration {
            fd,
            generation: token.generation,
            pid,
            marker,
            armed: interest,
            route,
            state: RecordState::Live,
        });
        Ok(token)
    }

    pub(super) fn rearm(
        &self,
        token: &ReadinessToken,
        interest: Interest,
    ) -> Result<(), ReadinessError> {
        self.check_available()?;
        let mut table = self.lock_table_for_mutation()?;
        let record = table
            .slots
            .get_mut(token.slot as usize)
            .and_then(|slot| slot.record.as_mut())
            .filter(|record| {
                record.generation == token.generation && record.state == RecordState::Live
            })
            .ok_or(ReadinessError::UnknownToken)?;
        let mut source = SourceFd(&record.fd);
        self.registry
            .reregister(&mut source, token.mio_token(), interest.as_mio())
            .map_err(|error| ReadinessError::Register {
                errno: errno(&error),
            })?;
        record.armed = interest;
        Ok(())
    }

    fn check_available(&self) -> Result<(), ReadinessError> {
        if self.failed.load(Ordering::Acquire) || self.stopping.load(Ordering::Acquire) {
            Err(ReadinessError::ServiceFailed)
        } else {
            Ok(())
        }
    }

    fn lock_table_for_mutation(
        &self,
    ) -> Result<std::sync::MutexGuard<'_, RegistrationTable>, ReadinessError> {
        match self.table.lock() {
            Ok(table) => Ok(table),
            Err(_) => {
                self.failed.store(true, Ordering::Release);
                Err(ReadinessError::ServiceFailed)
            }
        }
    }

    fn lock_table_for_tombstone(&self) -> (std::sync::MutexGuard<'_, RegistrationTable>, bool) {
        let failed = self.failed.load(Ordering::Acquire);
        match self.table.lock() {
            Ok(table) => (table, failed),
            Err(poisoned) => {
                // Publish FAILED before the one sanctioned poisoned-table
                // recovery: every subsequent register/rearm refuses pre-lock.
                self.failed.store(true, Ordering::Release);
                (poisoned.into_inner(), true)
            }
        }
    }

    pub(super) fn deregister(&self, token: ReadinessToken) {
        let bumped = {
            let (mut table, failed) = self.lock_table_for_tombstone();
            let bumped = self.tombstone(&mut table, token);
            // §4.2 step 2: the epoch is read UNDER the table lock, after the
            // tombstone — the wait then demands an iteration that starts
            // after the tombstone is visible (reading any later epoch only
            // strengthens the bound).
            let epoch = self.poll_epoch.load(Ordering::Acquire);
            (bumped, failed, epoch)
        };
        let (Some(bumped_generation), failed, epoch) = bumped else {
            return;
        };
        if !failed {
            self.wake_and_wait(epoch);
        }
        self.free_draining(token.slot as usize, bumped_generation);
    }

    fn tombstone(&self, table: &mut RegistrationTable, token: ReadinessToken) -> Option<u64> {
        let slot = table.slots.get_mut(token.slot as usize)?;
        let record = slot.record.as_mut()?;
        if record.generation != token.generation {
            return None;
        }
        slot.generation = slot.generation.wrapping_add(1).max(1);
        record.generation = Generation(slot.generation);
        record.state = RecordState::Draining;
        record.armed = Interest(0);
        let mut source = SourceFd(&record.fd);
        let _ = self.registry.deregister(&mut source);
        Some(slot.generation)
    }

    fn free_draining(&self, index: usize, generation: u64) {
        let (mut table, _failed) = self.lock_table_for_tombstone();
        if let Some(slot) = table.slots.get_mut(index)
            && slot.generation == generation
            && slot
                .record
                .as_ref()
                .is_some_and(|record| record.state == RecordState::Draining)
        {
            slot.record = None;
        }
    }

    pub(super) fn deregister_all_for(&self, consumer: ServiceConsumerId) {
        self.deregister_matching(|record| record.route.consumer == consumer);
    }

    pub(super) fn deregister_pid(&self, consumer: ServiceConsumerId, pid: u64) {
        self.deregister_matching(|record| record.route.consumer == consumer && record.pid == pid);
    }

    fn deregister_matching(&self, predicate: impl Fn(&Registration) -> bool) {
        let (mut table, failed) = self.lock_table_for_tombstone();
        let mut draining = Vec::new();
        for (index, slot) in table.slots.iter_mut().enumerate() {
            let Some(record) = slot.record.as_mut() else {
                continue;
            };
            if record.state != RecordState::Live || !predicate(record) {
                continue;
            }
            slot.generation = slot.generation.wrapping_add(1).max(1);
            record.generation = Generation(slot.generation);
            record.state = RecordState::Draining;
            record.armed = Interest(0);
            let mut source = SourceFd(&record.fd);
            let _ = self.registry.deregister(&mut source);
            draining.push((index, slot.generation));
        }
        // §4.2 step 2: read the epoch under the lock, after the batch
        // tombstone (same discipline as `deregister`).
        let epoch = self.poll_epoch.load(Ordering::Acquire);
        drop(table);
        if !failed && !draining.is_empty() {
            self.wake_and_wait(epoch);
        }
        for (index, generation) in draining {
            self.free_draining(index, generation);
        }
    }

    /// Wake every `wake_and_wait` waiter AFTER a predicate-visible state
    /// change (`poll_epoch`, `failed`, `stopping`). The notifier MUST pass
    /// through `epoch_lock` between the state change and the notify: a
    /// waiter holds that lock from its predicate check until `wait()`
    /// releases it atomically, so an unlocked notify can land in the
    /// check→wait window and be LOST — and the poller is tickless, so a
    /// lost wake has no second chance and the dereg handshake hangs
    /// forever. Acquiring-and-dropping the lock forces the notify to
    /// happen-after the waiter is actually waiting (or the waiter to see
    /// the new state before it waits).
    fn notify_epoch_waiters(&self) {
        if let Ok(guard) = self.epoch_lock.lock() {
            drop(guard);
        }
        self.epoch_changed.notify_all();
    }

    fn wake_and_wait(&self, epoch: u64) {
        let _ = self.waker.wake();
        let Ok(mut guard) = self.epoch_lock.lock() else {
            return;
        };
        while self.poll_epoch.load(Ordering::Acquire) <= epoch
            && !self.failed.load(Ordering::Acquire)
            && !self.stopping.load(Ordering::Acquire)
        {
            match self.epoch_changed.wait(guard) {
                Ok(next) => guard = next,
                Err(_) => return,
            }
        }
    }

    fn poll_loop(&self) -> std::io::Result<()> {
        let mut events = Events::with_capacity(256);
        loop {
            self.poll_epoch.fetch_add(1, Ordering::AcqRel);
            self.notify_epoch_waiters();
            if self.stopping.load(Ordering::Acquire) {
                return Ok(());
            }
            let poll_result = match self.poll.lock() {
                Ok(mut poll) => poll.poll(&mut events, None),
                Err(_) => return Err(std::io::Error::other("readiness poll lock poisoned")),
            };
            if let Err(error) = poll_result {
                if error.kind() == std::io::ErrorKind::Interrupted {
                    continue;
                }
                return Err(error);
            }
            for event in &events {
                if event.token() != WAKER_TOKEN {
                    self.deliver_event(event)?;
                }
            }
        }
    }

    pub(super) fn shutdown(&self) {
        self.stopping.store(true, Ordering::Release);
        let _ = self.waker.wake();
        let handle = match self.thread.lock() {
            Ok(mut thread) => thread.take(),
            Err(mut poisoned) => poisoned.get_mut().take(),
        };
        if let Some(handle) = handle {
            let _ = handle.join();
        }
        self.notify_epoch_waiters();
    }

    pub(super) fn poll_thread_names(&self) -> Vec<String> {
        if self.failed.load(Ordering::Acquire) || self.stopping.load(Ordering::Acquire) {
            return Vec::new();
        }
        let live = match self.thread.lock() {
            Ok(thread) => thread.as_ref().is_some_and(|handle| !handle.is_finished()),
            Err(_) => false,
        };
        if live {
            vec![format!("{READINESS_POLL_THREAD_PREFIX}-poll")]
        } else {
            Vec::new()
        }
    }

    #[cfg(test)]
    pub(super) fn live_registration_count(&self) -> usize {
        match self.table.lock() {
            Ok(table) => table
                .slots
                .iter()
                .filter(|slot| {
                    slot.record
                        .as_ref()
                        .is_some_and(|record| record.state == RecordState::Live)
                })
                .count(),
            Err(poisoned) if self.failed.load(Ordering::Acquire) => poisoned
                .into_inner()
                .slots
                .iter()
                .filter(|slot| {
                    slot.record
                        .as_ref()
                        .is_some_and(|record| record.state == RecordState::Live)
                })
                .count(),
            Err(_) => 0,
        }
    }

    #[cfg(test)]
    pub(super) fn poll_iterations(&self) -> u64 {
        self.poll_epoch.load(Ordering::Acquire)
    }

    #[cfg(test)]
    pub(super) fn panic_next_delivery(&self) {
        self.panic_in_delivery.store(true, Ordering::Release);
    }
}

impl Drop for ReadinessCore {
    fn drop(&mut self) {
        self.shutdown();
    }
}