#[cfg(all(target_os = "linux", feature = "handover"))]
use std::collections::VecDeque;
#[cfg(all(target_os = "linux", feature = "handover"))]
use std::mem;
#[cfg(unix)]
use std::os::fd::{AsFd, BorrowedFd};
use std::{
fmt,
future::poll_fn,
io::{self, IoSlice},
slice,
sync::{Arc, atomic::Ordering},
task::{Context, Poll},
};
#[cfg(all(target_os = "linux", feature = "handover"))]
use std::{future::Future, pin::Pin, sync::atomic::AtomicBool};
#[cfg(all(target_os = "linux", feature = "handover"))]
use zruntime::{Event, EventListener};
#[cfg(all(target_os = "linux", feature = "handover"))]
use super::handover::SocketKind;
use super::socket_reader::SocketStatus;
#[cfg(all(target_os = "linux", feature = "handover"))]
use crate::Error;
#[cfg(all(target_os = "linux", feature = "handover"))]
use crate::runtime::IoSource;
#[cfg(all(target_os = "linux", feature = "handover"))]
use crate::runtime::io::MESSAGES_PER_WRITE;
use crate::{
Message, Result,
connection::socket::{WriteHalf, Writes},
fdo::ConnectionCredentials,
log::trace,
runtime::io::{PollSend, Progress, poll_write, write_message, write_messages},
wire::serialized::Data,
};
#[derive(Debug)]
pub(super) struct SocketWrite {
half: Option<Box<dyn WriteHalf>>,
writes: Option<Writes>,
half_resumes: bool,
rest: Rest,
#[cfg(all(target_os = "linux", feature = "handover"))]
source: Option<(IoSource, SocketKind)>,
#[cfg(all(target_os = "linux", feature = "handover"))]
torn: bool,
shut_down: bool,
}
impl SocketWrite {
pub(super) fn new(
half: Box<dyn WriteHalf>,
writes: Option<Writes>,
status: Arc<SocketStatus>,
#[cfg(all(target_os = "linux", feature = "handover"))] source: Option<(
IoSource,
SocketKind,
)>,
#[cfg(all(target_os = "linux", feature = "handover"))] unwritten: Unwritten,
) -> Self {
Self {
half_resumes: half.can_resume_writes(),
half: Some(half),
writes,
rest: Rest::new(
status,
#[cfg(all(target_os = "linux", feature = "handover"))]
unwritten,
),
#[cfg(all(target_os = "linux", feature = "handover"))]
source,
#[cfg(all(target_os = "linux", feature = "handover"))]
torn: false,
shut_down: false,
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
pub(super) fn can_prepare_hand_over(&self) -> bool {
self.writes.is_some() || self.half_resumes
}
pub(super) async fn send_message(&mut self, msg: &Message) -> Result<()> {
let msgs = slice::from_ref(msg);
match &self.writes {
Some(Writes::Driven(socket)) => {
let mut writing = Writing::sending(&mut self.rest);
if !writing.rest.is_empty() && !write_unwritten_to(&**socket, &mut writing).await? {
writing.rest.keep(msgs);
return Ok(());
}
let left = write_message(&**socket, msg, &mut writing).await?;
writing.rest.keep(left);
return Ok(());
}
None if self.half_resumes => {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
send_through(half, msgs, &mut self.rest).await?;
return Ok(());
}
#[cfg(all(target_os = "linux", feature = "handover", feature = "p2p"))]
Some(Writes::Whole) => {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
return queue(half, msgs, &mut self.rest).await;
}
_ => (),
}
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
#[cfg(all(target_os = "linux", feature = "handover"))]
let torn = match self.writes {
#[cfg(feature = "p2p")]
Some(Writes::Whole) => self.torn,
_ => mem::replace(&mut self.torn, true),
};
let sent = half.send_message(msg).await;
#[cfg(all(target_os = "linux", feature = "handover"))]
self.through(torn, &sent);
sent
}
pub(super) async fn send_messages(&mut self, msgs: &[Message]) -> Result<()> {
match &self.writes {
Some(Writes::Driven(socket)) => {
let mut writing = Writing::sending(&mut self.rest);
if !writing.rest.is_empty() && !write_unwritten_to(&**socket, &mut writing).await? {
writing.rest.keep(msgs);
return Ok(());
}
let left = write_messages(&**socket, msgs, &mut writing).await?;
writing.rest.keep(left);
return Ok(());
}
None if self.half_resumes => {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
send_through(half, msgs, &mut self.rest).await?;
return Ok(());
}
#[cfg(all(target_os = "linux", feature = "handover", feature = "p2p"))]
Some(Writes::Whole) => {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
return queue(half, msgs, &mut self.rest).await;
}
_ => (),
}
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
#[cfg(all(target_os = "linux", feature = "handover"))]
let torn = match self.writes {
#[cfg(feature = "p2p")]
Some(Writes::Whole) => self.torn,
_ => mem::replace(&mut self.torn, true),
};
let sent = half.send_messages(msgs).await;
#[cfg(all(target_os = "linux", feature = "handover"))]
self.through(torn, &sent);
sent
}
pub(super) async fn flush(&mut self) -> Result<()> {
if self.shut_down {
return Err(closed().into());
}
self.write_unwritten().await?;
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
half.flush().await?;
Ok(())
}
pub(super) async fn peer_credentials(&mut self) -> io::Result<ConnectionCredentials> {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
half.peer_credentials().await
}
pub(super) async fn close(&mut self) -> io::Result<()> {
let Some(half) = self.half.as_deref_mut() else {
return Ok(());
};
self.shut_down = true;
self.rest.clear();
self.writes = None;
half.close().await
}
#[cfg(all(target_os = "linux", feature = "handover"))]
pub(super) fn is_torn(&self) -> bool {
self.torn
}
#[cfg(all(target_os = "linux", feature = "handover"))]
pub(super) fn is_shut_down(&self) -> bool {
self.shut_down
}
#[cfg(all(target_os = "linux", feature = "handover"))]
pub(super) fn source(&self) -> Option<&(IoSource, SocketKind)> {
self.source.as_ref()
}
#[cfg(all(target_os = "linux", feature = "handover"))]
pub(super) fn take_half(&mut self) -> Option<(Box<dyn WriteHalf>, Option<Writes>, Unwritten)> {
self.source = None;
let half = self.half.take()?;
Some((half, self.writes.take(), self.rest.take()))
}
async fn write_unwritten(&mut self) -> Result<()> {
let mut writing = Writing::waiting(&mut self.rest);
match &self.writes {
Some(Writes::Driven(socket)) => {
write_unwritten_to(&**socket, &mut writing).await?;
}
None if self.half_resumes => {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
write_unwritten_through(half, &mut writing).await?;
}
#[cfg(all(target_os = "linux", feature = "handover", feature = "p2p"))]
Some(Writes::Whole) => {
let half = self.half.as_deref_mut().ok_or_else(handed_over)?;
queue_kept(half, &mut writing).await?;
}
_ => (),
}
Ok(())
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn through(&mut self, torn: bool, sent: &Result<()>) {
if sent
.as_ref()
.is_err_and(|e| !matches!(e, Error::InputOutput(e) if receives_nothing_more(e)))
{
return;
}
self.torn = torn;
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[derive(Debug, Default)]
pub(super) struct Prepared {
is_set: AtomicBool,
event: Event,
}
#[cfg(all(target_os = "linux", feature = "handover"))]
impl Prepared {
pub(super) fn set(&self) {
if !self.is_set.swap(true, Ordering::AcqRel) {
self.event.notify(usize::MAX);
}
}
fn is_set(&self) -> bool {
self.is_set.load(Ordering::Acquire)
}
fn listen(&self) -> EventListener {
self.event.listen()
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[derive(Default)]
pub(crate) struct Unwritten {
pub(crate) cut: Option<(Data<'static, 'static>, usize)>,
pub(crate) kept: VecDeque<Message>,
}
#[cfg(all(target_os = "linux", feature = "handover"))]
impl Unwritten {
pub(crate) fn rest(&self) -> &[u8] {
self.cut
.as_ref()
.map_or(&[], |(bytes, written)| &bytes[*written..])
}
pub(crate) fn len(&self) -> usize {
let kept = self.kept.iter().map(|msg| msg.data().len()).sum::<usize>();
self.rest().len() + kept
}
}
async fn write_unwritten_to(socket: &dyn PollSend, writing: &mut Writing<'_>) -> io::Result<bool> {
if !write_rest_to(socket, writing).await? {
return Ok(false);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
while !writing.rest.kept.is_empty() {
let copies = writing.rest.kept_copies();
let mut taking = TakingKept::new(writing);
let left = write_messages(socket, &copies, &mut taking).await?;
if !left.is_empty() || writing.rest.cut.is_some() {
return Ok(false);
}
}
Ok(true)
}
async fn write_rest_to(socket: &dyn PollSend, writing: &mut Writing<'_>) -> io::Result<bool> {
while let Some((bytes, written)) = writing.rest.cut.clone() {
let left = [IoSlice::new(&bytes[written..])];
let polled = poll_fn(|cx| {
poll_write(
socket,
cx,
&left,
#[cfg(unix)]
&[],
writing,
)
});
let sent = match polled.await {
Ok(sent) => sent,
Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(false),
Err(e) => return Err(e),
};
writing.rest.advance(sent);
}
Ok(true)
}
async fn send_through(
half: &mut dyn WriteHalf,
msgs: &[Message],
rest: &mut Rest,
) -> io::Result<()> {
let mut writing = Writing::sending(rest);
if !writing.rest.is_empty() && !write_unwritten_through(half, &mut writing).await? {
writing.rest.keep(msgs);
} else {
let left = write_all_through(half, msgs, &mut writing).await?;
writing.rest.keep(left);
}
flush_through(half, &mut writing).await
}
async fn write_unwritten_through(
half: &mut dyn WriteHalf,
writing: &mut Writing<'_>,
) -> io::Result<bool> {
if !write_rest_through(half, writing).await? {
return Ok(false);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
while !writing.rest.kept.is_empty() {
let copies = writing.rest.kept_copies();
let mut taking = TakingKept::new(writing);
let left = write_all_through(half, &copies, &mut taking).await?;
if !left.is_empty() || writing.rest.cut.is_some() {
return Ok(false);
}
}
Ok(true)
}
async fn write_rest_through(
half: &mut dyn WriteHalf,
writing: &mut Writing<'_>,
) -> io::Result<bool> {
while let Some((bytes, written)) = writing.rest.cut.clone() {
let sent = sendmsg(
half,
&bytes[written..],
#[cfg(unix)]
&[],
writing,
)
.await?;
let Some(sent) = sent else {
return Ok(false);
};
writing.rest.advance(sent);
}
Ok(true)
}
async fn write_all_through<'m, P>(
half: &mut dyn WriteHalf,
msgs: &'m [Message],
progress: &mut P,
) -> io::Result<&'m [Message]>
where
P: Progress,
{
for (done, msg) in msgs.iter().enumerate() {
trace!("Sending message: {:?}", msg);
let data = msg.data();
let mut written = 0;
while written < data.len() {
#[cfg(unix)]
let fds: Vec<_> = if written == 0 {
data.fds().iter().map(|fd| fd.as_fd()).collect()
} else {
vec![]
};
let sent = sendmsg(
half,
&data[written..],
#[cfg(unix)]
&fds,
progress,
)
.await?;
let Some(sent) = sent else {
return Ok(&msgs[done + usize::from(written > 0)..]);
};
written += sent;
let cut = (written < data.len()).then_some((msg, written));
progress.wrote(done + usize::from(cut.is_none()), cut);
}
trace!(
"Sent message with serial: {}",
msg.primary_header().serial_num()
);
}
Ok(&[])
}
async fn sendmsg<P>(
half: &mut dyn WriteHalf,
buffer: &[u8],
#[cfg(unix)] fds: &[BorrowedFd<'_>],
progress: &mut P,
) -> io::Result<Option<usize>>
where
P: Progress,
{
let mut sending = half.sendmsg(
buffer,
#[cfg(unix)]
fds,
);
let sent = poll_fn(|cx| match sending.as_mut().poll(cx) {
Poll::Pending if progress.stops(cx) => Poll::Ready(Ok(None)),
polled => polled.map_ok(Some),
})
.await?;
match sent {
Some(0) => Err(io::Error::new(
io::ErrorKind::WriteZero,
"the write half took none of what it was given",
)),
sent => Ok(sent),
}
}
async fn flush_through<P>(half: &mut dyn WriteHalf, progress: &mut P) -> io::Result<()>
where
P: Progress,
{
let mut flushing = half.flush();
poll_fn(|cx| match flushing.as_mut().poll(cx) {
Poll::Pending if progress.stops(cx) => Poll::Ready(Ok(())),
polled => polled,
})
.await
}
#[cfg(all(target_os = "linux", feature = "handover", feature = "p2p"))]
async fn queue(half: &mut dyn WriteHalf, msgs: &[Message], rest: &mut Rest) -> Result<()> {
let mut writing = Writing::sending(rest);
if !writing.rest.is_empty() && !queue_kept(half, &mut writing).await? {
writing.rest.keep(msgs);
return Ok(());
}
let left = queue_all(half, msgs, &mut writing).await?;
writing.rest.keep(left);
Ok(())
}
#[cfg(all(target_os = "linux", feature = "handover", feature = "p2p"))]
async fn queue_kept(half: &mut dyn WriteHalf, writing: &mut Writing<'_>) -> Result<bool> {
while !writing.rest.kept.is_empty() {
let copies = writing.rest.kept_copies();
let mut taking = TakingKept::new(writing);
if !queue_all(half, &copies, &mut taking).await?.is_empty() {
return Ok(false);
}
}
Ok(true)
}
#[cfg(all(target_os = "linux", feature = "handover", feature = "p2p"))]
async fn queue_all<'m, P>(
half: &mut dyn WriteHalf,
msgs: &'m [Message],
progress: &mut P,
) -> Result<&'m [Message]>
where
P: Progress,
{
for (done, msg) in msgs.iter().enumerate() {
let mut queueing = half.send_message(msg);
let queued = poll_fn(|cx| match queueing.as_mut().poll(cx) {
Poll::Pending if progress.stops(cx) => Poll::Ready(None),
polled => polled.map(Some),
})
.await;
let Some(queued) = queued else {
return Ok(&msgs[done..]);
};
queued?;
progress.wrote(done + 1, None);
}
Ok(&[])
}
struct Writing<'r> {
rest: &'r mut Rest,
#[cfg(all(target_os = "linux", feature = "handover"))]
waits: bool,
}
impl<'r> Writing<'r> {
fn sending(rest: &'r mut Rest) -> Self {
Self {
rest,
#[cfg(all(target_os = "linux", feature = "handover"))]
waits: false,
}
}
fn waiting(rest: &'r mut Rest) -> Self {
Self {
rest,
#[cfg(all(target_os = "linux", feature = "handover"))]
waits: true,
}
}
}
impl Progress for Writing<'_> {
#[inline]
fn wrote(&mut self, _done: usize, cut: Option<(&Message, usize)>) {
self.rest.set(cut);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn stops(&mut self, cx: &mut Context<'_>) -> bool {
!self.waits && self.rest.prepared(cx)
}
#[cfg(not(all(target_os = "linux", feature = "handover")))]
fn stops(&mut self, _cx: &mut Context<'_>) -> bool {
false
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
struct TakingKept<'w, 'r> {
writing: &'w mut Writing<'r>,
taken: usize,
}
#[cfg(all(target_os = "linux", feature = "handover"))]
impl<'w, 'r> TakingKept<'w, 'r> {
fn new(writing: &'w mut Writing<'r>) -> Self {
Self { writing, taken: 0 }
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
impl Progress for TakingKept<'_, '_> {
fn wrote(&mut self, done: usize, cut: Option<(&Message, usize)>) {
let started = done + usize::from(cut.is_some());
self.writing.rest.took(started - self.taken, cut);
self.taken = started;
}
fn stops(&mut self, cx: &mut Context<'_>) -> bool {
self.writing.stops(cx)
}
}
struct Rest {
cut: Option<(Data<'static, 'static>, usize)>,
#[cfg(all(target_os = "linux", feature = "handover"))]
kept: VecDeque<Message>,
#[cfg(all(target_os = "linux", feature = "handover"))]
kept_len: usize,
#[cfg(all(target_os = "linux", feature = "handover"))]
listener: Option<EventListener>,
status: Arc<SocketStatus>,
}
impl Rest {
fn new(
status: Arc<SocketStatus>,
#[cfg(all(target_os = "linux", feature = "handover"))] unwritten: Unwritten,
) -> Self {
#[cfg(all(target_os = "linux", feature = "handover"))]
let Unwritten { cut, kept } = unwritten;
#[cfg(not(all(target_os = "linux", feature = "handover")))]
let cut = None;
let rest = Self {
cut,
#[cfg(all(target_os = "linux", feature = "handover"))]
kept_len: kept.iter().map(|msg| msg.data().len()).sum(),
#[cfg(all(target_os = "linux", feature = "handover"))]
kept,
#[cfg(all(target_os = "linux", feature = "handover"))]
listener: None,
status,
};
rest.store();
rest
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn is_empty(&self) -> bool {
self.cut.is_none() && self.kept.is_empty()
}
#[cfg(not(all(target_os = "linux", feature = "handover")))]
fn is_empty(&self) -> bool {
self.cut.is_none()
}
#[inline]
fn set(&mut self, cut: Option<(&Message, usize)>) {
let Some((msg, written)) = cut else {
if self.cut.is_some() {
self.cut = None;
self.store();
}
return;
};
self.cut = Some((msg.data().clone(), written));
self.store();
}
fn advance(&mut self, len: usize) {
let Some((bytes, written)) = &mut self.cut else {
return;
};
*written += len;
if *written == bytes.len() {
trace!("Sent the rest of a message");
self.cut = None;
}
self.store();
}
#[inline]
fn keep(&mut self, msgs: &[Message]) {
#[cfg(all(target_os = "linux", feature = "handover"))]
if !msgs.is_empty() {
self.keep_all(msgs);
}
#[cfg(not(all(target_os = "linux", feature = "handover")))]
debug_assert!(msgs.is_empty(), "a send left messages without a hand-over");
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[cold]
fn keep_all(&mut self, msgs: &[Message]) {
self.kept_len += msgs.iter().map(|msg| msg.data().len()).sum::<usize>();
self.kept.extend(msgs.iter().cloned());
self.store();
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn kept_copies(&self) -> Vec<Message> {
self.kept.iter().take(MESSAGES_PER_WRITE).cloned().collect()
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn took(&mut self, taken: usize, cut: Option<(&Message, usize)>) {
for msg in self.kept.drain(..taken) {
self.kept_len -= msg.data().len();
}
self.cut = cut.map(|(msg, written)| (msg.data().clone(), written));
self.store();
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn take(&mut self) -> Unwritten {
let unwritten = Unwritten {
cut: self.cut.take(),
kept: mem::take(&mut self.kept),
};
self.kept_len = 0;
self.store();
unwritten
}
fn clear(&mut self) {
self.cut = None;
#[cfg(all(target_os = "linux", feature = "handover"))]
{
self.kept.clear();
self.kept_len = 0;
}
self.store();
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn prepared(&mut self, cx: &mut Context<'_>) -> bool {
let prepared = &self.status.prepared;
let listener = self.listener.get_or_insert_with(|| prepared.listen());
prepared.is_set() || Pin::new(listener).poll(cx).is_ready()
}
#[inline]
fn store(&self) {
let left = self
.cut
.as_ref()
.map_or(0, |(bytes, written)| bytes.len() - written);
#[cfg(all(target_os = "linux", feature = "handover"))]
let left = left + self.kept_len;
self.status.unwritten.store(left, Ordering::Relaxed);
}
}
impl fmt::Debug for Rest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut rest = f.debug_struct("Rest");
let cut = self
.cut
.as_ref()
.map(|(bytes, written)| bytes.len() - written);
rest.field("cut", &cut);
#[cfg(all(target_os = "linux", feature = "handover"))]
rest.field("kept", &self.kept.len())
.field("kept_len", &self.kept_len);
rest.finish_non_exhaustive()
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn receives_nothing_more(error: &io::Error) -> bool {
matches!(
error.kind(),
io::ErrorKind::BrokenPipe
| io::ErrorKind::ConnectionReset
| io::ErrorKind::ConnectionAborted
)
}
fn handed_over() -> io::Error {
io::Error::new(
io::ErrorKind::NotConnected,
"the connection was handed over",
)
}
fn closed() -> io::Error {
io::Error::new(io::ErrorKind::NotConnected, "the connection was closed")
}
#[cfg(all(test, unix, feature = "p2p"))]
mod tests {
#[cfg(feature = "unixexec")]
use std::path::Path;
use std::{
future::Future,
io::{self, ErrorKind, IoSlice, Read, Write},
os::{
fd::{AsFd, BorrowedFd, OwnedFd},
unix::net::UnixStream,
},
pin::{Pin, pin},
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
thread::{self, JoinHandle},
time::Duration,
};
#[cfg(all(target_os = "linux", feature = "handover"))]
use std::{
collections::VecDeque,
sync::{Mutex, atomic::AtomicBool},
task::{Context, Poll, Wake, Waker},
};
use futures_lite::future::poll_once;
use ntest::timeout;
use test_log::test;
#[cfg(all(target_os = "linux", feature = "handover"))]
use zoter::Save;
#[cfg(all(target_os = "linux", feature = "handover"))]
use zruntime::Event;
#[cfg(all(target_os = "linux", feature = "handover"))]
use super::SocketWrite;
#[cfg(feature = "unixexec")]
use crate::{
Address,
address::transport::{Transport, Unixexec},
};
use crate::{
Connection, Error, Fd, Guid, Message, Result,
connection::{
Builder,
socket::{Channel, ReadHalf, RecvmsgResult, Socket, Split, WriteHalf},
},
runtime::{
IoSource, Runtime,
io::{Reader, RegisteredIo, SocketOps, UnixOps, registered, tests::Recording},
test_runtime::under_every_runtime,
},
};
#[cfg(all(target_os = "linux", feature = "handover"))]
use crate::{
MessageStream,
connection::{handover::StopStatus, socket::Writes, socket_reader::SocketStatus},
runtime::io::PollSend,
};
#[test]
#[timeout(15000)]
fn a_send_dropped_midway_is_finished_by_the_next_send() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
finish_a_send_dropped_midway(&runtime, &conn, &peer).await;
});
}
#[test]
#[timeout(15000)]
fn flush_writes_what_a_dropped_send_left() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let big = big_signal();
stuck(&runtime, pin!(conn.send(&big)), &peer).await;
let reading = read_in_the_background(&peer, big.data().len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), &big.data()[..]);
assert_eq!(conn.unwritten_len(), 0);
assert_nothing_to_read(&peer);
conn.flush().await.unwrap();
});
}
#[test]
#[timeout(15000)]
fn a_message_none_of_which_was_written_is_not_sent() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (big, dropped, next) = (big_signal(), signal(1), signal(2));
stuck(&runtime, pin!(conn.send(&big)), &peer).await;
let sent = poll_once(pin!(conn.send(&dropped))).await;
assert!(
sent.is_none(),
"the socket took the rest after all: {sent:?}"
);
let reading = read_in_the_background(&peer, big.data().len() + next.data().len());
conn.send(&next).await.unwrap();
let received = reading.join().unwrap();
assert_eq!(received, [&big.data()[..], &next.data()[..]].concat());
assert_nothing_to_read(&peer);
});
}
#[test]
#[timeout(15000)]
fn a_send_all_dropped_midway_sends_none_of_the_messages_after_its_cut() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (before, big, after, next) = (signal(1), big_signal(), signal(2), signal(3));
let msgs = [before.clone(), big.clone(), after];
stuck(&runtime, pin!(conn.send_all(&msgs)), &peer).await;
let unwritten = conn.unwritten_len();
assert!(
0 < unwritten && unwritten < big.data().len(),
"{unwritten} of {} bytes left to write",
big.data().len(),
);
let len = before.data().len() + big.data().len() + next.data().len();
let reading = read_in_the_background(&peer, len);
conn.send(&next).await.unwrap();
let received = reading.join().unwrap();
let sent = [&before.data()[..], &big.data()[..], &next.data()[..]].concat();
assert_eq!(received, sent);
assert_nothing_to_read(&peer);
});
}
#[test]
#[timeout(15000)]
fn a_send_that_fails_midway_leaves_the_rest_of_its_message() {
under_every_runtime(|runtime| async move {
let (conn, peer) = over(&runtime, FailingMidway::default()).await;
let (msg, next) = (signal(1), signal(2));
let failed = conn.send(&msg).await;
let Err(Error::InputOutput(e)) = &failed else {
panic!("the send didn't fail on the write: {failed:?}");
};
assert_eq!(e.kind(), ErrorKind::Other, "{e}");
assert_eq!(conn.unwritten_len(), msg.data().len() - CUT);
conn.send(&next).await.unwrap();
let mut received = vec![0; msg.data().len() + next.data().len()];
(&peer).read_exact(&mut received).unwrap();
assert_eq!(received, [&msg.data()[..], &next.data()[..]].concat());
assert_eq!(conn.unwritten_len(), 0);
});
}
#[test]
#[timeout(15000)]
fn closing_a_connection_lets_go_of_what_it_has_not_written() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let big = big_signal();
stuck(&runtime, pin!(conn.send(&big)), &peer).await;
conn.clone().close().await.unwrap();
assert_eq!(conn.unwritten_len(), 0);
let flushed = conn.flush().await;
let Err(Error::InputOutput(e)) = &flushed else {
panic!("the flush of a closed connection didn't fail: {flushed:?}");
};
assert_eq!(e.kind(), ErrorKind::NotConnected, "{e}");
let sent = conn.send(&signal(1)).await;
assert!(sent.is_err(), "a send on a closed connection went through");
let mut received = vec![];
(&peer).read_to_end(&mut received).unwrap();
assert!(
received.len() < big.data().len(),
"the peer got the whole message"
);
});
}
#[test]
#[timeout(15000)]
fn the_rest_of_a_message_goes_without_its_descriptors() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (read_end, _write_end) = io::pipe().unwrap();
let big = Message::signal(PATH, INTERFACE, "Big")
.build(&(Fd::from(read_end.as_fd()), vec![0u8; 1024 * 1024]))
.unwrap();
let next = signal(1);
stuck(&runtime, pin!(conn.send(&big)), &peer).await;
let mut peer = Reader::new(Arc::new(registered(&runtime, peer, UnixOps).unwrap()));
let receiving = async {
let (mut bytes, mut fds) = (vec![], vec![]);
let first = peer.receive_message(0, &mut bytes, &mut fds).await.unwrap();
let second = peer.receive_message(1, &mut bytes, &mut fds).await.unwrap();
(first, second)
};
let (sent, (first, second)) = futures_util::join!(conn.send(&next), receiving);
sent.unwrap();
assert_eq!(&first.data()[..], &big.data()[..]);
assert_eq!(first.data().fds().len(), 1);
assert_eq!(&second.data()[..], &next.data()[..]);
assert_eq!(second.data().fds().len(), 0);
});
}
#[test]
#[timeout(15000)]
fn a_send_dropped_on_a_full_channel_sends_nothing() {
under_every_runtime(|runtime| async move {
let (ours, theirs) = Channel::pair();
let conn = on(
Builder::authenticated_socket(ours, Guid::generate()),
&runtime,
)
.p2p()
.build()
.await
.unwrap();
let (mut peer, _sends_nothing) = Socket::split(theirs).take();
let mut queued = vec![];
loop {
let msg = signal(queued.len() as u32);
match poll_once(pin!(conn.send(&msg))).await {
Some(sent) => sent.unwrap(),
None => break,
}
queued.push(msg);
}
assert_eq!(conn.unwritten_len(), 0);
let (mut bytes, mut fds) = (vec![], vec![]);
let mut received = vec![peer.receive_message(0, &mut bytes, &mut fds).await.unwrap()];
let next = signal(u32::MAX);
conn.send(&next).await.unwrap();
while received.len() < queued.len() + 1 {
let seq = received.len() as u64;
let msg = peer.receive_message(seq, &mut bytes, &mut fds).await;
received.push(msg.unwrap());
}
let received: Vec<_> = received.iter().map(|msg| msg.data().to_vec()).collect();
let sent: Vec<_> = queued
.iter()
.chain([&next])
.map(|msg| msg.data().to_vec())
.collect();
assert_eq!(received, sent);
#[cfg(all(target_os = "linux", feature = "handover"))]
{
let handed_over = MessageStream::from(&conn).hand_over().await;
assert!(handed_over.is_ok(), "{handed_over:?}");
}
});
}
#[test]
#[timeout(15000)]
fn a_half_that_can_resume_writes_is_written_through_sendmsg() {
under_every_runtime(|runtime| async move {
let (conn, peer, counts) = over_a_half_of_your_own(&runtime, Some(CAP), None).await;
let pipes = [pipe(), pipe(), pipe()];
let fd = |i: usize| Fd::from(pipes[i].as_fd());
let msgs = [
signal(1),
Message::signal(PATH, INTERFACE, "WithFds")
.build(&fd(0))
.unwrap(),
signal(2),
Message::signal(PATH, INTERFACE, "WithFds")
.build(&(fd(1), fd(2)))
.unwrap(),
];
conn.send_all(&msgs[..3]).await.unwrap();
assert_eq!(counts.flushes.load(Ordering::Relaxed), 1);
conn.send(&msgs[3]).await.unwrap();
assert_eq!(counts.flushes.load(Ordering::Relaxed), 2);
conn.flush().await.unwrap();
assert_eq!(counts.flushes.load(Ordering::Relaxed), 3);
let mut peer = Reader::new(Arc::new(registered(&runtime, peer, UnixOps).unwrap()));
let (mut bytes, mut fds) = (vec![], vec![]);
for (seq, sent) in msgs.iter().enumerate() {
let received = peer.receive_message(seq as u64, &mut bytes, &mut fds).await;
let received = received.unwrap();
assert_eq!(&received.data()[..], &sent.data()[..]);
assert_eq!(received.data().fds().len(), sent.data().fds().len());
}
});
}
#[test]
#[timeout(15000)]
fn a_send_through_a_half_that_can_resume_writes_dropped_midway_is_finished() {
under_every_runtime(|runtime| async move {
let (conn, peer, _) = over_a_half_of_your_own(&runtime, Some(BIG_CAP), None).await;
finish_a_send_dropped_midway(&runtime, &conn, &peer).await;
});
}
#[test]
#[timeout(15000)]
fn a_send_through_a_half_that_can_resume_writes_that_fails_midway_leaves_the_rest() {
under_every_runtime(|runtime| async move {
let (conn, peer, _) = over_a_half_of_your_own(&runtime, Some(CAP), Some(2)).await;
let (msg, next) = (signal(1), signal(2));
let failed = conn.send(&msg).await;
let Err(Error::InputOutput(e)) = &failed else {
panic!("the send didn't fail on the write: {failed:?}");
};
assert_eq!(e.kind(), ErrorKind::Other, "{e}");
assert_eq!(conn.unwritten_len(), msg.data().len() - CAP);
conn.send(&next).await.unwrap();
let mut received = vec![0; msg.data().len() + next.data().len()];
(&peer).read_exact(&mut received).unwrap();
assert_eq!(received, [&msg.data()[..], &next.data()[..]].concat());
assert_eq!(conn.unwritten_len(), 0);
});
}
#[test]
#[timeout(15000)]
fn a_half_that_cant_resume_writes_is_given_whole_messages() {
under_every_runtime(|runtime| async move {
let (conn, peer, counts) = over_a_half_of_your_own(&runtime, None, None).await;
let msgs = [signal(1), signal(2), signal(3)];
conn.send(&msgs[0]).await.unwrap();
conn.send_all(&msgs[1..]).await.unwrap();
assert_eq!(counts.whole.load(Ordering::Relaxed), 2);
let mut received = vec![0; msgs.iter().map(|msg| msg.data().len()).sum()];
(&peer).read_exact(&mut received).unwrap();
let sent: Vec<u8> = msgs.iter().flat_map(|msg| msg.data().to_vec()).collect();
assert_eq!(received, sent);
stuck(&runtime, pin!(conn.send(&big_signal())), &peer).await;
assert_eq!(conn.unwritten_len(), 0);
let flushed = poll_once(pin!(conn.flush())).await;
assert!(matches!(flushed, Some(Ok(()))), "{flushed:?}");
assert_eq!(counts.flushes.load(Ordering::Relaxed), 1);
});
}
#[test]
#[timeout(15000)]
fn the_halves_of_a_socket_that_zbus_drives_resume_writes_as_halves_of_your_own() {
under_every_runtime(|runtime| async move {
let (ours, peer) = UnixStream::pair().unwrap();
let socket = registered(&runtime, ours, UnixOps).unwrap();
let (read, write) = Socket::split(socket).take();
let split = Split::new(
Box::new(read) as Box<dyn ReadHalf>,
Box::new(write) as Box<dyn WriteHalf>,
);
let conn = on(
Builder::authenticated_socket(split, Guid::generate()),
&runtime,
)
.p2p()
.build()
.await
.unwrap();
finish_a_send_dropped_midway(&runtime, &conn, &peer).await;
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn the_rest_of_a_send_through_a_half_that_can_resume_writes_is_handed_over() {
under_every_runtime(|runtime| async move {
let (conn, peer, _) = over_a_half_of_your_own(&runtime, Some(BIG_CAP), None).await;
let big = big_signal();
stuck(&runtime, pin!(conn.send(&big)), &peer).await;
let unwritten = conn.unwritten_len();
let handover = MessageStream::from(&conn).hand_over().await.unwrap();
assert_eq!(handover.unwritten_len(), unwritten);
assert_eq!(handover.saved_len(), None);
assert_eq!(conn.unwritten_len(), 0);
let conn = on(Builder::take_over(handover), &runtime)
.build()
.await
.unwrap();
assert_eq!(conn.unwritten_len(), unwritten);
let reading = read_in_the_background(&peer, big.data().len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), &big.data()[..]);
assert_eq!(conn.unwritten_len(), 0);
});
}
#[test]
#[timeout(15000)]
fn messages_go_in_writes_of_several() {
under_every_runtime(|runtime| async move {
let ops = Recording::default();
let writes = ops.writes.clone();
let (conn, theirs) = over(&runtime, ops).await;
let mut peer = Reader::new(Arc::new(registered(&runtime, theirs, UnixOps).unwrap()));
let (read_end, _write_end) = io::pipe().unwrap();
let msgs: Vec<_> = (0..22u32)
.map(|i| {
let builder = Message::method_call("/", "Test");
match i {
18 | 20 => builder.build(&(i, Fd::from(read_end.as_fd()))),
_ => builder.build(&i),
}
.unwrap()
})
.collect();
conn.send_all(&msgs[..21]).await.unwrap();
conn.send(&msgs[21]).await.unwrap();
assert_eq!(
*writes.lock().unwrap(),
[(16, 0), (2, 0), (1, 1), (1, 0), (1, 1), (1, 0)],
"the writes were not split as expected, as (messages, descriptors)",
);
for (seq, sent) in msgs.iter().enumerate() {
let received = peer
.receive_message(seq as u64, &mut vec![], &mut vec![])
.await
.unwrap();
assert_eq!(&received.data()[..], &sent.data()[..]);
assert_eq!(received.data().fds().len(), sent.data().fds().len());
}
});
}
#[cfg(feature = "unixexec")]
#[test]
#[timeout(15000)]
fn closing_a_unixexec_connection_ends_the_input_of_the_helper() {
under_every_runtime(|runtime| async move {
let dir = tempfile::tempdir().unwrap();
let ended = dir.path().join("ended");
let script = "read -r line; printf 'OK 0123456789abcdef0123456789abcdef\\r\\n'; \
cat >/dev/null; : >\"$0\"";
let helper = Unixexec::new(
"sh".into(),
None,
vec!["-c".into(), script.into(), ended.clone().into()],
);
let address = Address::from(Transport::Unixexec(helper));
let conn = on(Builder::address(address).p2p(), &runtime)
.build()
.await
.unwrap();
conn.send(&signal(1)).await.unwrap();
conn.clone().close().await.unwrap();
wait_for(&ended);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn sends_dont_wait_once_a_hand_over_is_prepared() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (before, big, after, next) = (signal(1), big_signal(), signal(2), signal(3));
assert!(conn.prepare_hand_over());
assert!(conn.prepare_hand_over(), "the second call refused");
let msgs = [before.clone(), big.clone(), after.clone()];
without_waiting(&runtime, conn.send_all(&msgs)).await;
let left = conn.unwritten_len();
let cut = left - after.data().len();
assert!(
0 < cut && cut < big.data().len(),
"{cut} of {} bytes left to write",
big.data().len(),
);
without_waiting(&runtime, conn.send(&next)).await;
assert_eq!(conn.unwritten_len(), left + next.data().len());
let sent = [&before, &big, &after, &next]
.iter()
.flat_map(|msg| msg.data().to_vec())
.collect::<Vec<_>>();
let reading = read_in_the_background(&peer, sent.len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), sent);
assert_eq!(conn.unwritten_len(), 0);
assert_nothing_to_read(&peer);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_send_waiting_when_a_hand_over_is_prepared_returns() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (before, big, after) = (signal(1), big_signal(), signal(2));
let msgs = [before.clone(), big.clone(), after.clone()];
let mut sending = pin!(conn.send_all(&msgs));
stuck(&runtime, sending.as_mut(), &peer).await;
let woken = Arc::new(Woken::default());
let waker = Waker::from(woken.clone());
let mut cx = Context::from_waker(&waker);
assert!(sending.as_mut().poll(&mut cx).is_pending());
assert!(!woken.0.load(Ordering::Relaxed));
assert!(conn.prepare_hand_over());
assert!(woken.0.load(Ordering::Relaxed), "the send wasn't woken");
let sent = sending.as_mut().poll(&mut cx);
assert!(matches!(sent, Poll::Ready(Ok(()))), "{sent:?}");
let cut = conn.unwritten_len() - after.data().len();
assert!(
0 < cut && cut < big.data().len(),
"{cut} of {} bytes left to write",
big.data().len(),
);
let sent: Vec<u8> = msgs.iter().flat_map(|msg| msg.data().to_vec()).collect();
let reading = read_in_the_background(&peer, sent.len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), sent);
assert_eq!(conn.unwritten_len(), 0);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_prepared_send_writes_what_the_socket_takes_at_once() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let msgs = [signal(1), signal(2), signal(3)];
assert!(conn.prepare_hand_over());
without_waiting(&runtime, conn.send(&msgs[0])).await;
without_waiting(&runtime, conn.send_all(&msgs[1..])).await;
assert_eq!(conn.unwritten_len(), 0);
let mut received = vec![0; msgs.iter().map(|msg| msg.data().len()).sum()];
(&peer).read_exact(&mut received).unwrap();
let sent: Vec<u8> = msgs.iter().flat_map(|msg| msg.data().to_vec()).collect();
assert_eq!(received, sent);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_prepared_send_that_fails_keeps_nothing() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
assert!(conn.prepare_hand_over());
drop(peer);
let sent = conn.send_all(&[signal(1), signal(2)]).await;
let Err(Error::InputOutput(e)) = &sent else {
panic!("the send to a peer that went didn't fail: {sent:?}");
};
assert_eq!(e.kind(), ErrorKind::BrokenPipe, "{e}");
assert_eq!(conn.unwritten_len(), 0);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_kept_message_goes_with_its_descriptors() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (read_end, _write_end) = io::pipe().unwrap();
let big = big_signal();
let with_fd = Message::signal(PATH, INTERFACE, "WithFd")
.build(&Fd::from(read_end.as_fd()))
.unwrap();
assert!(conn.prepare_hand_over());
without_waiting(&runtime, conn.send(&big)).await;
without_waiting(&runtime, conn.send(&with_fd)).await;
assert!(conn.unwritten_len() > with_fd.data().len());
let mut peer = Reader::new(Arc::new(registered(&runtime, peer, UnixOps).unwrap()));
let receiving = async {
let (mut bytes, mut fds) = (vec![], vec![]);
let first = peer.receive_message(0, &mut bytes, &mut fds).await.unwrap();
let second = peer.receive_message(1, &mut bytes, &mut fds).await.unwrap();
(first, second)
};
let (flushed, (first, second)) = futures_util::join!(conn.flush(), receiving);
flushed.unwrap();
assert_eq!(&first.data()[..], &big.data()[..]);
assert_eq!(first.data().fds().len(), 0);
assert_eq!(&second.data()[..], &with_fd.data()[..]);
assert_eq!(second.data().fds().len(), 1);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn kept_messages_go_in_writes_of_several() {
under_every_runtime(|runtime| async move {
let ops = Recording::default();
let writes = ops.writes.clone();
let (conn, peer) = over(&runtime, ops).await;
let big = big_signal();
let msgs: Vec<_> = (0..20).map(signal).collect();
assert!(conn.prepare_hand_over());
without_waiting(&runtime, conn.send(&big)).await;
without_waiting(&runtime, conn.send_all(&msgs)).await;
writes.lock().unwrap().clear();
let sent: Vec<u8> = [&big]
.into_iter()
.chain(&msgs)
.flat_map(|msg| msg.data().to_vec())
.collect();
let reading = read_in_the_background(&peer, sent.len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), sent);
let writes = writes.lock().unwrap();
assert!(writes.contains(&(16, 0)), "{writes:?}");
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_send_keeps_its_messages_until_what_was_kept_is_written() {
let (kept, msg) = (signal(1), signal(2));
let half = kept.data().len() / 2;
let socket = Arc::new(Scripted::new([None, Some(half), None]));
let status = prepared_status();
let mut write = SocketWrite::new(
Box::new(Unused),
Some(Writes::Driven(socket.clone())),
status.clone(),
None,
Default::default(),
);
keep_until_what_was_kept_is_written(&mut write, &socket, &status, &kept, &msg);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_send_through_a_half_keeps_its_messages_until_what_was_kept_is_written() {
let (kept, msg) = (signal(1), signal(2));
let half = kept.data().len() / 2;
let socket = Arc::new(Scripted::new([None, Some(half), None]));
let status = prepared_status();
let mut write = SocketWrite::new(
Box::new(ScriptedHalf(socket.clone())),
None,
status.clone(),
None,
Default::default(),
);
keep_until_what_was_kept_is_written(&mut write, &socket, &status, &kept, &msg);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn a_connection_over_a_half_that_cant_resume_writes_isnt_prepared() {
under_every_runtime(|runtime| async move {
let (conn, peer, _) = over_a_half_of_your_own(&runtime, None, None).await;
let big = big_signal();
let mut sending = pin!(conn.send(&big));
stuck(&runtime, sending.as_mut(), &peer).await;
assert!(!conn.prepare_hand_over());
let sent = poll_once(sending.as_mut()).await;
assert!(sent.is_none(), "the send stopped waiting: {sent:?}");
assert_eq!(conn.unwritten_len(), 0);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn sends_through_a_half_that_can_resume_writes_dont_wait_once_prepared() {
under_every_runtime(|runtime| async move {
let (conn, peer, counts) = over_a_half_of_your_own(&runtime, Some(BIG_CAP), None).await;
let (big, next) = (big_signal(), signal(1));
assert!(conn.prepare_hand_over());
without_waiting(&runtime, conn.send(&big)).await;
without_waiting(&runtime, conn.send(&next)).await;
let cut = conn.unwritten_len() - next.data().len();
assert!(
0 < cut && cut <= big.data().len(),
"{cut} of {} bytes left to write",
big.data().len(),
);
let flushes = counts.flushes.load(Ordering::Relaxed);
let sent = [&big.data()[..], &next.data()[..]].concat();
let reading = read_in_the_background(&peer, sent.len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), sent);
assert_eq!(conn.unwritten_len(), 0);
assert_eq!(counts.flushes.load(Ordering::Relaxed), flushes + 1);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn sends_to_a_full_channel_dont_wait_once_prepared() {
under_every_runtime(|runtime| async move {
let (ours, theirs) = Channel::pair();
let conn = on(
Builder::authenticated_socket(ours, Guid::generate()),
&runtime,
)
.p2p()
.build()
.await
.unwrap();
let (mut peer, _sends_nothing) = Socket::split(theirs).take();
let msgs: Vec<_> = (0..64).map(signal).collect();
let mut queued = 0;
let mut sending = loop {
let mut sending = Box::pin(conn.send(&msgs[queued]));
match poll_once(sending.as_mut()).await {
Some(sent) => sent.unwrap(),
None => break sending,
}
queued += 1;
};
let woken = Arc::new(Woken::default());
let waker = Waker::from(woken.clone());
let mut cx = Context::from_waker(&waker);
assert!(sending.as_mut().poll(&mut cx).is_pending());
assert!(!woken.0.load(Ordering::Relaxed));
assert!(conn.prepare_hand_over());
assert!(woken.0.load(Ordering::Relaxed), "the send wasn't woken");
let sent = sending.as_mut().poll(&mut cx);
assert!(matches!(sent, Poll::Ready(Ok(()))), "{sent:?}");
drop(sending);
assert_eq!(conn.unwritten_len(), msgs[queued].data().len());
without_waiting(&runtime, conn.send_all(&msgs[queued + 1..queued + 3])).await;
let kept: usize = msgs[queued..queued + 3]
.iter()
.map(|msg| msg.data().len())
.sum();
assert_eq!(conn.unwritten_len(), kept);
let receiving = async {
let (mut bytes, mut fds) = (vec![], vec![]);
let mut received = vec![];
for seq in 0..queued + 3 {
let msg = peer.receive_message(seq as u64, &mut bytes, &mut fds).await;
received.push(msg.unwrap().data().to_vec());
}
received
};
let (flushed, received) = futures_util::join!(conn.flush(), receiving);
flushed.unwrap();
let sent: Vec<_> = msgs[..queued + 3]
.iter()
.map(|msg| msg.data().to_vec())
.collect();
assert_eq!(received, sent);
assert_eq!(conn.unwritten_len(), 0);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn what_sends_kept_is_handed_over() {
under_every_runtime(|runtime| async move {
let (conn, peer) = served(&runtime).await;
let (big, next) = (big_signal(), signal(1));
assert!(conn.prepare_hand_over());
without_waiting(&runtime, conn.send(&big)).await;
without_waiting(&runtime, conn.send(&next)).await;
let unwritten = conn.unwritten_len();
let handover = MessageStream::from(&conn).hand_over().await.unwrap();
assert_eq!(handover.unwritten_len(), unwritten);
assert_eq!(conn.unwritten_len(), 0);
let conn = on(Builder::take_over(handover), &runtime)
.build()
.await
.unwrap();
let sent = [&big.data()[..], &next.data()[..]].concat();
let reading = read_in_the_background(&peer, sent.len());
conn.flush().await.unwrap();
assert_eq!(reading.join().unwrap(), sent);
assert_eq!(conn.unwritten_len(), 0);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn what_sends_to_a_full_channel_kept_is_handed_over() {
under_every_runtime(|runtime| async move {
let (ours, theirs) = Channel::pair();
let conn = on(
Builder::authenticated_socket(ours, Guid::generate()),
&runtime,
)
.p2p()
.build()
.await
.unwrap();
let (mut peer, _sends_nothing) = Socket::split(theirs).take();
let msgs: Vec<_> = (0..64).map(signal).collect();
let mut queued = 0;
let sending = loop {
let mut sending = Box::pin(conn.send(&msgs[queued]));
match poll_once(sending.as_mut()).await {
Some(sent) => sent.unwrap(),
None => break sending,
}
queued += 1;
};
let mut stream = MessageStream::from(&conn);
let (handed_over, sent) = futures_util::join!(stream.hand_over(), sending);
sent.unwrap();
let handover = handed_over.unwrap();
assert_eq!(handover.unwritten_len(), msgs[queued].data().len());
assert_eq!(handover.saved_len(), None);
let conn = on(Builder::take_over(handover), &runtime)
.build()
.await
.unwrap();
let next = &msgs[queued + 1];
let receiving = async {
let (mut bytes, mut fds) = (vec![], vec![]);
let mut received = vec![];
for seq in 0..queued + 2 {
let msg = peer.receive_message(seq as u64, &mut bytes, &mut fds).await;
received.push(msg.unwrap().data().to_vec());
}
received
};
let (sent, received) = futures_util::join!(conn.send(next), receiving);
sent.unwrap();
let sent: Vec<_> = msgs[..queued + 2]
.iter()
.map(|msg| msg.data().to_vec())
.collect();
assert_eq!(received, sent);
assert_eq!(conn.unwritten_len(), 0);
});
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[test]
#[timeout(15000)]
fn closing_a_prepared_connection_lets_go_of_what_sends_kept() {
under_every_runtime(|runtime| async move {
let (conn, _peer) = served(&runtime).await;
assert!(conn.prepare_hand_over());
without_waiting(&runtime, conn.send(&big_signal())).await;
without_waiting(&runtime, conn.send(&signal(1))).await;
assert!(conn.unwritten_len() > 0);
conn.clone().close().await.unwrap();
assert_eq!(conn.unwritten_len(), 0);
let flushed = conn.flush().await;
let Err(Error::InputOutput(e)) = &flushed else {
panic!("the flush of a closed connection didn't fail: {flushed:?}");
};
assert_eq!(e.kind(), ErrorKind::NotConnected, "{e}");
});
}
async fn served(runtime: &Runtime) -> (Connection, UnixStream) {
let (client, server) = UnixStream::pair().unwrap();
let id = hex::encode(rustix::process::geteuid().as_raw().to_string());
let commands = format!("\0AUTH EXTERNAL {id}\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n");
(&client).write_all(commands.as_bytes()).unwrap();
let guid = Guid::generate();
let conn = on(Builder::unix_stream(server).server(&guid).p2p(), runtime)
.build()
.await
.unwrap();
let answer = format!("OK {guid}\r\nAGREE_UNIX_FD\r\n");
let mut answered = vec![0; answer.len()];
(&client).read_exact(&mut answered).unwrap();
assert_eq!(answered, answer.as_bytes());
(conn, client)
}
async fn over<O>(runtime: &Runtime, ops: O) -> (Connection, UnixStream)
where
O: SocketOps,
{
let (ours, theirs) = UnixStream::pair().unwrap();
let socket = registered(runtime, ours, ops).unwrap();
let conn = on(
Builder::authenticated_socket(socket, Guid::generate()),
runtime,
)
.p2p()
.build()
.await
.unwrap();
(conn, theirs)
}
#[derive(Debug, Default)]
struct FailingMidway {
sends: AtomicUsize,
}
impl SocketOps for FailingMidway {
fn recv(&self, source: &IoSource, buffer: &mut [u8]) -> RecvmsgResult {
UnixOps.recv(source, buffer)
}
fn send(
&self,
source: &IoSource,
buffers: &[IoSlice<'_>],
fds: &[BorrowedFd<'_>],
) -> io::Result<usize> {
match self.sends.fetch_add(1, Ordering::Relaxed) {
0 => UnixOps.send(source, &[IoSlice::new(&buffers[0][..CUT])], fds),
1 => Err(io::Error::other("the test failed this send")),
_ => UnixOps.send(source, buffers, fds),
}
}
fn can_pass_unix_fd(&self) -> bool {
true
}
}
async fn over_a_half_of_your_own(
runtime: &Runtime,
cap: Option<usize>,
fail_at: Option<usize>,
) -> (Connection, UnixStream, Arc<Counts>) {
let (ours, theirs) = UnixStream::pair().unwrap();
let socket = Arc::new(registered(runtime, ours, UnixOps).unwrap());
let counts = Arc::new(Counts::default());
let half = OfYourOwn {
socket: socket.clone(),
cap,
calls: 0,
fail_at,
counts: counts.clone(),
};
let split = Split::new(
Box::new(Reader::new(socket)) as Box<dyn ReadHalf>,
Box::new(half) as Box<dyn WriteHalf>,
);
let conn = on(
Builder::authenticated_socket(split, Guid::generate()),
runtime,
)
.p2p()
.build()
.await
.unwrap();
(conn, theirs, counts)
}
#[derive(Debug)]
struct OfYourOwn {
socket: Arc<RegisteredIo<UnixOps>>,
cap: Option<usize>,
calls: usize,
fail_at: Option<usize>,
counts: Arc<Counts>,
}
#[derive(Debug, Default)]
struct Counts {
whole: AtomicUsize,
flushes: AtomicUsize,
}
#[async_trait::async_trait]
impl WriteHalf for OfYourOwn {
async fn send_message(&mut self, msg: &Message) -> Result<()> {
assert!(
self.cap.is_none(),
"a half that can resume writes was handed {msg:?}"
);
self.counts.whole.fetch_add(1, Ordering::Relaxed);
WriteHalf::send_message(&mut self.socket, msg).await
}
async fn send_messages(&mut self, msgs: &[Message]) -> Result<()> {
assert!(
self.cap.is_none(),
"a half that can resume writes was handed {msgs:?}"
);
self.counts.whole.fetch_add(1, Ordering::Relaxed);
WriteHalf::send_messages(&mut self.socket, msgs).await
}
async fn sendmsg(&mut self, buffer: &[u8], fds: &[BorrowedFd<'_>]) -> io::Result<usize> {
self.calls += 1;
if self.fail_at == Some(self.calls) {
return Err(io::Error::other("the test failed this write"));
}
let len = buffer.len().min(self.cap.unwrap_or(usize::MAX));
WriteHalf::sendmsg(&mut self.socket, &buffer[..len], fds).await
}
async fn flush(&mut self) -> io::Result<()> {
self.counts.flushes.fetch_add(1, Ordering::Relaxed);
Ok(())
}
async fn close(&mut self) -> io::Result<()> {
WriteHalf::close(&mut self.socket).await
}
fn can_pass_unix_fd(&self) -> bool {
true
}
fn can_resume_writes(&self) -> bool {
self.cap.is_some()
}
}
async fn finish_a_send_dropped_midway(runtime: &Runtime, conn: &Connection, peer: &UnixStream) {
let (big, next) = (big_signal(), signal(1));
stuck(runtime, pin!(conn.send(&big)), peer).await;
let unwritten = conn.unwritten_len();
assert!(
0 < unwritten && unwritten < big.data().len(),
"{unwritten} of {} bytes left to write",
big.data().len(),
);
let reading = read_in_the_background(peer, big.data().len() + next.data().len());
conn.send(&next).await.unwrap();
let received = reading.join().unwrap();
assert_eq!(received, [&big.data()[..], &next.data()[..]].concat());
assert_eq!(conn.unwritten_len(), 0);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn keep_until_what_was_kept_is_written(
write: &mut SocketWrite,
socket: &Scripted,
status: &SocketStatus,
kept: &Message,
msg: &Message,
) {
let half = kept.data().len() / 2;
futures_lite::future::block_on(async {
write.send_message(kept).await.unwrap();
assert_eq!(status.unwritten.load(Ordering::Relaxed), kept.data().len());
write.send_message(msg).await.unwrap();
assert_eq!(socket.written(), &kept.data()[..half]);
let unwritten = kept.data().len() - half + msg.data().len();
assert_eq!(status.unwritten.load(Ordering::Relaxed), unwritten);
socket.empty();
write.flush().await.unwrap();
});
assert_eq!(
socket.written(),
[&kept.data()[..], &msg.data()[..]].concat()
);
assert_eq!(status.unwritten.load(Ordering::Relaxed), 0);
}
#[cfg(all(target_os = "linux", feature = "handover"))]
fn prepared_status() -> Arc<SocketStatus> {
let status = Arc::new(SocketStatus {
activity_event: Event::new(),
closed: AtomicBool::new(false),
closed_event: Event::new(),
unwritten: AtomicUsize::new(0),
stop: StopStatus::new(),
prepared: Default::default(),
});
status.prepared.set();
status
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[derive(Debug)]
struct Scripted {
full: AtomicBool,
attempts: Mutex<VecDeque<Option<usize>>>,
written: Mutex<Vec<u8>>,
}
#[cfg(all(target_os = "linux", feature = "handover"))]
impl Scripted {
fn new(attempts: impl IntoIterator<Item = Option<usize>>) -> Self {
Self {
full: AtomicBool::new(true),
attempts: Mutex::new(attempts.into_iter().collect()),
written: Mutex::default(),
}
}
fn empty(&self) {
self.full.store(false, Ordering::Relaxed);
}
fn written(&self) -> Vec<u8> {
self.written.lock().unwrap().clone()
}
fn take(&self, buffers: &[IoSlice<'_>], len: usize) -> usize {
let mut written = self.written.lock().unwrap();
let mut taken = 0;
for buffer in buffers {
let part = &buffer[..buffer.len().min(len - taken)];
written.extend_from_slice(part);
taken += part.len();
}
taken
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
impl PollSend for Scripted {
fn poll_send(
&self,
_cx: &mut Context<'_>,
buffers: &[IoSlice<'_>],
_fds: &[BorrowedFd<'_>],
) -> Poll<io::Result<usize>> {
if self.full.load(Ordering::Relaxed) {
return Poll::Pending;
}
Poll::Ready(Ok(self.take(buffers, usize::MAX)))
}
fn try_send(&self, buffers: &[IoSlice<'_>], _fds: &[BorrowedFd<'_>]) -> io::Result<usize> {
match self.attempts.lock().unwrap().pop_front() {
Some(None) => Err(ErrorKind::WouldBlock.into()),
Some(Some(len)) => Ok(self.take(buffers, len)),
None => Ok(self.take(buffers, usize::MAX)),
}
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[derive(Debug)]
struct ScriptedHalf(Arc<Scripted>);
#[cfg(all(target_os = "linux", feature = "handover"))]
#[async_trait::async_trait]
impl WriteHalf for ScriptedHalf {
async fn sendmsg(&mut self, buffer: &[u8], _fds: &[BorrowedFd<'_>]) -> io::Result<usize> {
let buffers = [IoSlice::new(buffer)];
let sent = match self.0.full.load(Ordering::Relaxed) {
true => self.0.try_send(&buffers, &[]),
false => Ok(self.0.take(&buffers, usize::MAX)),
};
match sent {
Err(e) if e.kind() == ErrorKind::WouldBlock => std::future::pending().await,
sent => sent,
}
}
async fn close(&mut self) -> io::Result<()> {
Ok(())
}
fn can_resume_writes(&self) -> bool {
true
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[derive(Debug)]
struct Unused;
#[cfg(all(target_os = "linux", feature = "handover"))]
#[async_trait::async_trait]
impl WriteHalf for Unused {
async fn close(&mut self) -> io::Result<()> {
Ok(())
}
}
async fn stuck<F>(runtime: &Runtime, mut sending: Pin<&mut F>, peer: &UnixStream)
where
F: Future<Output = Result<()>>,
{
while rustix::io::ioctl_fionread(peer).unwrap() == 0 {
let sent = poll_once(sending.as_mut()).await;
assert!(
sent.is_none(),
"the socket took the whole message: {sent:?}"
);
runtime.sleep(Duration::from_millis(1)).await;
}
}
#[cfg(all(target_os = "linux", feature = "handover"))]
async fn without_waiting<F>(runtime: &Runtime, sending: F)
where
F: Future<Output = Result<()>>,
{
let sent = runtime.timeout(sending, Duration::from_secs(5)).await;
assert!(sent.is_ok(), "the send didn't go through: {sent:?}");
}
#[cfg(all(target_os = "linux", feature = "handover"))]
#[derive(Debug, Default)]
struct Woken(AtomicBool);
#[cfg(all(target_os = "linux", feature = "handover"))]
impl Wake for Woken {
fn wake(self: Arc<Self>) {
self.0.store(true, Ordering::Relaxed);
}
}
fn read_in_the_background(peer: &UnixStream, len: usize) -> JoinHandle<Vec<u8>> {
let peer = peer.try_clone().unwrap();
thread::spawn(move || {
let mut received = vec![0; len];
(&peer).read_exact(&mut received).unwrap();
received
})
}
fn assert_nothing_to_read(peer: &UnixStream) {
peer.set_nonblocking(true).unwrap();
let read = (&*peer).read(&mut [0; 1]);
assert!(
matches!(&read, Err(e) if e.kind() == ErrorKind::WouldBlock),
"the peer got {read:?}",
);
}
fn on<'a>(builder: Builder<'a>, runtime: &Runtime) -> Builder<'a> {
match runtime {
#[cfg(feature = "default-rt")]
Runtime::ZRuntime(_) => builder,
#[cfg(feature = "tokio")]
Runtime::Tokio(_) => builder,
Runtime::External(runtime) => builder.runtime(runtime.clone()),
}
}
#[cfg(feature = "unixexec")]
fn wait_for(path: &Path) {
while !path.exists() {
thread::sleep(Duration::from_millis(1));
}
}
fn pipe() -> OwnedFd {
io::pipe().unwrap().0.into()
}
fn signal(n: u32) -> Message {
Message::signal(PATH, INTERFACE, "Test").build(&n).unwrap()
}
fn big_signal() -> Message {
Message::signal(PATH, INTERFACE, "Big")
.build(&vec![0u8; 1024 * 1024])
.unwrap()
}
const CUT: usize = 16;
const CAP: usize = 16;
const BIG_CAP: usize = 64 * 1024;
const PATH: &str = "/org/zbus/Test";
const INTERFACE: &str = "org.zbus.Test";
}