fix regression in xous-names blocking connect

The `libstd` implementation has a bug where it was going through
a vector and mutating it by removing items from the vector,
but not updating the indices.

This fix goes through the vector in reverse order, so that removed
items don't affect the position of the next items in iteration order.

This seems to make the blocking connect much more stable.
This commit is contained in:
bunnie 2023-01-31 15:06:27 +08:00
parent 21778f5895
commit f2068f2c89
2 changed files with 32 additions and 41 deletions

View file

@ -161,45 +161,37 @@ impl XousNames {
/// This call uses the API already in place in `libstd`, hence the different style of
/// argument passing, and tons of `unsafe` code.
pub fn request_connection_blocking(&self, name: &str) -> Result<xous::CID, xous::Error> {
match self.request_connection(name) { // try a direct connection first
Ok(val) => Ok(val),
Err(xous::Error::AccessDenied) => Err(xous::Error::AccessDenied),
_ => {
// if it fails, yield the slice, and then register ourselves for the blocking path
xous::yield_slice();
let mut cr: ConnectRequest = Default::default();
let name_bytes = name.as_bytes();
let mut cr: ConnectRequest = Default::default();
let name_bytes = name.as_bytes();
// Set the string length to the length of the passed-in String,
// or the maximum possible length. Which ever is smaller.
cr.len = cr.name.len().min(name_bytes.len()) as u32;
// Set the string length to the length of the passed-in String,
// or the maximum possible length. Which ever is smaller.
cr.len = cr.name.len().min(name_bytes.len()) as u32;
// Copy the string into our backing store.
for (&src_byte, dest_byte) in name_bytes.iter().zip(&mut cr.name) {
*dest_byte = src_byte;
}
log::info!("connection requested {}", name);
let msg = xous::MemoryMessage {
id: api::Opcode::BlockingConnect.to_usize().unwrap(),
buf: unsafe{ // safety: `cr` is #[repr(C, align(4096))], and should be exactly on page in size
xous::MemoryRange::new(&mut cr as *mut _ as *mut u8 as usize, core::mem::size_of::<ConnectRequest>())?
},
offset: None,
valid: xous::MemorySize::new(cr.len as usize),
};
xous::send_message(self.conn, xous::Message::MutableBorrow(msg))?;
// Copy the string into our backing store.
for (&src_byte, dest_byte) in name_bytes.iter().zip(&mut cr.name) {
*dest_byte = src_byte;
}
log::debug!("connection requested {}", name);
let msg = xous::MemoryMessage {
id: api::Opcode::BlockingConnect.to_usize().unwrap(),
buf: unsafe{ // safety: `cr` is #[repr(C, align(4096))], and should be exactly on page in size
xous::MemoryRange::new(&mut cr as *mut _ as *mut u8 as usize, core::mem::size_of::<ConnectRequest>())?
},
offset: None,
valid: xous::MemorySize::new(cr.len as usize),
};
xous::send_message(self.conn, xous::Message::MutableBorrow(msg))?;
let response_ptr = &cr as *const ConnectRequest as *const u32;
let result = unsafe { response_ptr.read() }; // safety: because that's how it was packed on the server, a naked u32
let response_ptr = &cr as *const ConnectRequest as *const u32;
let result = unsafe { response_ptr.read() }; // safety: because that's how it was packed on the server, a naked u32
if result == 0 {
let cid = unsafe { response_ptr.add(1).read() }.into(); // safety: because that's how it was packed on the server
log::info!("connected to {}:{}", name, cid);
Ok(cid)
} else {
Err(xous::Error::InternalError)
}
}
if result == 0 {
let cid = unsafe { response_ptr.add(1).read() }.into(); // safety: because that's how it was packed on the server
log::debug!("connected to {}:{}", name, cid);
Ok(cid)
} else {
Err(xous::Error::InternalError)
}
}

View file

@ -437,10 +437,10 @@ fn main() -> ! {
// See if we have any requests matching this server ID. If so, make the
// connection. Note that this could be replaced by `drain_filter()` when
// that is stabilized
let mut i = 0;
while i < waiting_connections.len() {
if name_from_msg(&waiting_connections[i]) == Ok(name) {
let mut msg = waiting_connections.remove(i);
let mut i = waiting_connections.len() as isize - 1;
while i >= 0 {
if name_from_msg(&waiting_connections[i as usize]) == Ok(name) {
let mut msg = waiting_connections.remove(i as usize);
match blocking_connect(&mut msg, &mut name_table) {
Err(e) => respond_connect_error(msg, e),
Ok(ConnectSuccess::Connected(cid, disc)) => {
@ -450,9 +450,8 @@ fn main() -> ! {
panic!("message connection attempt resulted in `Wait` even though it ought to exist");
}
}
} else {
i += 1;
}
i -= 1;
}
}
}