acktor-ipc 1.1.1

Interprocess communication support for the acktor actor framework
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
//! Per-connection session actor.
//!
//! A [`Session`] wraps a single [`IpcConnection`] and mediates all traffic over it: routing
//! inbound frames to local actors, forwarding outbound messages from local actors, and tracking
//! pending request tags for response correlation. Sessions are owned by a
//! [`Node`][crate::node::Node] and are created through it rather than directly.
//!

use std::fmt::{self, Debug};
use std::result::Result as StdResult;
use std::sync::Arc;

use ahash::HashMap;
use bytes::Bytes;
use futures_util::{FutureExt, TryFutureExt};
use tokio::time::{Duration, Instant};
use tracing::{Instrument, debug, info, warn};

use acktor::{
    Actor, ActorContext, Address, ErrorReport, Handler, Message, Sender, SenderInfo,
    channel::oneshot,
    message::FutureMessageResult,
    utils::{ShortName, debug_trace},
};
use acktor_ipc_proto::{message, utils as proto_utils};

use crate::actor_ref::ActorRef;
use crate::codec::{Decode, DecodeContext, DecodeError, Encode, EncodeContext};
use crate::error::SessionError;
use crate::ipc_method::IpcConnection;
use crate::node::actor_mgr::{self, ActorMgr};
use crate::remote::{
    BinaryMessage, RemoteAddressable, RemoteMailbox, RemoteMailboxRegistry, RemoteSpawnable,
};

pub mod command;

mod context;
use context::SessionContext;

mod proxy;
use proxy::Proxy;

type Result<T> = StdResult<T, SessionError>;

/// How long a pending request may sit in the response maps before the cleanup sweep resolves
/// it with [`SessionError::ResponseTimeout`]. The observable timeout is up to
/// `RESPONSE_TIMEOUT + CLEANUP_INTERVAL` because the sweep is periodic.
pub(crate) const RESPONSE_TIMEOUT: Duration = Duration::from_secs(60);

/// How often the session runs the response-map cleanup sweep.
pub(crate) const CLEANUP_INTERVAL: Duration = Duration::from_secs(15);

struct MessageResponse {
    tag: u64,
    result: StdResult<Bytes, String>,
}

impl Debug for MessageResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("MessageResponse").field(&self.tag).finish()
    }
}

impl Message for MessageResponse {
    type Result = ();
}

/// An actor which manages the IPC connection to a remote endpoint.
pub struct Session {
    connection: Box<dyn IpcConnection>,
    registry: RemoteMailboxRegistry,
    actor_mgr: Address<ActorMgr>,
    tag: u64, // unique tag generator
    proxy: Option<Arc<Proxy>>,
    message_res_tx_map: HashMap<u64, (oneshot::Sender<Bytes>, Instant)>,
}

impl Session {
    /// Constructs a new [`Session`]. Called internally by [`Node`][crate::node::Node] when it
    /// accepts or initiates an IPC connection; not intended for direct use.
    pub(crate) fn new(
        connection: Box<dyn IpcConnection>,
        registry: RemoteMailboxRegistry,
        actor_mgr: Address<ActorMgr>,
    ) -> Self {
        Self {
            connection,
            registry,
            actor_mgr,
            tag: 0,
            proxy: None,
            message_res_tx_map: HashMap::default(),
        }
    }

    fn cleanup_message_res_tx_map(&mut self) {
        let now = Instant::now();

        let expired = self.message_res_tx_map.extract_if(|_, (tx, timestamp)| {
            tx.is_closed() || now.duration_since(*timestamp) >= RESPONSE_TIMEOUT
        });

        for (tag, (tx, _)) in expired {
            if tx.is_closed() {
                debug!(
                    "The sender of message with tag {} has closed the response rx, remove the \
                     corresponding response tx",
                    tag
                );
            } else {
                let _ = tx.send_err(SessionError::ResponseTimeout);
            }
        }
    }

    fn next_tag(&mut self) -> u64 {
        let tag = self.tag;
        self.tag = self.tag.wrapping_add(1);
        tag
    }

    async fn send_ipc_message(&mut self, ipc_msg: message::IpcMessage) -> Result<()> {
        let encoded_ipc_msg = ipc_msg.encode_to_bytes(None)?;
        self.connection
            .send(encoded_ipc_msg)
            .await
            .map_err(SessionError::SendOutboundMessageFailed)?;

        Ok(())
    }

    fn encode_context(&self) -> Arc<dyn EncodeContext + Send + Sync> {
        self.proxy
            .clone()
            .expect("proxy is always available after session is started")
            as Arc<dyn EncodeContext + Send + Sync>
    }

    fn decode_context(&self) -> Arc<dyn DecodeContext + Send + Sync> {
        self.proxy
            .clone()
            .expect("proxy is always available after session is started")
            as Arc<dyn DecodeContext + Send + Sync>
    }

    fn find_mailbox(&self, actor_id: u64) -> Result<RemoteMailbox> {
        self.registry
            .get(actor_id)
            .ok_or_else(|| SessionError::ActorNotFound(actor_id.to_string()))
    }

    async fn find_mailbox_by_label(&self, label: String) -> Result<RemoteMailbox> {
        self.actor_mgr
            .send(actor_mgr::GetActor { label })
            .await?
            .await?
    }

    async fn handle_node_message(
        &mut self,
        message: message::NodeMessage,
        ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        match message.message {
            Some(message::NodeMessageType::CreateActor(message::CreateActor {
                type_id,
                label,
                config,
                tag,
            })) => {
                let actor_mgr = self.actor_mgr.clone();
                let address = ctx.address();

                // spawn a task to handle the potentially time consuming actor creation process
                tokio::spawn(
                    async move {
                        actor_mgr
                            .send(actor_mgr::CreateActor {
                                type_id,
                                label,
                                config,
                            })
                            .await?
                            .await?
                    }
                    .then(move |result| async move {
                        let result = match result {
                            Ok(actor_id) => proto_utils::ResultAddress::ok(actor_id.as_local()),
                            Err(e) => proto_utils::ResultAddress::err(e.report()),
                        };

                        let bytes: Bytes = prost::Message::encode_to_vec(&result).into();

                        // send the result back to this actor with the MessageResponse message
                        // the IpcConnection can not be cloned into the spawned task without a
                        // Arc<Mutex<..>>, so we convert this into a sequential message handling
                        // process
                        address
                            .do_send(MessageResponse {
                                tag,
                                result: Ok(bytes),
                            })
                            .await
                    })
                    .inspect_err(|e| {
                        // we can not do much if sending the response back to this actor fails,
                        // just log it
                        warn!(
                            "Could not send `NodeMessageResponse::CreateActor` to remote node: {}",
                            e.report()
                        );
                    })
                    .in_current_span(),
                );

                Ok(())
            }

            Some(message::NodeMessageType::GetActor(message::GetActor { actor, tag })) => {
                let result = match actor {
                    Some(proto_utils::ActorRef { r#ref }) => match r#ref {
                        Some(proto_utils::ActorRefType::Index(actor_id)) => {
                            self.find_mailbox(actor_id)
                        }
                        Some(proto_utils::ActorRefType::Label(label)) => {
                            self.find_mailbox_by_label(label).await
                        }
                        None => Err(DecodeError::from("missing field `ref` in `ActorRef`").into()),
                    },
                    _ => Err(
                        DecodeError::from("missing field `actor` in `NodeMessage::GetActor`")
                            .into(),
                    ),
                };

                let result = match result {
                    Ok(mailbox) => proto_utils::ResultAddress::ok(mailbox.index().as_local()),
                    Err(e) => proto_utils::ResultAddress::err(e.report()),
                };

                let bytes: Bytes = prost::Message::encode_to_vec(&result).into();
                let ipc_msg = message::IpcMessage::message_response(message::MessageResponse::new(
                    tag,
                    Ok(bytes),
                ));

                self.send_ipc_message(ipc_msg).await.inspect_err(|e| {
                    // we can not do much if sending the response back to the remote node fails,
                    // just log it
                    warn!(
                        "Could not send `NodeMessageResponse::GetActor` to remote node: {}",
                        e.report()
                    );
                })
            }

            _ => Err(DecodeError::from("missing field `message` in `NodeMessage`").into()),
        }
    }

    async fn handle_actor_message(
        &mut self,
        message: message::ActorMessage,
        ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let message::ActorMessage {
            actor_id,
            message_id,
            message,
            tag,
        } = message;

        match tag {
            Some(tag) => {
                // send

                let address = ctx.address();
                let recipient = self.find_mailbox(actor_id);
                let (tx, rx) = oneshot::channel();
                let message = BinaryMessage::send(actor_id, message_id, message, tx)
                    .with_encode_context(self.encode_context())
                    .with_decode_context(self.decode_context());

                // spawn a task to handle the potentially time consuming message handling process
                tokio::spawn(
                    async move {
                        recipient?
                            .do_send(message)
                            .await
                            .map_err(|e| SessionError::ForwardInboundMessageFailed(e.into()))?;

                        let result = rx
                            .await
                            .map_err(|e| SessionError::HandleInboundMessageFailed(e.into()))?;

                        Ok::<Bytes, SessionError>(result)
                    }
                    .then(move |result| async move {
                        // send the result back to this actor with the MessageResponse message
                        // the IpcConnection can not be cloned into the spawned task without a
                        // Arc<Mutex<..>>, so we convert this into a sequential message handling
                        // process
                        address
                            .do_send(MessageResponse {
                                tag,
                                result: result.map_err(|e| e.report()),
                            })
                            .await
                    })
                    .inspect_err(|e| {
                        // we can not do much if sending the response back to this actor fails,
                        // just log it
                        warn!(
                            "Could not send `ActorMessageResponse` to remote node: {}",
                            e.report()
                        );
                    })
                    .in_current_span(),
                );

                Ok(())
            }

            None => {
                // do_send

                // sender has explicitly indicated that it does not care about the result of this
                // message, so we just return the error and the session's context will log it
                self.find_mailbox(actor_id)?
                    .do_send(
                        BinaryMessage::do_send(actor_id, message_id, message)
                            .with_decode_context(self.decode_context()),
                    )
                    .await
                    .map_err(|e| SessionError::ForwardInboundMessageFailed(e.into()))
            }
        }
    }

    fn handle_message_response(
        &mut self,
        response: message::MessageResponse,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let tag = response.tag;

        let (sender, _) = self
            .message_res_tx_map
            .remove(&tag)
            // if the tag is not found in the map, we do not know who to send the result
            // to, and we do not know who to report the error to either, so just return
            // an error and the session's context will log it
            .ok_or(SessionError::InvalidMessageResTxTag(tag))?;

        // remote error and processing error should be reported to the original sender who is
        // waiting for a `Result<M::Result, RecvError>`
        let result: Result<_> = match response.response {
            Some(message::ResponseType::Ok(bytes)) => Ok(bytes),
            Some(message::ResponseType::Err(err)) => Err(SessionError::RemoteNodeError(err)),
            None => Err(DecodeError::from("missing field `response` in `MessageResponse`").into()),
        };

        // we can not do much if sending the result back to the original sender fails, just return
        // an error and the session's context will log it
        match result {
            Ok(bytes) => sender
                .send(bytes)
                .map_err(|_| SessionError::ForwardMessageResFailed),
            Err(e) => sender
                .send_err(e)
                .map_err(|_| SessionError::ForwardMessageResFailed),
        }
    }

    async fn handle_ipc_message(
        &mut self,
        message: Bytes,
        ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let ipc_message = message::IpcMessage::decode(message, None)?;

        match ipc_message.message {
            Some(message::IpcMessageType::NodeMessage(message)) => {
                self.handle_node_message(message, ctx).await
            }
            Some(message::IpcMessageType::ActorMessage(message)) => {
                self.handle_actor_message(message, ctx).await
            }
            Some(message::IpcMessageType::MessageResponse(response)) => {
                self.handle_message_response(response, ctx)
            }
            _ => Err(DecodeError::from("missing field `message` in `IpcMessage`").into()),
        }
    }
}

impl Actor for Session {
    type Context = SessionContext;
    type Error = SessionError;

    async fn post_start(&mut self, ctx: &mut Self::Context) -> Result<()> {
        info!("Session {} is started", self.connection.peer_endpoint());

        self.proxy = Some(Proxy::new(ctx.address(), self.registry.clone()));

        Ok(())
    }

    async fn post_stop(&mut self, _ctx: &mut Self::Context) -> Result<()> {
        self.connection
            .close()
            .await
            .map_err(SessionError::IoError)?;

        info!("Session {} is stopped", self.connection.peer_endpoint());

        Ok(())
    }
}

impl<A> Handler<command::RemoteCreateActor<A>> for Session
where
    A: Actor + RemoteSpawnable,
{
    type Result = FutureMessageResult<command::RemoteCreateActor<A>>;

    async fn handle(
        &mut self,
        msg: command::RemoteCreateActor<A>,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command RemoteCreateActor<{}>", ShortName::of::<A>());

        let command::RemoteCreateActor { label, config, .. } = msg;

        let (tx, rx) = oneshot::channel();

        let tag = self.next_tag();
        let ipc_msg = message::IpcMessage::node_message(message::NodeMessage::create_actor(
            A::TYPE_ID.as_u64(),
            label,
            config.unwrap_or_default(),
            tag,
        ));

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            warn!(
                "Could not send `NodeMessage::CreateActor` to remote node: {}",
                e.report()
            );
            // sends the error back to the original sender who is waiting for a
            // `Result<Result<Address<A>, SessionError>, RecvError>`
            if let Err(e) = tx.send_err(e) {
                // we can not do much if sending the error back to the original sender fails, just
                // log it
                warn!(
                    "Could not report the error in `Handler<RemoteCreateActor<{}>>` to the \
                     original sender: {}",
                    ShortName::of::<A>(),
                    e.report()
                );
            }
        } else {
            self.message_res_tx_map.insert(tag, (tx, Instant::now()));
        }

        let decode_context = self.decode_context();

        FutureMessageResult::new(async move {
            let bytes = rx.await?;
            let result = <proto_utils::ResultAddress as prost::Message>::decode(bytes)
                .map_err(DecodeError::other)?;
            match result.result {
                Some(proto_utils::ResultAddressType::Ok(actor_id)) => {
                    Address::new_with_decode_context(actor_id, decode_context.as_ref())
                        .map_err(Into::into)
                }
                Some(proto_utils::ResultAddressType::Err(e)) => {
                    Err(SessionError::RemoteNodeError(e))
                }
                _ => Err(DecodeError::other("missing field `result` in `ResultAddress`").into()),
            }
        })
    }
}

impl<A> Handler<command::RemoteGetActor<A>> for Session
where
    A: Actor + RemoteAddressable,
{
    type Result = FutureMessageResult<command::RemoteGetActor<A>>;

    async fn handle(
        &mut self,
        msg: command::RemoteGetActor<A>,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command RemoteGetActor<{}>", ShortName::of::<A>());

        let command::RemoteGetActor { actor, .. } = msg;

        let (tx, rx) = oneshot::channel();

        let tag = self.next_tag();
        let ipc_msg = match &actor {
            ActorRef::Index(actor_id) => message::IpcMessage::node_message(
                message::NodeMessage::get_actor_by_index(actor_id.as_local(), tag),
            ),
            ActorRef::Label(label) => message::IpcMessage::node_message(
                message::NodeMessage::get_actor_by_label(label.clone(), tag),
            ),
        };

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            warn!(
                "Could not send `NodeMessage::GetActor` to remote node: {}",
                e.report()
            );
            // sends the error back to the original sender who is waiting for a
            // `Result<Result<Address<A>, SessionError>, RecvError>`
            if let Err(e) = tx.send_err(e) {
                // we can not do much if sending the error back to the original sender fails, just
                // log it
                warn!(
                    "Could not report the error in `Handler<GetRemoteActor>` to the original \
                     sender: {}",
                    e.report()
                );
            }
        } else {
            self.message_res_tx_map.insert(tag, (tx, Instant::now()));
        }

        let decode_context = self.decode_context();

        FutureMessageResult::new(async move {
            let bytes = rx.await?;
            let result = <proto_utils::ResultAddress as prost::Message>::decode(bytes)
                .map_err(DecodeError::other)?;
            match result.result {
                Some(proto_utils::ResultAddressType::Ok(actor_id)) => {
                    Address::new_with_decode_context(actor_id, decode_context.as_ref())
                        .map_err(Into::into)
                }
                Some(proto_utils::ResultAddressType::Err(e)) => {
                    Err(SessionError::RemoteNodeError(e))
                }
                _ => Err(DecodeError::other("missing field `result` in `ResultAddress`").into()),
            }
        })
    }
}

impl Handler<BinaryMessage> for Session {
    type Result = ();

    async fn handle(
        &mut self,
        msg: BinaryMessage,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let BinaryMessage {
            actor_id,
            message_id,
            bytes: message,
            result_tx,
            ..
        } = msg;

        match result_tx {
            Some(tx) => {
                // send

                let tag = self.next_tag();
                let ipc_msg = message::IpcMessage::actor_message(message::ActorMessage::send(
                    actor_id, message_id, message, tag,
                ));

                if let Err(e) = self.send_ipc_message(ipc_msg).await {
                    warn!(
                        "Could not send `ActorMessage` to remote node: {}",
                        e.report()
                    );
                    // sends the error back to the original sender who is waiting for a
                    // `Result<M::Result, RecvError>`
                    if let Err(e) = tx.send_err(e) {
                        // we can not do much if sending the error back to the original sender
                        // fails, just log it
                        warn!(
                            "Could not report the error in `Handler<BinaryMessage>` to the \
                             original sender: {}",
                            e.report()
                        );
                    }

                    return;
                }

                self.message_res_tx_map.insert(tag, (tx, Instant::now()));
            }

            None => {
                // do_send

                let ipc_msg = message::IpcMessage::actor_message(message::ActorMessage::do_send(
                    actor_id, message_id, message,
                ));

                if let Err(e) = self.send_ipc_message(ipc_msg).await {
                    // sender has explicitly indicated that it does not care about the result of
                    // this message, so we just log the error
                    warn!(
                        "Could not do_send `ActorMessage` to remote node: {}",
                        e.report()
                    );
                }
            }
        }
    }
}

impl Handler<MessageResponse> for Session {
    type Result = ();

    async fn handle(
        &mut self,
        msg: MessageResponse,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let MessageResponse { tag, result } = msg;

        let ipc_msg =
            message::IpcMessage::message_response(message::MessageResponse::new(tag, result));

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            warn!(
                "Could not send `MessageResponse` to remote node: {}",
                e.report()
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use acktor::RecvError;
    use anyhow::Result;
    use pretty_assertions::assert_eq;
    use tracing_test::traced_test;

    use super::*;
    use crate::actor_ref::ActorRef;
    use crate::test_utils::{Echo, start_failing_session};

    #[test]
    fn test_debug_fmt() {
        let ok = MessageResponse {
            tag: 42,
            result: Ok(Bytes::from_static(b"payload")),
        };
        assert_eq!(format!("{:?}", ok), "MessageResponse(42)");

        let err = MessageResponse {
            tag: 7,
            result: Err("boom".to_string()),
        };
        assert_eq!(format!("{:?}", err), "MessageResponse(7)");
    }

    /// When the outbound IPC send fails for a `send` (response-expected) message, the handler must
    /// log the failure and report the error back to the original sender instead of leaving it
    /// hanging.
    #[tokio::test]
    #[traced_test]
    async fn test_binary_message_send_reports_failure() -> Result<()> {
        let session = start_failing_session();

        let (tx, rx) = oneshot::channel::<Bytes>();

        session
            .do_send(BinaryMessage::send(1, 2, Bytes::new(), tx))
            .await?;

        let result = rx.await;
        assert!(
            matches!(result, Err(RecvError::Other(e)) if e.to_string() == "could not send the outbound remote message to the remote node")
        );

        assert!(logs_contain("Could not send `ActorMessage` to remote node"));

        Ok(())
    }

    /// When the outbound IPC send fails for a `do_send` (fire-and-forget) message, the error is
    /// only logged and the session must keep running. We prove it survived by following up with a
    /// `send` message and still receiving the reported failure.
    #[tokio::test]
    #[traced_test]
    async fn test_binary_message_do_send_survives_failure() -> Result<()> {
        let session = start_failing_session();

        // do_send variant: no result channel; failure is swallowed (logged) by the handler.
        session
            .do_send(BinaryMessage::do_send(1, 2, Bytes::new()))
            .await?;

        // The session is still alive and processes the next message.
        let (tx, rx) = oneshot::channel::<Bytes>();
        session
            .do_send(BinaryMessage::send(3, 4, Bytes::new(), tx))
            .await?;

        assert!(matches!(rx.await, Err(RecvError::Other(_))));
        assert!(logs_contain(
            "Could not do_send `ActorMessage` to remote node"
        ));

        Ok(())
    }

    /// A failed outbound send while serving a `MessageResponse` (the relayed result of an inbound
    /// request) is only logged; the session must keep running.
    #[tokio::test]
    #[traced_test]
    async fn test_message_response_survives_failure() -> Result<()> {
        let session = start_failing_session();

        session
            .do_send(MessageResponse {
                tag: 1,
                result: Ok(Bytes::from_static(b"payload")),
            })
            .await?;

        // Still responsive afterwards.
        let (tx, rx) = oneshot::channel::<Bytes>();
        session
            .do_send(BinaryMessage::send(2, 3, Bytes::new(), tx))
            .await?;

        assert!(matches!(rx.await, Err(RecvError::Other(_))));
        assert!(logs_contain(
            "Could not send `MessageResponse` to remote node"
        ));

        Ok(())
    }

    /// `RemoteCreateActor` must surface a failed outbound send to the caller as an error rather
    /// than hanging on the response, and log the failure.
    #[tokio::test]
    #[traced_test]
    async fn test_remote_create_actor_reports_failure() -> Result<()> {
        let session = start_failing_session();

        let result = session
            .send(command::RemoteCreateActor::<Echo>::new("remote-echo", None))
            .await?
            .await?;

        assert!(result.is_err());
        assert!(logs_contain(
            "Could not send `NodeMessage::CreateActor` to remote node"
        ));

        Ok(())
    }

    /// `RemoteGetActor` must likewise surface a failed outbound send to the caller as an error,
    /// and log the failure.
    #[tokio::test]
    #[traced_test]
    async fn test_remote_get_actor_reports_failure() -> Result<()> {
        let session = start_failing_session();

        let result = session
            .send(command::RemoteGetActor::<Echo>::new(ActorRef::Label(
                "remote-echo".to_string(),
            )))
            .await?
            .await?;

        assert!(result.is_err());
        assert!(logs_contain(
            "Could not send `NodeMessage::GetActor` to remote node"
        ));

        Ok(())
    }
}