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
2 changes: 1 addition & 1 deletion libshpool/src/daemon/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn wait_for_startup(pty_master: &mut shpool_pty::fork::Master) -> anyhow::Result

let len = pty_master.read(&mut buf).context("reading chunk to scan for startup")?;
if len == 0 {
continue;
return Err(anyhow!("EOF during shell startup"));
}
let buf = &buf[..len];
debug!("buf='{}'", String::from_utf8_lossy(buf));
Expand Down
40 changes: 38 additions & 2 deletions shpool/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use crate::support::{daemon::DaemonArgs, tmpdir};
fn list_not_blocked_by_slow_shell_spawn() -> anyhow::Result<()> {
let tmp_dir = tmpdir::Dir::new("/tmp/shpool-test")?;

let config_tmpl = fs::read_to_string(support::testdata_file("hang_shell.toml.tmpl"))?;
let config_tmpl = fs::read_to_string(support::testdata_file("custom_shell.toml.tmpl"))?;
let config_contents = config_tmpl
.replace("SHELL", support::testdata_file("hang_shell.sh").to_string_lossy().as_ref());
let config_file = tmp_dir.path().join("motd_dump.toml");
let config_file = tmp_dir.path().join("custom_shell.toml");
{
let mut f = fs::File::create(&config_file)?;
f.write_all(config_contents.as_bytes())?;
Expand Down Expand Up @@ -83,3 +83,39 @@ fn list_not_blocked_by_slow_shell_spawn() -> anyhow::Result<()> {
}
}
}

/// Regression test for a bug where shpool would loop forever if the shell
/// exited immediately during startup while we were waiting for the
/// startup sentinel.
#[test]
#[timeout(10000)]
fn no_loop_on_shell_exit_during_startup() -> anyhow::Result<()> {
let tmp_dir = tmpdir::Dir::new("/tmp/shpool-test")?;

let config_tmpl = fs::read_to_string(support::testdata_file("custom_shell.toml.tmpl"))?;
// Use /bin/true as the shell so it exits immediately.
// We need to trigger wait_for_startup, which happens when prompt_prefix is set.
let config_contents = config_tmpl.replace("SHELL", "/bin/true");
let config_file = tmp_dir.path().join("exit_shell.toml");
{
let mut f = fs::File::create(&config_file)?;
f.write_all(config_contents.as_bytes())?;
}

let mut daemon_proc = support::daemon::Proc::new(&config_file, DaemonArgs::default())
.context("starting daemon proc")?;

// Try to attach. This should trigger wait_for_startup because prompt_prefix is
// set in hang_shell.toml.tmpl.
// The shell (/bin/true) will exit immediately, causing wait_for_startup to get
// EOF. On BUGGY code: this loops forever in the daemon.
// On FIXED code: this returns an error in the daemon, and the attach proc
// finishes.
let mut attach_proc =
daemon_proc.attach("sh1", Default::default()).context("starting attach proc")?;

// Wait for the attach process to exit.
let _status = attach_proc.proc.wait().context("waiting for attach proc")?;

Ok(())
}
Loading