ice-rpc 0.2.2

Zero-copy RPC over iceoryx2 with automatic reconnection, service discovery and HTTP gateway
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
//! Node crash monitoring through a cross-platform kernel mutex.
//!
//! # Principle
//!
//! The provider creates a named Mutex and keeps it held in a dedicated
//! thread. On process crash, the kernel kills all threads → the mutex
//! is abandoned → the client detects the absence of the mutex → crash confirmed.
//!
//! | OS      | Provider                      | Watcher                       |
//! |---------|-------------------------------|-------------------------------|
//! | Windows | `CreateMutexA` + dedicated thread | `OpenMutexA` existence check  |
//! | Linux   | `open` + `flock(LOCK_EX\|NB)` | `open` + `flock(LOCK_EX\|NB)` |
//! | macOS   | `open` + `flock(LOCK_EX\|NB)` | `open` + `flock(LOCK_EX\|NB)` |

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use crate::node_supervisor::fire as fire_reconnect_callbacks;
use crate::types::NodeId;

/// Polling interval of the watcher (ms).
pub const LOCK_WATCHER_POLL_MS: u64 = 100;

/// Prefix of the mutex/lock file name.
pub(crate) const LOCK_NAME_PREFIX: &str = "ice_rpc_node_";

#[cfg(windows)]
mod platform {
    use std::ffi::CString;
    use windows_sys::Win32::Foundation::{
        CloseHandle, ERROR_ALREADY_EXISTS, HANDLE, WAIT_OBJECT_0,
    };
    use windows_sys::Win32::System::Threading::{CreateMutexA, WaitForSingleObject, INFINITE};
    use windows_sys::Win32::System::WindowsProgramming::OpenMutexA;

    const SYNCHRONIZE: u32 = 0x00100000;

    /// Creates a Win32 Mutex and keeps it held in a dedicated thread.
    pub fn acquire(name: &str) -> Result<(), String> {
        let (tx, rx) = std::sync::mpsc::channel::<Result<(), String>>();
        let name_owned = name.to_string();

        std::thread::Builder::new()
            .name(format!("node-lock-{}", name_owned))
            .spawn(move || {
                let cname = match CString::new(name_owned.as_str()) {
                    Ok(s) => s,
                    Err(e) => {
                        let _ = tx.send(Err(format!("CString: {}", e)));
                        return;
                    }
                };

                let handle: HANDLE =
                    unsafe { CreateMutexA(std::ptr::null(), 0, cname.as_ptr() as *const u8) };
                if handle.is_null() {
                    let _ = tx.send(Err(format!(
                        "[NodeLock] CreateMutexA('{}') failed",
                        name_owned
                    )));
                    return;
                }

                let last_err = unsafe { windows_sys::Win32::Foundation::GetLastError() };
                if last_err == ERROR_ALREADY_EXISTS {
                    unsafe {
                        CloseHandle(handle);
                    }
                    let _ = tx.send(Err(format!(
                        "[NodeLock] Mutex '{}' already created by another process",
                        name_owned
                    )));
                    return;
                }

                let rc = unsafe { WaitForSingleObject(handle, INFINITE) };
                if rc != WAIT_OBJECT_0 {
                    unsafe {
                        CloseHandle(handle);
                    }
                    let _ = tx.send(Err(format!(
                        "[NodeLock] WaitForSingleObject failed rc={}",
                        rc
                    )));
                    return;
                }

                let _ = tx.send(Ok(()));
                loop {
                    std::thread::sleep(std::time::Duration::from_secs(3600));
                }
            })
            .map_err(|e| format!("[NodeLock] spawn thread failed: {}", e))?;

        rx.recv()
            .map_err(|e| format!("[NodeLock] channel recv failed: {}", e))?
    }

    /// Checks whether the provider is still alive through the Win32 mutex.
    pub fn is_alive(name: &str) -> bool {
        let cname = match CString::new(name) {
            Ok(s) => s,
            Err(_) => return false,
        };
        let handle: HANDLE = unsafe { OpenMutexA(SYNCHRONIZE, 0, cname.as_ptr() as *const u8) };
        if handle.is_null() {
            return false;
        }
        unsafe {
            CloseHandle(handle);
        }
        true
    }

    /// Releases the mutex (called during a clean shutdown).
    pub fn release(_name: &str) {
        // The mutex is released automatically by the kernel at process end.
        // For a clean shutdown, the dedicated thread cannot be easily killed.
        // We let the kernel clean up.
    }
}

#[cfg(unix)]
mod platform {
    use std::fs::OpenOptions;
    use std::os::unix::io::IntoRawFd;

    /// Builds the lock file path.
    fn lock_path(name: &str) -> String {
        format!("/tmp/{}.lock", name)
    }

    /// Creates the lock file and acquires an exclusive flock.
    pub fn acquire(name: &str) -> Result<(), String> {
        let path = lock_path(name);
        let file = OpenOptions::new()
            .create(true)
            .write(true)
            .open(&path)
            .map_err(|e| format!("[NodeLock] open('{}') failed: {}", path, e))?;

        let fd = file.into_raw_fd();
        let rc = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
        if rc == 0 {
            Ok(())
        } else {
            unsafe {
                libc::close(fd);
            }
            Err(format!("[NodeLock] flock('{}') failed: already held", path))
        }
    }

    /// Checks whether the flock is still held (provider alive).
    pub fn is_alive(name: &str) -> bool {
        let path = lock_path(name);
        let file = match OpenOptions::new().write(true).open(&path) {
            Ok(f) => f,
            Err(_) => return false,
        };
        let fd = file.into_raw_fd();
        let rc = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
        if rc == 0 {
            unsafe {
                libc::flock(fd, libc::LOCK_UN);
                libc::close(fd);
            }
            false
        } else {
            unsafe {
                libc::close(fd);
            }
            true
        }
    }

    /// Releases the flock (called during a clean shutdown).
    pub fn release(name: &str) {
        let path = lock_path(name);
        // Removes the lock file to release immediately.
        let _ = std::fs::remove_file(&path);
    }
}

static GLOBAL_LOCK_NAME: OnceLock<String> = OnceLock::new();

/// Acquires the kernel lock for this node.
///
/// Idempotent: if already acquired, returns the existing name.
///
/// # Returns
/// * `Ok(String)` — Name of the lock.
/// * `Err(String)` — Acquisition failure.
pub fn acquire_global_node_lock(node_id: NodeId) -> Result<String, String> {
    if let Some(name) = GLOBAL_LOCK_NAME.get() {
        return Ok(name.clone());
    }
    let lock_name = format!("{}{}", LOCK_NAME_PREFIX, node_id.0);
    platform::acquire(&lock_name)?;
    let _ = GLOBAL_LOCK_NAME.set(lock_name.clone());
    Ok(lock_name)
}

/// Explicitly releases the kernel lock.
///
/// Called during a clean shutdown so that `is_node_alive()`
/// returns `false` immediately (without waiting for process end).
pub fn release_global_node_lock() {
    if let Some(name) = GLOBAL_LOCK_NAME.get() {
        platform::release(name);
    }
}

/// Returns `true` when this process acquired the global node lock, i.e. when
/// it published a discovery registry as a Provider. Pure consumers never
/// acquire the lock and therefore have nothing to announce on shutdown.
pub fn has_global_node_lock() -> bool {
    GLOBAL_LOCK_NAME.get().is_some()
}

/// Checks whether a provider node is still alive through its kernel lock.
pub fn is_node_alive(lock_name: &str) -> bool {
    platform::is_alive(lock_name)
}

/// Common monitoring loop (Tokio and std::thread).
fn watcher_loop(
    node_id: NodeId,
    lock_name: &str,
    running: &Arc<AtomicBool>,
    cancel: &crate::rt::CancellationToken,
) {
    let poll_interval = Duration::from_millis(LOCK_WATCHER_POLL_MS);

    loop {
        if cancel.is_cancelled() {
            break;
        }
        if !running.load(Ordering::Relaxed) {
            break;
        }

        if !is_node_alive(lock_name) {
            log::warn!("[NodeLockWatcher] CRASH DETECTED for Node {}", node_id);
            crate::locator::ServiceLocator::global()
                .node_discovery()
                .invalidate_node_services(node_id);
            fire_reconnect_callbacks(node_id.0);
            running.store(false, Ordering::Relaxed);
            break;
        }

        std::thread::sleep(poll_interval);
    }
}

/// Crash watcher for a remote provider node.
pub struct NodeLockWatcher {
    node_id: NodeId,
    lock_name: String,
    running: Arc<AtomicBool>,
}

impl NodeLockWatcher {
    /// Launches a watcher in a blocking thread (runtime-agnostic).
    pub fn spawn(node_id: NodeId, lock_name: String) -> Self {
        let running = Arc::new(AtomicBool::new(true));
        let running_clone = running.clone();
        let lock_name_clone = lock_name.clone();
        let cancel = crate::global_cancel_token().clone();

        let handle = crate::rt::spawn_blocking(move || {
            watcher_loop(node_id, &lock_name_clone, &running_clone, &cancel);
        });

        crate::locator::ServiceLocator::global().register_shutdown_handle(handle);
        Self {
            node_id,
            lock_name,
            running,
        }
    }

    /// Stops the watcher.
    pub fn stop(&self) {
        self.running.store(false, Ordering::Relaxed);
    }

    /// Returns `true` if the watcher is active.
    #[inline]
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::Relaxed)
    }

    /// Returns the name of the watched lock.
    #[inline]
    pub fn lock_name(&self) -> &str {
        &self.lock_name
    }

    /// Returns the id of the watched node.
    #[inline]
    pub fn node_id(&self) -> NodeId {
        self.node_id
    }
}

impl Drop for NodeLockWatcher {
    fn drop(&mut self) {
        self.stop();
    }
}

fn watcher_registry() -> &'static std::sync::Mutex<std::collections::HashMap<u32, NodeLockWatcher>>
{
    static REGISTRY: OnceLock<std::sync::Mutex<std::collections::HashMap<u32, NodeLockWatcher>>> =
        OnceLock::new();
    REGISTRY.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
}

/// Registers a lock watcher for a remote node.
pub fn register_node_lock_watcher(node_id: NodeId, lock_name: String) {
    if node_id == NodeId::current() {
        return;
    }
    let mut registry = match watcher_registry().lock() {
        Ok(r) => r,
        Err(_) => return,
    };
    if let Some(existing) = registry.get(&node_id.0) {
        if existing.is_running() {
            return;
        }
    }
    // The runtime-agnostic blocking facade works with or without an active
    // async runtime, so a single path is sufficient.
    let watcher = NodeLockWatcher::spawn(node_id, lock_name);
    registry.insert(node_id.0, watcher);
}

/// Removes the watcher of a node.
pub fn unregister_node_lock_watcher(node_id: NodeId) {
    if let Ok(mut registry) = watcher_registry().lock() {
        if let Some(watcher) = registry.remove(&node_id.0) {
            watcher.stop();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lock_name_format() {
        assert_eq!(
            format!("{}{}", LOCK_NAME_PREFIX, 12345u32),
            "ice_rpc_node_12345"
        );
    }

    #[test]
    fn is_alive_unknown_lock_returns_false() {
        assert!(!is_node_alive("ice_rpc_node_99999999_unknown"));
    }

    #[test]
    fn acquire_then_detected_alive() {
        // Use a unique lock name (PID + monotonic counter) so this test is
        // isolated from the global lock name shared by other tests running in
        // parallel. Another test may call release_global_node_lock() and remove
        // the lock file, which would otherwise race with this assertion.
        static COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
        let unique = std::process::id() ^ (COUNTER.fetch_add(1, Ordering::SeqCst) << 16);
        let node_id = NodeId(unique);
        let lock_name = format!("{}{}", LOCK_NAME_PREFIX, node_id.0);

        platform::acquire(&lock_name)
            .unwrap_or_else(|e| panic!("failed to acquire node lock: {}", e));

        // Bounded wait: the kernel lock must become observable shortly after
        // acquisition. Retrying avoids flakiness on slow CI runners.
        let alive = (0..10).any(|_| {
            if is_node_alive(&lock_name) {
                return true;
            }
            std::thread::sleep(std::time::Duration::from_millis(100));
            false
        });
        assert!(
            alive,
            "The node must be detected alive (lock '{}')",
            lock_name
        );

        platform::release(&lock_name);

        // On Unix, release() removes the lock file, so the node must become
        // undetectable. On Windows the kernel releases the mutex only at process
        // end (the holding thread keeps it), so this check is Unix-only.
        if cfg!(unix) {
            assert!(
                !is_node_alive(&lock_name),
                "The node must be detected dead after release (lock '{}')",
                lock_name
            );
        }
    }

    #[test]
    fn watcher_stop_flag() {
        let running = Arc::new(AtomicBool::new(true));
        assert!(running.load(Ordering::Relaxed));
        running.store(false, Ordering::Relaxed);
        assert!(!running.load(Ordering::Relaxed));
    }

    #[test]
    fn watcher_detects_missing_lock_and_stops() {
        let node_id = NodeId(0x0D1E_0002);
        let lock_name = format!("{}{}", LOCK_NAME_PREFIX, node_id.0);
        let watcher = NodeLockWatcher::spawn(node_id, lock_name);

        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        while watcher.is_running() && std::time::Instant::now() < deadline {
            std::thread::sleep(std::time::Duration::from_millis(50));
        }

        assert!(
            !watcher.is_running(),
            "watcher must detect the missing lock and stop"
        );
    }
}