diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index f84761d..426df25 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -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 = 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"); +} diff --git a/crates/voelin-core/src/ts_session/driver.rs b/crates/voelin-core/src/ts_session/driver.rs index 6d8dc7c..bd2efec 100644 --- a/crates/voelin-core/src/ts_session/driver.rs +++ b/crates/voelin-core/src/ts_session/driver.rs @@ -1463,7 +1463,7 @@ impl Driver { fn read( &mut self, request: v1::FileRead, - reply: oneshot::Sender>, + mut reply: oneshot::Sender>, ) { 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>, + mut reply: oneshot::Sender>, ) { 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 {