ipc_ring 0.2.1

High-performance memory-mapped SPSC ring buffer for Unix IPC
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
#![cfg(feature = "failpoints")]

use ipc_ring::{failpoints_enabled, IpcError, RingReader, RingWriter};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Barrier};
use std::{thread, time::Duration};

const PAYLOAD: &[u8] = b"failpoint message";

struct FailpointGuard {
    name: &'static str,
}

impl FailpointGuard {
    fn new(name: &'static str) -> Self {
        fail::cfg(name, "panic").unwrap();
        Self { name }
    }
}

impl Drop for FailpointGuard {
    fn drop(&mut self) {
        fail::remove(self.name);
    }
}

#[derive(Clone, Copy)]
struct WriterCase {
    name: &'static str,
    needs_wrap: bool,
    expect_message_visible: bool,
}

#[derive(Clone, Copy)]
struct ReaderCase {
    name: &'static str,
    needs_wrap: bool,
    message_survives: bool,
}

fn shared_tmp_dir() -> PathBuf {
    #[cfg(target_os = "linux")]
    {
        let shm = Path::new("/dev/shm");
        if shm.exists() && shm.is_dir() {
            return shm.to_path_buf();
        }
    }
    PathBuf::from("/tmp")
}

fn unique_ring_path() -> PathBuf {
    static COUNTER: AtomicU32 = AtomicU32::new(0);
    let dir = shared_tmp_dir();
    dir.join(format!(
        "ipc_ring_failpoint_{}_{}",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::SeqCst)
    ))
}

fn cleanup(path: &Path) {
    let _ = std::fs::remove_file(path);
}

fn prime_wrap(writer: &mut RingWriter, reader: &mut RingReader) {
    let mut buf = Vec::new();
    let prep = vec![0u8; 24];
    for _ in 0..2 {
        writer.try_push(&prep).expect("prime push");
        let observed = reader.try_pop(&mut buf).expect("prime pop");
        assert_eq!(observed, Some(prep.len()), "prime pop length mismatch");
        buf.clear();
    }
}

fn run_writer_case(case: WriterCase) {
    assert!(
        failpoints_enabled(),
        "crate compiled without failpoints feature"
    );

    let path = unique_ring_path();
    cleanup(&path);

    let mut writer = RingWriter::create(&path, 64).expect("create ring");
    let mut reader = RingReader::open(&path).expect("open ring");

    if case.needs_wrap {
        prime_wrap(&mut writer, &mut reader);
    }

    {
        let _guard = FailpointGuard::new(case.name);
        let smoke = std::panic::catch_unwind(|| {
            fail::fail_point!(case.name);
        });
        assert!(smoke.is_err(), "failpoint {} did not panic", case.name);

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            writer.try_push(PAYLOAD).expect("writer push failed");
        }));
        assert!(
            result.is_err(),
            "failpoint {} did not trigger during push",
            case.name
        );
    }

    let mut buf = Vec::new();
    let observed = reader
        .try_pop(&mut buf)
        .expect("reader pop after failpoint");
    if case.expect_message_visible {
        assert_eq!(
            observed,
            Some(PAYLOAD.len()),
            "failpoint {} expected message visibility",
            case.name
        );
        assert_eq!(buf, PAYLOAD, "failpoint {} payload mismatch", case.name);
        buf.clear();
        assert!(
            reader.try_pop(&mut buf).expect("second pop").is_none(),
            "ring not empty after consuming message for {}",
            case.name
        );
    } else {
        assert!(
            observed.is_none(),
            "failpoint {} unexpectedly left readable data",
            case.name
        );
    }

    cleanup(&path);
}

fn run_reader_case(case: ReaderCase) {
    assert!(
        failpoints_enabled(),
        "crate compiled without failpoints feature"
    );

    let path = unique_ring_path();
    cleanup(&path);

    let mut writer = RingWriter::create(&path, 64).expect("create ring");
    let mut reader = RingReader::open(&path).expect("open ring");

    if case.needs_wrap {
        prime_wrap(&mut writer, &mut reader);
    }

    writer.try_push(PAYLOAD).expect("prepare payload");

    {
        let _guard = FailpointGuard::new(case.name);
        let smoke = std::panic::catch_unwind(|| {
            fail::fail_point!(case.name);
        });
        assert!(smoke.is_err(), "failpoint {} did not panic", case.name);

        let mut buf = Vec::new();
        let result =
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| reader.try_pop(&mut buf)));
        assert!(
            result.is_err(),
            "failpoint {} did not trigger during pop",
            case.name
        );
    }

    drop(reader);

    let mut reader = RingReader::open(&path).expect("reopen reader");
    let mut buf = Vec::new();
    let observed = reader
        .try_pop(&mut buf)
        .expect("reader pop after failpoint");
    if case.message_survives {
        assert_eq!(
            observed,
            Some(PAYLOAD.len()),
            "failpoint {} should leave payload readable",
            case.name
        );
        assert_eq!(buf, PAYLOAD, "failpoint {} payload mismatch", case.name);
        buf.clear();
        assert!(
            reader.try_pop(&mut buf).expect("second pop").is_none(),
            "ring not empty after replay for {}",
            case.name
        );
    } else {
        assert!(
            observed.is_none(),
            "failpoint {} should have consumed payload",
            case.name
        );
    }

    cleanup(&path);
}

#[test]
fn writer_failpoints_cover_crash_windows() {
    let _scenario = fail::FailScenario::setup();
    let cases = [
        WriterCase {
            name: "ring_writer::after_wrap_publish",
            needs_wrap: true,
            expect_message_visible: false,
        },
        WriterCase {
            name: "ring_writer::after_wrap_advance",
            needs_wrap: true,
            expect_message_visible: false,
        },
        WriterCase {
            name: "ring_writer::after_wrap_signal",
            needs_wrap: true,
            expect_message_visible: false,
        },
        WriterCase {
            name: "ring_writer::after_write_header",
            needs_wrap: false,
            expect_message_visible: false,
        },
        WriterCase {
            name: "ring_writer::after_write_payload",
            needs_wrap: false,
            expect_message_visible: false,
        },
        WriterCase {
            name: "ring_writer::after_publish_header",
            needs_wrap: false,
            expect_message_visible: false,
        },
        WriterCase {
            name: "ring_writer::after_write_advance",
            needs_wrap: false,
            expect_message_visible: true,
        },
        WriterCase {
            name: "ring_writer::after_data_signal",
            needs_wrap: false,
            expect_message_visible: true,
        },
    ];

    for case in cases {
        run_writer_case(case);
    }
}

// Tests above rely on failpoints to simulate in-flight crashes. The following tests exercise
// observable error paths without failpoints.

#[test]
fn writer_reports_timeout_when_full() {
    let _scenario = fail::FailScenario::setup();
    let path = unique_ring_path();
    cleanup(&path);
    let mut writer = RingWriter::create(&path, 64).expect("create ring");

    let filler = vec![0xCC; 60];
    while writer.try_push(&filler).is_ok() {}

    let err = writer
        .push(PAYLOAD, Some(Duration::from_millis(20)))
        .unwrap_err();
    assert!(
        matches!(err, IpcError::Timeout),
        "unexpected error: {err:?}"
    );

    cleanup(&path);
}

#[test]
fn writer_reports_peer_stalled_without_reader() {
    let _scenario = fail::FailScenario::setup();
    let path = unique_ring_path();
    cleanup(&path);
    let mut writer = RingWriter::create(&path, 64).expect("create ring");
    writer.set_poll_interval(Duration::from_millis(1));

    let filler = vec![0xDD; 60];
    while writer.try_push(&filler).is_ok() {}

    let err = writer.push(PAYLOAD, None).unwrap_err();
    assert!(
        matches!(err, IpcError::PeerStalled),
        "unexpected error: {err:?}"
    );

    cleanup(&path);
}

#[test]
fn reader_reports_timeout_when_empty() {
    let _scenario = fail::FailScenario::setup();
    let path = unique_ring_path();
    cleanup(&path);

    let writer = RingWriter::create(&path, 64).expect("create ring");
    drop(writer);

    let mut reader = RingReader::open(&path).expect("open reader");
    reader.set_poll_interval(Duration::from_millis(1));
    let mut buf = Vec::new();
    let err = reader
        .pop(&mut buf, Some(Duration::from_millis(20)))
        .unwrap_err();
    assert!(
        matches!(err, IpcError::Timeout),
        "unexpected error: {err:?}"
    );

    cleanup(&path);
}

#[test]
fn writer_self_wake_after_reader_crash() {
    let _scenario = fail::FailScenario::setup();
    assert!(failpoints_enabled(), "failpoints feature disabled");

    let path = unique_ring_path();
    cleanup(&path);

    let mut writer = RingWriter::create(&path, 64).expect("create ring");
    writer.set_poll_interval(Duration::from_millis(1));

    let filler = vec![0xDD; 40];
    writer.try_push(&filler).expect("filler push");

    let barrier = Arc::new(Barrier::new(2));
    let writer_barrier = barrier.clone();
    let writer_handle = thread::spawn(move || -> Result<(), IpcError> {
        writer_barrier.wait();
        writer.push(PAYLOAD, None)
    });

    let reader_barrier = barrier.clone();
    let reader_path = path.clone();
    let reader_handle = thread::spawn(move || {
        let mut reader = RingReader::open(&reader_path).expect("open reader");
        reader_barrier.wait();
        let mut buf = Vec::new();
        let guard = FailpointGuard::new("ring_reader::after_read_advance");
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            reader.pop(&mut buf, None).expect("reader pop");
        }));
        drop(guard);
        result
    });

    let writer_result = writer_handle.join().expect("writer thread panicked");
    assert!(
        writer_result.is_ok(),
        "writer push failed: {writer_result:?}"
    );

    let reader_result = reader_handle.join().expect("reader thread join failed");
    assert!(reader_result.is_err(), "reader thread did not panic");

    let mut verify_reader = RingReader::open(&path).expect("verify reader open");
    let mut buf = Vec::new();
    let observed = verify_reader.try_pop(&mut buf).expect("verify pop");
    assert_eq!(
        observed,
        Some(PAYLOAD.len()),
        "payload missing after recovery"
    );
    assert_eq!(buf.as_slice(), PAYLOAD, "payload mismatch after recovery");

    cleanup(&path);
}

#[test]
fn reader_failpoints_cover_crash_windows() {
    let _scenario = fail::FailScenario::setup();
    let cases = [
        ReaderCase {
            name: "ring_reader::after_wrap_read_advance",
            needs_wrap: true,
            message_survives: true,
        },
        ReaderCase {
            name: "ring_reader::after_wrap_space_signal",
            needs_wrap: true,
            message_survives: true,
        },
        ReaderCase {
            name: "ring_reader::before_read_advance",
            needs_wrap: false,
            message_survives: true,
        },
        ReaderCase {
            name: "ring_reader::after_read_advance",
            needs_wrap: false,
            message_survives: false,
        },
        ReaderCase {
            name: "ring_reader::after_space_signal",
            needs_wrap: false,
            message_survives: false,
        },
    ];

    for case in cases {
        run_reader_case(case);
    }
}

#[test]
fn writer_create_failpoint_leaves_valid_ring() {
    let _scenario = fail::FailScenario::setup();
    assert!(failpoints_enabled(), "failpoints feature disabled");

    let path = unique_ring_path();
    cleanup(&path);

    {
        let _guard = FailpointGuard::new("ring_writer::create::after_init");
        let result = std::panic::catch_unwind(|| {
            RingWriter::create(&path, 4096).expect("create should panic via failpoint");
        });
        assert!(
            result.is_err(),
            "create failpoint did not produce panic as expected"
        );
    }

    // After the panic, the ring layout is still initialized. Readers can reopen.
    let mut reader = RingReader::open(&path).expect("reader should open after writer crash");
    let mut buf = Vec::new();
    assert!(
        reader.try_pop(&mut buf).expect("post-crash pop").is_none(),
        "ring unexpectedly contains data after create failpoint"
    );

    cleanup(&path);
}

#[test]
fn reader_open_failpoint_allows_retry() {
    let _scenario = fail::FailScenario::setup();
    assert!(failpoints_enabled(), "failpoints feature disabled");

    let path = unique_ring_path();
    cleanup(&path);

    let writer = RingWriter::create(&path, 4096).expect("create ring");
    drop(writer);

    {
        let _guard = FailpointGuard::new("ring_reader::open::after_map");
        let result = std::panic::catch_unwind(|| {
            RingReader::open(&path).expect("open should panic via failpoint");
        });
        assert!(
            result.is_err(),
            "reader open failpoint did not produce panic as expected"
        );
    }

    // With the failpoint removed the reader should open cleanly.
    let mut reader = RingReader::open(&path).expect("retry reader open");
    let mut buf = Vec::new();
    assert!(
        reader.try_pop(&mut buf).expect("post-retry pop").is_none(),
        "ring unexpectedly contains data after reader open retry"
    );

    cleanup(&path);
}