Skip to content

Commit 736f33d

Browse files
committed
fix: adapt to tokio 1.53+ AsyncFd deprecation and LIFO slot revert
- Replace deprecated AsyncFd::new with safe wrappers around the unsafe AsyncFd::register (tokio 1.53.3+) in openshell-core::net; use them for the sandbox PTY master and the sandbox-backend vsock listener so openshell-sandbox-backend keeps forbid(unsafe_code). - Fix server_managed_sandbox_cas_retries_concurrent_updates hanging under the refreshed lockfile: wrap the synchronous barrier wait in block_in_place. Tokio 1.52.2 reverted LIFO slot stealing, so a sqlx pool waiter woken by a worker that then blocks on the barrier can be stranded in that worker's non-stealable LIFO slot forever. - Raise the workspace tokio requirement to 1.54 (AsyncFd::register range) and refresh the example-workspace lockfiles that path-depend on openshell-core and were stale against the shared lockfile. Signed-off-by: Chris Moore <cmoore@redhat.com>
1 parent 579336c commit 736f33d

10 files changed

Lines changed: 63 additions & 11 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repository = "https://github.com/NVIDIA/OpenShell"
1616
# Product backend selection. Consumer crates inherit this choice.
1717
openshell-crypto = { path = "crates/openshell-crypto", default-features = false, features = ["aws-lc"] }
1818
# Async runtime
19-
tokio = { version = "1.43", features = ["full"] }
19+
tokio = { version = "1.54", features = ["full"] }
2020

2121
# gRPC/Protobuf
2222
tonic = "0.14"

‎crates/openshell-core/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ openshell-otel = { path = "../openshell-otel", optional = true }
4646
[target.'cfg(unix)'.dependencies]
4747
nix = { workspace = true }
4848
rustix = { workspace = true }
49+
socket2 = { workspace = true }
4950

5051
[features]
5152
default = ["telemetry"]

‎crates/openshell-core/src/net.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
//! [`connect_tcp_nodelay_best_effort`]) help avoid the known latency
1515
//! imposed by conflict between Nagle's algorithm and delayed ACK behaviors.
1616
//!
17+
//! The `AsyncFd` registration helpers ([`register_socket_async_fd`],
18+
//! [`register_file_async_fd`]) wrap tokio's I/O-safety-`unsafe`
19+
//! `AsyncFd::register` for owned descriptor types that uphold its contract.
20+
//!
1721
1822
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
1923
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
@@ -285,6 +289,40 @@ fn is_internal_v4(v4: Ipv4Addr) -> bool {
285289
false
286290
}
287291

292+
/// Register an owned [`socket2::Socket`] with the current tokio reactor.
293+
///
294+
/// Tokio's `AsyncFd::register` is `unsafe` because a general `AsRawFd`
295+
/// implementation can expose a descriptor that is closed, reused, or shared
296+
/// while the `AsyncFd` still holds it. A `socket2::Socket` owns its descriptor
297+
/// exclusively and keeps it open and referring to the same file description
298+
/// until dropped, which upholds the contract, so this wrapper is safe.
299+
#[cfg(unix)]
300+
#[allow(unsafe_code)]
301+
pub fn register_socket_async_fd(
302+
socket: socket2::Socket,
303+
) -> std::io::Result<tokio::io::unix::AsyncFd<socket2::Socket>> {
304+
// SAFETY: `socket` is moved into the returned `AsyncFd`, and a
305+
// `socket2::Socket` keeps its descriptor valid, open, and referring to the
306+
// same file description for the value's entire lifetime.
307+
unsafe { tokio::io::unix::AsyncFd::register(socket) }.map_err(std::io::Error::from)
308+
}
309+
310+
/// Register an owned [`std::fs::File`] with the current tokio reactor.
311+
///
312+
/// Sound for the same reason as [`register_socket_async_fd`]: a `File` owns
313+
/// its descriptor exclusively and keeps it open and referring to the same
314+
/// file description until dropped.
315+
#[cfg(unix)]
316+
#[allow(unsafe_code)]
317+
pub fn register_file_async_fd(
318+
file: std::fs::File,
319+
) -> std::io::Result<tokio::io::unix::AsyncFd<std::fs::File>> {
320+
// SAFETY: `file` is moved into the returned `AsyncFd`, and a `File`
321+
// keeps its descriptor valid, open, and referring to the same file
322+
// description for the value's entire lifetime.
323+
unsafe { tokio::io::unix::AsyncFd::register(file) }.map_err(std::io::Error::from)
324+
}
325+
288326
/// Enable `TCP_NODELAY` on a stream, logging (not returning) any failure.
289327
///
290328
/// Disabling Nagle's algorithm keeps small writes from waiting on delayed ACKs.

‎crates/openshell-sandbox-backend/src/runtime_initiated.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl Listener {
256256
socket.bind(&socket2::SockAddr::vsock(libc::VMADDR_CID_ANY, port))?;
257257
socket.listen(64)?;
258258
Ok(Self::Vsock {
259-
listener: tokio::io::unix::AsyncFd::new(socket)?,
259+
listener: openshell_core::net::register_socket_async_fd(socket)?,
260260
guest_cid,
261261
})
262262
}

‎crates/openshell-sandbox/src/main_session.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::sync::{Arc, Mutex};
1414
use bytes::Bytes;
1515
use nix::fcntl::{FcntlArg, OFlag, fcntl};
1616
use rustix::termios::Winsize;
17-
use tokio::io::unix::AsyncFd;
1817
use tokio::io::{AsyncReadExt, AsyncWriteExt};
1918
use tokio::sync::Notify;
2019
use tokio::sync::watch;
@@ -560,7 +559,10 @@ impl MainSession {
560559
) {
561560
match io {
562561
ProcessIo::Pty(master) => {
563-
let master = Arc::new(AsyncFd::new(master).expect("register canonical PTY master"));
562+
let master = Arc::new(
563+
openshell_core::net::register_file_async_fd(master)
564+
.expect("register canonical PTY master"),
565+
);
564566
let reader = Arc::clone(&master);
565567
let output = Arc::clone(this);
566568
tokio::spawn(async move {

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9378,7 +9378,14 @@ mod tests {
93789378
update_sandbox_cas(&state, &sandbox_id, 0, |sandbox| {
93799379
if first_attempt {
93809380
first_attempt = false;
9381-
barrier.wait();
9381+
// Block inside `block_in_place` so the runtime hands
9382+
// this worker's core (including any task parked in its
9383+
// LIFO slot) to a replacement worker before the
9384+
// synchronous barrier wait. Since tokio 1.52.2
9385+
// reverted LIFO slot stealing, a plain `barrier.wait()`
9386+
// can strand a woken pool waiter in the LIFO slot of
9387+
// the blocked worker and deadlock the test.
9388+
tokio::task::block_in_place(|| barrier.wait());
93829389
}
93839390
let annotations = &mut sandbox.metadata.as_mut().unwrap().annotations;
93849391
let count = annotations

‎examples/governance-interceptor/Cargo.lock‎

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/isolation-backend-v8/Cargo.lock‎

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/supervisor-middleware-content-guard/Cargo.lock‎

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)