Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions libshpool/src/daemon/pager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ use tracing::{error, info, instrument, span, trace, warn, Level};

use crate::{consts, protocol::ChunkExt as _, tty::TtySizeExt as _};

use super::shell::TtySizeChangeMsg;

// poll relatively quickly to pick up pager exits reasonably fast,
// but still slow enough to spend most of the time parked.
const POLL_MS: u16 = 100;
Expand All @@ -63,9 +65,7 @@ const POLL_MS: u16 = 100;
pub struct PagerCtl {
/// Used to signal size changes so we can correctly trigger
/// a SIGWINCH on the pty.
pub tty_size_change: crossbeam_channel::Sender<TtySize>,
/// Acks the completion of a resize/SIGWINCH.
pub tty_size_change_ack: crossbeam_channel::Receiver<()>,
pub tty_size_change: crossbeam_channel::Sender<TtySizeChangeMsg>,
}

/// Pager is capable of displaying a message to the user via
Expand Down Expand Up @@ -110,18 +110,14 @@ impl Pager {
shell_env: &[(OsString, OsString)],
) -> anyhow::Result<TtySize> {
let (tty_size_change_tx, tty_size_change_rx) = crossbeam_channel::bounded(0);
let (tty_size_change_ack_tx, tty_size_change_ack_rx) = crossbeam_channel::bounded(0);
{
let mut ctl_handle = ctl_slot.lock();
if ctl_handle.is_some() {
return Err(anyhow!("only one pager per session at a time allowed"));
}

trace!("registering PagerCtl");
*ctl_handle = Some(PagerCtl {
tty_size_change: tty_size_change_tx,
tty_size_change_ack: tty_size_change_ack_rx,
});
*ctl_handle = Some(PagerCtl { tty_size_change: tty_size_change_tx });
}
// make sure we reset the control handles
let _ctl_guard = PagerCltGuard { ctl_slot };
Expand Down Expand Up @@ -191,20 +187,19 @@ impl Pager {
// since pagers don't stick around when the client hangs up
// it is not really that importaint. Let's KISS.
while let Ok(size) = tty_size_change_rx.recv() {
info!("recvd new size: {:?}", size);
if let Err(e) = size.set_fd(pty_master_fd) {
info!("recvd new size: {:?}", size.req);
if let Err(e) = size.req.set_fd(pty_master_fd) {
warn!("setting pager size: {:?}", e);
}

{
// register the new size so it will get returned
let mut tty_size = tty_size_ref.lock();
*tty_size = size;
*tty_size = size.req;
}

if let Err(e) = tty_size_change_ack_tx.send(()) {
error!("could not send size change ack: {:?}", e);
break;
if let Err(e) = size.reply.send(()) {
warn!("could not send size change ack: {:?}", e);
}
}
info!("pager size change loop done");
Expand Down
82 changes: 46 additions & 36 deletions libshpool/src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,23 +691,26 @@ impl Server {
for (session, shell_to_client_ctl) in targets.into_iter() {
let _s = span!(Level::INFO, "lock(shell_to_client_ctl)", s = session).entered();
let shell_to_client_ctl = shell_to_client_ctl.lock();
if let Err(err) = shell_to_client_ctl
.client_connection
.send_timeout(shell::ClientConnectionMsg::Disconnect, SESSION_MSG_TIMEOUT)
{
let (client_conn_reply_tx, client_conn_reply_rx) = crossbeam_channel::bounded(1);
if let Err(err) = shell_to_client_ctl.client_connection.send_timeout(
shell::ClientConnMsg {
req: shell::ClientConnReq::Disconnect,
reply: client_conn_reply_tx,
},
SESSION_MSG_TIMEOUT,
) {
error!("sending client detach to shell->client for {}: {:?}", session, err);
not_attached_sessions.push(session);
continue;
}
let status =
match shell_to_client_ctl.client_connection_ack.recv_timeout(SESSION_MSG_TIMEOUT) {
Ok(status) => status,
Err(err) => {
error!("getting client conn ack for {}: {:?}", session, err);
not_attached_sessions.push(session);
continue;
}
};
let status = match client_conn_reply_rx.recv_timeout(SESSION_MSG_TIMEOUT) {
Ok(status) => status,
Err(err) => {
error!("getting client conn ack for {}: {:?}", session, err);
not_attached_sessions.push(session);
continue;
}
};
info!("detached session({}), status = {:?}", session, status);
if let shell::ClientConnectionStatus::DetachNone = status {
not_attached_sessions.push(session);
Expand Down Expand Up @@ -956,16 +959,22 @@ impl Server {
}
};
let _s = span!(Level::INFO, "lock(pager_ctl)").entered();
let (size_change_reply_tx, size_change_reply_rx) = crossbeam_channel::bounded(1);
let pager_ctl = pager_ctl.lock();

if let Some(pager_ctl) = pager_ctl.as_ref() {
info!("resizing pager");
pager_ctl
.tty_size_change
.send_timeout(resize_request.tty_size.clone(), SESSION_MSG_TIMEOUT)
.send_timeout(
shell::TtySizeChangeMsg {
req: resize_request.tty_size.clone(),
reply: size_change_reply_tx,
},
SESSION_MSG_TIMEOUT,
)
.context("sending tty size change to pager")?;
pager_ctl
.tty_size_change_ack
size_change_reply_rx
.recv_timeout(SESSION_MSG_TIMEOUT)
.context("recving tty size change ack from pager")?;
} else {
Expand All @@ -983,10 +992,15 @@ impl Server {

shell_to_client_ctl
.tty_size_change
.send_timeout(resize_request.tty_size, SESSION_MSG_TIMEOUT)
.send_timeout(
shell::TtySizeChangeMsg {
req: resize_request.tty_size,
reply: size_change_reply_tx,
},
SESSION_MSG_TIMEOUT,
)
.context("sending tty size change to shell->client")?;
shell_to_client_ctl
.tty_size_change_ack
size_change_reply_rx
.recv_timeout(SESSION_MSG_TIMEOUT)
.context("recving tty size ack")?;
}
Expand All @@ -1004,14 +1018,20 @@ impl Server {
}
};
let _s = span!(Level::INFO, "lock(shell_to_client_ctl)").entered();
let (client_conn_reply_tx, client_conn_reply_rx) = crossbeam_channel::bounded(1);
let shell_to_client_ctl = shell_to_client_ctl.lock();

shell_to_client_ctl
.client_connection
.send_timeout(shell::ClientConnectionMsg::Disconnect, SESSION_MSG_TIMEOUT)
.send_timeout(
shell::ClientConnMsg {
req: shell::ClientConnReq::Disconnect,
reply: client_conn_reply_tx,
},
SESSION_MSG_TIMEOUT,
)
.context("sending client detach to shell->client")?;
let status = shell_to_client_ctl
.client_connection_ack
let status = client_conn_reply_rx
.recv_timeout(SESSION_MSG_TIMEOUT)
.context("getting client conn ack")?;
info!("detached session({}), status = {:?}", header.session_name, status);
Expand Down Expand Up @@ -1229,17 +1249,13 @@ impl Server {
}
}

// It would be safe to buffer these channels because reply chans are
// stapled to requests, but there is no point, because we always
// do syncronous RPC style calls so it would not actually
// increase parallelism.
let (client_connection_tx, client_connection_rx) = crossbeam_channel::bounded(0);
let (client_connection_ack_tx, client_connection_ack_rx) = crossbeam_channel::bounded(0);
let (tty_size_change_tx, tty_size_change_rx) = crossbeam_channel::bounded(0);
let (tty_size_change_ack_tx, tty_size_change_ack_rx) = crossbeam_channel::bounded(0);

let (heartbeat_tx, heartbeat_rx) = crossbeam_channel::bounded(0);
// One slot rather than a rendezvous, so the shell->client thread can
// always deposit an ack and get back to its select loop. If the
// heartbeat thread gave up waiting, the ack it abandoned sits here and
// is discarded by request id on the next pass.
let (heartbeat_ack_tx, heartbeat_ack_rx) = crossbeam_channel::bounded(1);

// We make this buffered to avoid blocking during a broadcast. There is
// no ack chan so we can afford to buffer a bit.
Expand All @@ -1251,11 +1267,8 @@ impl Server {
let shell_to_client_ctl = Arc::new(Mutex::new(shell::ShellToClientCtl {
next_heartbeat_id: std::sync::atomic::AtomicU64::new(0),
client_connection: client_connection_tx,
client_connection_ack: client_connection_ack_rx,
tty_size_change: tty_size_change_tx,
tty_size_change_ack: tty_size_change_ack_rx,
heartbeat: heartbeat_tx,
heartbeat_ack: heartbeat_ack_rx,
maybe_switch: maybe_switch_tx,
}));

Expand Down Expand Up @@ -1285,11 +1298,8 @@ impl Server {
(None, _) => DEFAULT_OUTPUT_SPOOL_LINES,
},
client_connection: client_connection_rx,
client_connection_ack: client_connection_ack_tx,
tty_size_change: tty_size_change_rx,
tty_size_change_ack: tty_size_change_ack_tx,
heartbeat: heartbeat_rx,
heartbeat_ack: heartbeat_ack_tx,
maybe_switch: maybe_switch_rx,
child_exit_notifier: shell_to_client_child_exit_notifier,
fetch_metadata: fetch_metadata_rx,
Expand Down
Loading
Loading