Skip to content

Commit

Permalink
Ensure that Server::service will return when the connection terminates
Browse files Browse the repository at this point in the history
Otherwise the per-connection helper processes used by Capsicum would
never exit.
  • Loading branch information
asomers committed Jan 10, 2024
1 parent 9e9de4c commit 8565d02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/server/controlchan/control_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use tokio::{
mpsc::{channel, Receiver, Sender},
Mutex,
},
task::JoinHandle,
};
use tokio_util::codec::{Decoder, Framed};

Expand Down Expand Up @@ -81,7 +82,7 @@ pub(crate) async fn spawn<Storage, User>(
proxyloop_msg_tx: Option<ProxyLoopSender<Storage, User>>,
mut shutdown: shutdown::Listener,
failed_logins: Option<Arc<FailedLoginsCache>>,
) -> Result<(), ControlChanError>
) -> Result<JoinHandle<()>, ControlChanError>
where
User: UserDetail + 'static,
Storage: StorageBackend<User> + 'static,
Expand Down Expand Up @@ -182,7 +183,7 @@ where
reply_sink.send(Reply::new(ReplyCode::ServiceReady, config.greeting)).await?;
reply_sink.flush().await?;

tokio::spawn(async move {
let jh = tokio::spawn(async move {
// The control channel event loop
slog::info!(logger, "Starting control loop");
loop {
Expand Down Expand Up @@ -297,7 +298,7 @@ where
}
});

Ok(())
Ok(jh)
}

// gets the reply to be sent to the client and tells if the connection should be closed.
Expand Down
13 changes: 9 additions & 4 deletions src/server/ftpserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,15 @@ where
let shutdown_listener = shutdown_notifier.subscribe().await;
slog::debug!(self.logger, "Servicing control connection from");
let result = controlchan::spawn_loop::<Storage, User>((&options).into(), tcp_stream, None, None, shutdown_listener, failed_logins.clone()).await;
if let Err(err) = result {
slog::error!(self.logger, "Could not spawn control channel loop: {:?}", err);
} else {
shutdown_notifier.linger().await;
match result {
Err(err) => {
slog::error!(self.logger, "Could not spawn control channel loop: {:?}", err);
}
Ok(jh) => {
if let Err(e) = jh.await {
slog::error!(self.logger, "Control loop failed to complete: {:?}", e);
}
}
}
Ok(())
}
Expand Down

0 comments on commit 8565d02

Please sign in to comment.