ruCCL 0.21.1

Ruda collective communication algorithms and orchestration.
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
use super::network::{NetworkError, read_frame, write_frame};
use super::protocol::{
    ANY_RANK, ElementType, FLAG_P2P_CHANNEL, Frame, FrameHeader, Opcode, ProtocolError, UniqueId,
};
use std::collections::{HashMap, VecDeque};
use std::io;
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
use std::sync::mpsc::{self, RecvTimeoutError, Sender};
use std::sync::{Arc, Condvar, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

const FAILURE_POLL_INTERVAL: Duration = Duration::from_millis(50);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct AckKey {
    peer: u32,
    rail: usize,
    sequence: u64,
}

type PendingPeerAcks = Arc<Mutex<HashMap<AckKey, Sender<Result<(), String>>>>>;

#[derive(Debug)]
struct PeerSubmission {
    stream: TcpStream,
    next_sequence: u64,
}

#[derive(Debug)]
struct PeerConnection {
    peer: u32,
    rail: usize,
    submission: Mutex<PeerSubmission>,
}

#[derive(Debug)]
struct QueuedFrame {
    rail: usize,
    frame: Frame,
}

#[derive(Debug, Default)]
struct PeerInbox {
    frames: VecDeque<QueuedFrame>,
}

#[derive(Debug)]
pub(crate) struct DirectPeerMesh {
    unique_id: UniqueId,
    rank: u32,
    world_size: u32,
    rails: usize,
    connections: Vec<Vec<Option<Arc<PeerConnection>>>>,
    inbox: Arc<(Mutex<PeerInbox>, Condvar)>,
    pending_acks: PendingPeerAcks,
    failure: Arc<Mutex<Option<String>>>,
    timeout: Mutex<Duration>,
    readers: Mutex<Vec<JoinHandle<()>>>,
}

impl DirectPeerMesh {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn connect(
        listeners: Vec<TcpListener>,
        endpoints: &[Vec<SocketAddr>],
        unique_id: UniqueId,
        rank: u32,
        world_size: u32,
        rails: usize,
        timeout: Duration,
        failure: Arc<Mutex<Option<String>>>,
    ) -> Result<Self, NetworkError> {
        if endpoints.len() != world_size as usize {
            return Err(NetworkError::InvalidConfiguration(format!(
                "peer endpoint table contains {} ranks, expected {world_size}",
                endpoints.len()
            )));
        }
        if let Some((peer, endpoints)) = endpoints
            .iter()
            .enumerate()
            .find(|(_, endpoints)| endpoints.len() != rails)
        {
            return Err(NetworkError::InvalidConfiguration(format!(
                "peer endpoint table contains {} rail endpoints for rank {peer}, expected {rails}",
                endpoints.len()
            )));
        }
        if listeners.len() != 1 && listeners.len() != rails {
            return Err(NetworkError::InvalidConfiguration(format!(
                "direct peer mesh contains {} listeners, expected one shared listener or {rails} rail listeners",
                listeners.len()
            )));
        }
        let mut connections = (0..rails)
            .map(|_| vec![None; world_size as usize])
            .collect::<Vec<Vec<Option<Arc<PeerConnection>>>>>();

        for peer in 0..rank {
            for (rail, rail_connections) in connections.iter_mut().enumerate() {
                let stream = connect_peer(
                    endpoints[peer as usize][rail],
                    unique_id,
                    rank,
                    peer,
                    world_size,
                    rail,
                    timeout,
                )?;
                rail_connections[peer as usize] = Some(Arc::new(PeerConnection {
                    peer,
                    rail,
                    submission: Mutex::new(PeerSubmission {
                        stream,
                        next_sequence: 0,
                    }),
                }));
            }
        }

        for listener in &listeners {
            listener.set_nonblocking(true)?;
        }
        let expected_incoming = (world_size - rank - 1) as usize * rails;
        let deadline = Instant::now() + timeout;
        let mut accepted = 0_usize;
        while accepted < expected_incoming {
            let mut made_progress = false;
            for (listener_index, listener) in listeners.iter().enumerate() {
                match listener.accept() {
                    Ok((mut stream, _)) => {
                        made_progress = true;
                        stream.set_nonblocking(false)?;
                        stream.set_nodelay(true)?;
                        stream.set_read_timeout(Some(timeout))?;
                        stream.set_write_timeout(Some(timeout))?;
                        let join = read_frame(&mut stream)?.ok_or_else(|| {
                            NetworkError::InvalidConfiguration(
                                "direct peer closed before JOIN".into(),
                            )
                        })?;
                        let (peer, rail) =
                            validate_peer_join(&join, unique_id, rank, world_size, rails)?;
                        if listeners.len() == rails && rail != listener_index {
                            return Err(NetworkError::InvalidConfiguration(format!(
                                "direct peer rail {rail} joined dedicated listener {listener_index}"
                            )));
                        }
                        if peer <= rank {
                            return Err(NetworkError::InvalidConfiguration(format!(
                                "rank {rank} accepted direct peer {peer}; only higher ranks connect"
                            )));
                        }
                        if connections[rail][peer as usize].is_some() {
                            return Err(NetworkError::InvalidConfiguration(format!(
                                "direct peer rank {peer} rail {rail} joined twice"
                            )));
                        }
                        let ready = peer_ready_frame(unique_id, rank, peer, world_size, rail)?;
                        write_frame(&mut stream, &ready)?;
                        connections[rail][peer as usize] = Some(Arc::new(PeerConnection {
                            peer,
                            rail,
                            submission: Mutex::new(PeerSubmission {
                                stream,
                                next_sequence: 0,
                            }),
                        }));
                        accepted += 1;
                    }
                    Err(error) if error.kind() == io::ErrorKind::WouldBlock => {}
                    Err(error) => return Err(error.into()),
                }
            }
            if !made_progress {
                if Instant::now() >= deadline {
                    return Err(NetworkError::Timeout(format!(
                        "direct peer mesh setup; accepted {accepted} of {expected_incoming} connections"
                    )));
                }
                thread::park_timeout(Duration::from_millis(1));
            }
        }

        let inbox = Arc::new((Mutex::new(PeerInbox::default()), Condvar::new()));
        let pending_acks = Arc::new(Mutex::new(HashMap::new()));
        let mut readers = Vec::with_capacity((world_size.saturating_sub(1) as usize) * rails);
        for (rail, peers) in connections.iter().enumerate() {
            for (peer, connection) in peers.iter().enumerate() {
                let Some(connection) = connection else {
                    continue;
                };
                let mut read_stream = connection
                    .submission
                    .lock()
                    .map_err(|_| NetworkError::Poisoned)?
                    .stream
                    .try_clone()?;
                read_stream.set_read_timeout(None)?;
                let connection = Arc::clone(connection);
                let inbox = Arc::clone(&inbox);
                let pending_acks = Arc::clone(&pending_acks);
                let failure = Arc::clone(&failure);
                readers.push(
                    thread::Builder::new()
                        .name(format!("gx1-direct-peer-{rank}-{peer}-rail-{rail}"))
                        .spawn(move || {
                            run_peer_reader(
                                &mut read_stream,
                                unique_id,
                                rank,
                                world_size,
                                &connection,
                                &inbox,
                                &pending_acks,
                                &failure,
                            );
                        })
                        .map_err(|error| {
                            NetworkError::InvalidConfiguration(format!(
                                "cannot start direct peer reader for rank {peer} rail {rail}: {error}"
                            ))
                        })?,
                );
            }
        }

        Ok(Self {
            unique_id,
            rank,
            world_size,
            rails,
            connections,
            inbox,
            pending_acks,
            failure,
            timeout: Mutex::new(timeout),
            readers: Mutex::new(readers),
        })
    }

    pub(crate) const fn rails(&self) -> usize {
        self.rails
    }

    pub(crate) fn set_timeout(&self, timeout: Duration) -> Result<(), NetworkError> {
        if timeout.is_zero() {
            return Err(NetworkError::InvalidConfiguration(
                "direct peer timeout must be greater than zero".into(),
            ));
        }
        for peers in &self.connections {
            for connection in peers.iter().flatten() {
                connection
                    .submission
                    .lock()
                    .map_err(|_| NetworkError::Poisoned)?
                    .stream
                    .set_write_timeout(Some(timeout))?;
            }
        }
        *self.timeout.lock().map_err(|_| NetworkError::Poisoned)? = timeout;
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn send_on_rail(
        &self,
        rail: usize,
        destination: u32,
        tag: u64,
        element_type: ElementType,
        element_count: u64,
        payload: Vec<u8>,
    ) -> Result<(), NetworkError> {
        self.validate_rail(rail)?;
        self.validate_peer(destination)?;
        self.check_failure()?;
        if destination == self.rank {
            let mut header = FrameHeader::collective(
                self.unique_id,
                Opcode::Send,
                element_type,
                self.rank,
                ANY_RANK,
                self.world_size,
                0,
                element_count,
            );
            header.destination_rank = self.rank;
            header.tag = tag;
            let frame = Frame::new(header, payload)?;
            let (inbox, ready) = &*self.inbox;
            inbox
                .lock()
                .map_err(|_| NetworkError::Poisoned)?
                .frames
                .push_back(QueuedFrame { rail, frame });
            ready.notify_all();
            return Ok(());
        }

        let connection = self.connection(rail, destination)?;
        let timeout = *self.timeout.lock().map_err(|_| NetworkError::Poisoned)?;
        let (sender, receiver) = mpsc::channel();
        let key;
        {
            let mut submission = connection
                .submission
                .lock()
                .map_err(|_| NetworkError::Poisoned)?;
            let sequence = submission.next_sequence;
            submission.next_sequence = sequence
                .checked_add(1)
                .ok_or(ProtocolError::SequenceOverflow)?;
            key = AckKey {
                peer: destination,
                rail,
                sequence,
            };
            let mut header = FrameHeader::collective(
                self.unique_id,
                Opcode::Send,
                element_type,
                self.rank,
                ANY_RANK,
                self.world_size,
                sequence,
                element_count,
            );
            header.destination_rank = destination;
            header.tag = tag;
            let frame = Frame::new(header, payload)?;
            self.pending_acks
                .lock()
                .map_err(|_| NetworkError::Poisoned)?
                .insert(key, sender);
            if let Err(error) = write_frame(&mut submission.stream, &frame) {
                set_peer_failure(
                    &self.failure,
                    &self.inbox,
                    &self.pending_acks,
                    &format!("direct peer send to rank {destination} rail {rail} failed: {error}"),
                );
                return Err(error);
            }
        }
        let deadline = Instant::now() + timeout;
        loop {
            if let Some(message) = self.failure_message()? {
                if let Ok(mut pending) = self.pending_acks.lock() {
                    pending.remove(&key);
                }
                return Err(NetworkError::RemoteAbort(message));
            }
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                if let Ok(mut pending) = self.pending_acks.lock() {
                    pending.remove(&key);
                }
                let message = format!(
                    "direct peer send to rank {destination} rail {rail} with tag {tag} timed out"
                );
                set_peer_failure(&self.failure, &self.inbox, &self.pending_acks, &message);
                return Err(NetworkError::Timeout(message));
            }
            match receiver.recv_timeout(remaining.min(FAILURE_POLL_INTERVAL)) {
                Ok(Ok(())) => return Ok(()),
                Ok(Err(message)) => return Err(NetworkError::RemoteAbort(message)),
                Err(RecvTimeoutError::Timeout) => {}
                Err(RecvTimeoutError::Disconnected) => {
                    return Err(NetworkError::ChannelClosed);
                }
            }
        }
    }

    pub(crate) fn receive_on_rail(
        &self,
        rail: usize,
        source: Option<u32>,
        tag: u64,
        element_type: ElementType,
        element_count: u64,
    ) -> Result<Frame, NetworkError> {
        self.validate_rail(rail)?;
        if let Some(source) = source {
            self.validate_peer(source)?;
        }
        let timeout = *self.timeout.lock().map_err(|_| NetworkError::Poisoned)?;
        let deadline = Instant::now() + timeout;
        let (inbox, ready) = &*self.inbox;
        let mut inbox = inbox.lock().map_err(|_| NetworkError::Poisoned)?;
        loop {
            if let Some(message) = self.failure_message()? {
                return Err(NetworkError::RemoteAbort(message));
            }
            if let Some(index) = inbox.frames.iter().position(|queued| {
                queued.rail == rail
                    && queued.frame.header.tag == tag
                    && source.is_none_or(|source| queued.frame.header.source_rank == source)
            }) {
                let queued = inbox
                    .frames
                    .remove(index)
                    .expect("matched peer frame exists");
                if queued.frame.header.element_type != element_type
                    || queued.frame.header.element_count != element_count
                {
                    return Err(NetworkError::InvalidConfiguration(format!(
                        "direct peer tag {tag} contract mismatch: send {:?}/{} receive {:?}/{}",
                        queued.frame.header.element_type,
                        queued.frame.header.element_count,
                        element_type,
                        element_count
                    )));
                }
                return Ok(queued.frame);
            }
            let now = Instant::now();
            if now >= deadline {
                drop(inbox);
                let message =
                    format!("direct peer receive on rail {rail} with tag {tag} timed out");
                set_peer_failure(&self.failure, &self.inbox, &self.pending_acks, &message);
                return Err(NetworkError::Timeout(message));
            }
            let wait = deadline
                .saturating_duration_since(now)
                .min(FAILURE_POLL_INTERVAL);
            let (next, _) = ready
                .wait_timeout(inbox, wait)
                .map_err(|_| NetworkError::Poisoned)?;
            inbox = next;
        }
    }

    pub(crate) fn abort(&self, message: &str) -> Result<(), NetworkError> {
        set_peer_failure(&self.failure, &self.inbox, &self.pending_acks, message);
        let mut first_error = None;
        for peers in &self.connections {
            for connection in peers.iter().flatten() {
                let payload = message.as_bytes().to_vec();
                let mut header = FrameHeader::collective(
                    self.unique_id,
                    Opcode::Abort,
                    ElementType::U8,
                    self.rank,
                    ANY_RANK,
                    self.world_size,
                    0,
                    payload.len() as u64,
                );
                header.destination_rank = connection.peer;
                let result = connection
                    .submission
                    .lock()
                    .map_err(|_| NetworkError::Poisoned)
                    .and_then(|mut submission| {
                        write_frame(&mut submission.stream, &Frame::new(header, payload)?)
                    });
                if let Err(error) = result
                    && first_error.is_none()
                {
                    first_error = Some(error);
                }
            }
        }
        match first_error {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    fn connection(&self, rail: usize, peer: u32) -> Result<&Arc<PeerConnection>, NetworkError> {
        self.connections[rail][peer as usize]
            .as_ref()
            .ok_or_else(|| {
                NetworkError::InvalidConfiguration(format!(
                    "direct peer connection to rank {peer} rail {rail} is absent"
                ))
            })
    }

    fn validate_peer(&self, peer: u32) -> Result<(), NetworkError> {
        if peer < self.world_size {
            Ok(())
        } else {
            Err(ProtocolError::RankOutOfRange {
                name: "peer",
                rank: peer,
                world_size: self.world_size,
            }
            .into())
        }
    }

    fn validate_rail(&self, rail: usize) -> Result<(), NetworkError> {
        if rail < self.rails {
            Ok(())
        } else {
            Err(NetworkError::InvalidConfiguration(format!(
                "direct peer rail {rail} is outside 0..{}",
                self.rails
            )))
        }
    }

    fn failure_message(&self) -> Result<Option<String>, NetworkError> {
        Ok(self
            .failure
            .lock()
            .map_err(|_| NetworkError::Poisoned)?
            .clone())
    }

    fn check_failure(&self) -> Result<(), NetworkError> {
        match self.failure_message()? {
            Some(message) => Err(NetworkError::RemoteAbort(message)),
            None => Ok(()),
        }
    }
}

impl Drop for DirectPeerMesh {
    fn drop(&mut self) {
        for peers in &self.connections {
            for connection in peers.iter().flatten() {
                if let Ok(mut submission) = connection.submission.lock() {
                    let mut header = FrameHeader::collective(
                        self.unique_id,
                        Opcode::Leave,
                        ElementType::None,
                        self.rank,
                        ANY_RANK,
                        self.world_size,
                        0,
                        0,
                    );
                    header.destination_rank = connection.peer;
                    if let Ok(frame) = Frame::new(header, Vec::new()) {
                        let _ = write_frame(&mut submission.stream, &frame);
                    }
                    let _ = submission.stream.shutdown(Shutdown::Both);
                }
            }
        }
        if let Ok(readers) = self.readers.get_mut() {
            for reader in readers.drain(..) {
                let _ = reader.join();
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn connect_peer(
    endpoint: SocketAddr,
    unique_id: UniqueId,
    rank: u32,
    peer: u32,
    world_size: u32,
    rail: usize,
    timeout: Duration,
) -> Result<TcpStream, NetworkError> {
    let mut stream = TcpStream::connect_timeout(&endpoint, timeout)?;
    stream.set_nodelay(true)?;
    stream.set_read_timeout(Some(timeout))?;
    stream.set_write_timeout(Some(timeout))?;
    let mut header = FrameHeader::collective(
        unique_id,
        Opcode::Join,
        ElementType::None,
        rank,
        ANY_RANK,
        world_size,
        0,
        0,
    );
    header.destination_rank = peer;
    header.flags |= FLAG_P2P_CHANNEL;
    header.tag = rail as u64;
    write_frame(&mut stream, &Frame::new(header, Vec::new())?)?;
    let ready = read_frame(&mut stream)?.ok_or_else(|| {
        NetworkError::InvalidConfiguration("direct peer closed before READY".into())
    })?;
    if ready.header.opcode != Opcode::Ready
        || ready.header.unique_id != unique_id
        || ready.header.source_rank != peer
        || ready.header.destination_rank != rank
        || ready.header.world_size != world_size
        || ready.header.flags & FLAG_P2P_CHANNEL == 0
        || ready.header.tag != rail as u64
        || !ready.payload.is_empty()
    {
        return Err(NetworkError::InvalidConfiguration(format!(
            "invalid READY from direct peer rank {peer} rail {rail}"
        )));
    }
    Ok(stream)
}

fn validate_peer_join(
    frame: &Frame,
    unique_id: UniqueId,
    rank: u32,
    world_size: u32,
    rails: usize,
) -> Result<(u32, usize), NetworkError> {
    if frame.header.opcode != Opcode::Join
        || frame.header.element_type != ElementType::None
        || frame.header.unique_id != unique_id
        || frame.header.destination_rank != rank
        || frame.header.world_size != world_size
        || frame.header.flags & FLAG_P2P_CHANNEL == 0
        || !frame.payload.is_empty()
    {
        return Err(NetworkError::InvalidConfiguration(
            "invalid direct peer JOIN".into(),
        ));
    }
    let rail = usize::try_from(frame.header.tag)
        .map_err(|_| NetworkError::InvalidConfiguration("direct peer rail exceeds usize".into()))?;
    if rail >= rails {
        return Err(NetworkError::InvalidConfiguration(format!(
            "direct peer rail {rail} is outside 0..{rails}"
        )));
    }
    Ok((frame.header.source_rank, rail))
}

fn peer_ready_frame(
    unique_id: UniqueId,
    rank: u32,
    peer: u32,
    world_size: u32,
    rail: usize,
) -> Result<Frame, NetworkError> {
    let mut header = FrameHeader::collective(
        unique_id,
        Opcode::Ready,
        ElementType::None,
        rank,
        ANY_RANK,
        world_size,
        0,
        0,
    );
    header.destination_rank = peer;
    header.flags |= FLAG_P2P_CHANNEL;
    header.tag = rail as u64;
    Ok(Frame::new(header, Vec::new())?)
}

#[allow(clippy::too_many_arguments)]
fn run_peer_reader(
    stream: &mut TcpStream,
    unique_id: UniqueId,
    rank: u32,
    world_size: u32,
    connection: &Arc<PeerConnection>,
    inbox: &Arc<(Mutex<PeerInbox>, Condvar)>,
    pending_acks: &PendingPeerAcks,
    failure: &Arc<Mutex<Option<String>>>,
) {
    loop {
        let frame = match read_frame(stream) {
            Ok(Some(frame)) => frame,
            Ok(None) => {
                set_peer_failure(
                    failure,
                    inbox,
                    pending_acks,
                    &format!(
                        "direct peer rank {} rail {} closed",
                        connection.peer, connection.rail
                    ),
                );
                return;
            }
            Err(error) => {
                set_peer_failure(
                    failure,
                    inbox,
                    pending_acks,
                    &format!(
                        "direct peer rank {} rail {} read failed: {error}",
                        connection.peer, connection.rail
                    ),
                );
                return;
            }
        };
        if frame.header.unique_id != unique_id
            || frame.header.world_size != world_size
            || frame.header.source_rank != connection.peer
            || frame.header.destination_rank != rank
            || frame.header.root_rank != ANY_RANK
        {
            set_peer_failure(
                failure,
                inbox,
                pending_acks,
                "invalid direct peer frame envelope",
            );
            return;
        }
        match frame.header.opcode {
            Opcode::Send if frame.header.element_type == ElementType::None => {
                if !frame.payload.is_empty() || frame.header.element_count != 0 {
                    set_peer_failure(
                        failure,
                        inbox,
                        pending_acks,
                        "invalid direct peer send acknowledgement",
                    );
                    return;
                }
                let key = AckKey {
                    peer: connection.peer,
                    rail: connection.rail,
                    sequence: frame.header.sequence,
                };
                if let Ok(mut pending) = pending_acks.lock()
                    && let Some(sender) = pending.remove(&key)
                {
                    let _ = sender.send(Ok(()));
                }
            }
            Opcode::Send => {
                let acknowledgement = match peer_send_acknowledgement(
                    unique_id,
                    rank,
                    connection.peer,
                    world_size,
                    &frame,
                ) {
                    Ok(frame) => frame,
                    Err(error) => {
                        set_peer_failure(failure, inbox, pending_acks, &error.to_string());
                        return;
                    }
                };
                let (queue, ready) = &**inbox;
                match queue.lock() {
                    Ok(mut queue) => queue.frames.push_back(QueuedFrame {
                        rail: connection.rail,
                        frame,
                    }),
                    Err(_) => return,
                }
                ready.notify_all();
                let write_result = connection
                    .submission
                    .lock()
                    .map_err(|_| NetworkError::Poisoned)
                    .and_then(|mut submission| {
                        write_frame(&mut submission.stream, &acknowledgement)
                    });
                if let Err(error) = write_result {
                    set_peer_failure(
                        failure,
                        inbox,
                        pending_acks,
                        &format!("direct peer acknowledgement failed: {error}"),
                    );
                    return;
                }
            }
            Opcode::Abort => {
                set_peer_failure(
                    failure,
                    inbox,
                    pending_acks,
                    &String::from_utf8_lossy(&frame.payload),
                );
                return;
            }
            Opcode::Leave => {
                set_peer_failure(
                    failure,
                    inbox,
                    pending_acks,
                    &format!("direct peer rank {} left", connection.peer),
                );
                return;
            }
            opcode => {
                set_peer_failure(
                    failure,
                    inbox,
                    pending_acks,
                    &format!("unexpected {opcode:?} on direct peer connection"),
                );
                return;
            }
        }
    }
}

fn peer_send_acknowledgement(
    unique_id: UniqueId,
    rank: u32,
    peer: u32,
    world_size: u32,
    send: &Frame,
) -> Result<Frame, NetworkError> {
    let mut header = FrameHeader::collective(
        unique_id,
        Opcode::Send,
        ElementType::None,
        rank,
        ANY_RANK,
        world_size,
        send.header.sequence,
        0,
    );
    header.destination_rank = peer;
    header.tag = send.header.tag;
    Ok(Frame::new(header, Vec::new())?)
}

fn set_peer_failure(
    failure: &Arc<Mutex<Option<String>>>,
    inbox: &Arc<(Mutex<PeerInbox>, Condvar)>,
    pending_acks: &PendingPeerAcks,
    message: &str,
) {
    if let Ok(mut failure) = failure.lock() {
        failure.get_or_insert_with(|| message.to_owned());
    }
    if let Ok(mut pending) = pending_acks.lock() {
        for (_, sender) in pending.drain() {
            let _ = sender.send(Err(message.to_owned()));
        }
    }
    inbox.1.notify_all();
}