Leave the deadline of a transfer's negotiation to the caller

LinkSession gave the reply to an OpenRead or OpenWrite 30 seconds, for every
transfer. Only pictures had a deadline before the seam (IMAGE_NEGOTIATION,
which SessionLink still applies to them); a file the user asked for waited for
a slow server as long as it took. With the extra deadline such a transfer
failed with "timed out", and the inner job stayed in voice.rs until the server
opened it, found nobody to hand the connection to, and dropped it.

The driver waits without a deadline now. A caller that drops the call (the
picture's deadline, a cancel) is noticed through the closed reply channel, and
the driver's wait ends, dropping the receiver that the inner job holds.

Test (virtual time): a read and a write wait 120 seconds for the server and
then open; a read that is dropped closes the reply of the inner job. It fails
on the code before this commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-23 22:28:32 +03:00
1 parent 96dece42e6
commit b0b1c82ddc
2 files changed
+87 -15

No files matched your search

+65
View File
@@ -2046,3 +2046,68 @@ async fn a_move_back_after_a_refused_move_is_answered_though_the_link_says_nothi
let back = tokio::time::timeout(Duration::from_secs(2), back).await;
assert_eq!(voice_channel(back.expect("answered at once").unwrap()), GAMES);
}
fn file_path(path: &str) -> v1::FilePath {
v1::FilePath { channel_id: GAMES, path: path.into(), password: String::new() }
}
/// A transfer waits for the server as long as its caller does: the engine
/// puts the deadline on a picture's negotiation and none on a file the user
/// asked for. A caller that gives up takes the link's job with it.
#[tokio::test(start_paused = true)]
async fn a_transfer_waits_for_the_server_for_as_long_as_the_caller_does() {
let (session, _pushes, mut inner) = open(everything()).await;
let long = Duration::from_secs(120);
let read = tokio::spawn(session.read_file(v1::FileRead {
target: Some(v1::file_read::Target::Path(file_path("/a.txt"))),
offset: 0,
length: 0,
}));
let LinkCmd::OpenRead { reply, .. } = within("a read", inner.cmds.recv()).await else {
panic!("a connection to read")
};
tokio::time::sleep(long).await;
assert!(!read.is_finished(), "a read gave up on its own");
let _ = reply.send(Ok((5, Box::pin(std::io::Cursor::new(b"hello".to_vec())))));
let Ok(Ok(download)) = read.await else { panic!("the read opened") };
let sizes: Vec<u64> = download
.head
.items
.iter()
.filter_map(|e| match &e.kind {
Some(v1::entity::Kind::File(file)) => Some(file.size),
_ => None,
})
.collect();
assert_eq!(sizes, [5]);
let write = tokio::spawn(session.write_file(v1::FileWrite {
target: Some(v1::file_write::Target::Path(file_path("/b.txt"))),
size: 3,
offset: 0,
overwrite: true,
..Default::default()
}));
let LinkCmd::OpenWrite { reply, .. } = within("a write", inner.cmds.recv()).await else {
panic!("a connection to write")
};
tokio::time::sleep(long).await;
assert!(!write.is_finished(), "a write gave up on its own");
let (_server, client) = tokio::io::duplex(64);
let _ = reply.send(Ok((0, Box::pin(client))));
assert!(matches!(write.await, Ok(Ok(_))), "the write opened");
// The caller gives up: the link's job is told.
let read = session.read_file(v1::FileRead {
target: Some(v1::file_read::Target::Path(file_path("/c.txt"))),
offset: 0,
length: 0,
});
let LinkCmd::OpenRead { reply, .. } = within("a read", inner.cmds.recv()).await else {
panic!("a connection to read")
};
drop(read);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(reply.is_closed(), "the link's job outlived its caller");
}
+22 -15
View File
@@ -1463,7 +1463,7 @@ impl Driver {
fn read(
&mut self,
request: v1::FileRead,
reply: oneshot::Sender<Result<Download, SessionError>>,
mut reply: oneshot::Sender<Result<Download, SessionError>>,
) {
let Some(v1::file_read::Target::Path(path)) = request.target else {
let _ = reply.send(Err(rejected(
@@ -1482,11 +1482,17 @@ impl Driver {
let (dir, name) = split_path(&path.path);
let channel = path.channel_id;
tokio::spawn(async move {
let opened = match tokio::time::timeout(ANSWER_TIME, rx).await {
Ok(Ok(Ok(opened))) => Ok(opened),
Ok(Ok(Err(text))) => Err(rejected(ErrorCode::Unavailable, text)),
Ok(Err(_)) => Err(rejected(ErrorCode::Unavailable, "the connection ended")),
Err(_) => Err(SessionError::Timeout),
// No deadline of ours: how long a transfer may take to start is the
// caller's to say (the engine gives a picture's negotiation 30 s, a
// file the user asked for as long as the server needs). A caller
// that has given up drops `reply`, and the job goes with `rx`.
let opened = tokio::select! {
() = reply.closed() => return,
opened = rx => match opened {
Ok(Ok(opened)) => Ok(opened),
Ok(Err(text)) => Err(rejected(ErrorCode::Unavailable, text)),
Err(_) => Err(rejected(ErrorCode::Unavailable, "the connection ended")),
},
};
let answer = opened.map(|(size, body): (u64, BoxRead)| Download {
head: v1::Entities {
@@ -1508,7 +1514,7 @@ impl Driver {
fn write(
&mut self,
request: v1::FileWrite,
reply: oneshot::Sender<Result<Upload, SessionError>>,
mut reply: oneshot::Sender<Result<Upload, SessionError>>,
) {
let Some(v1::file_write::Target::Path(path)) = request.target else {
let _ = reply.send(Err(rejected(
@@ -1534,21 +1540,22 @@ impl Driver {
let (dir, name) = split_path(&path.path);
let channel = path.channel_id;
tokio::spawn(async move {
let opened = match tokio::time::timeout(ANSWER_TIME, rx).await {
Ok(Ok(Ok(opened))) => opened,
Ok(Ok(Err(text))) => {
// No deadline of ours, as in `read`.
let opened = tokio::select! {
() = reply.closed() => return,
opened = rx => opened,
};
let opened = match opened {
Ok(Ok(opened)) => opened,
Ok(Err(text)) => {
let _ = reply.send(Err(rejected(ErrorCode::Unavailable, text)));
return;
}
Ok(Err(_)) => {
Err(_) => {
let _ =
reply.send(Err(rejected(ErrorCode::Unavailable, "the connection ended")));
return;
}
Err(_) => {
let _ = reply.send(Err(SessionError::Timeout));
return;
}
};
let (seek, stream): (u64, BoxDuplex) = opened;
if seek != offset {