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
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ reads that marker.
from its NFC form with the default options, so a `RegexBuilder` option
such as `case_insensitive` does not reach it; a test that built one
with a decomposed literal should give the flag inline, as `(?i)`. (#847)
- The asciicast header describes the recording it heads. `timestamp` is
the second `Terminal::record` was called in, the "beginning of the
recording session" the asciicast v2 spec defines, rather than the time
of the export, so two exports of one recording are the same document.
`env.TERM` is the `TERM` the child was given, where it was always
`xterm-256color`, and a recording with no frame takes the terminal's
size at `record` rather than 80x24. A test that bounded `timestamp` by
the export should bound it by the call to `record`. (#850)

### Fixed

Expand Down Expand Up @@ -194,6 +202,42 @@ reads that marker.
selftest in CI also extracts the workspace version's section, so a
release pull request that bumps the version without moving
`[Unreleased]` goes red. (#857)
- A `wait_frame` timeout and `Recorder::stop` name a DEC 2026 update the
application began and has not ended, as `wait_idle` does. An
application stalled inside one, on a read or on a query's reply, was
described as one that never emitted a synchronized update, or as one
that had not repainted in response to the input, with advice to use
`wait_until`; `wait_frame` is the right wait for it, and returns once
the update ends. (#837)
- XTGETTCAP answers a key capability with what `send` writes for the key
in the cursor-key mode the application has set, as xterm does: after
`CSI ? 1 h`, which ncurses sends for `keypad()`, `kcuu1` is `\eOA`
where it was `\e[A`, and `khome` and `kend` likewise. Each key is
answered by its termcap name too (`kb`, `ku`, `kd`, `kr`, `kl`, `kh`,
`@7`, `kD`, `kP`, `kN`), the names Vim asks by, which were declined.
(#831)
- `wait_frame` after a `resize` no longer offers the frame of a DEC 2026
update that was open when the resize landed. The application finishes
that frame before it handles `SIGWINCH`, so it was drawn at the old
size, and it was the first frame offered: a predicate the old layout
satisfied passed on it. A timeout while that update is still open
names it. An update a hard reset closes without a frame leaves the
next frame offered. (#836)
- A running `Recorder` no longer keeps every image the application sent.
Each recorded frame holds the graphics log as it stood, so a recording
of 400 repaints each carrying a 1 MiB image held 400 MiB while the
terminal kept 4. A recording now keeps no more image data than
`capture_graphics` across its frames, each image counted once, and
drops its oldest frames, counted in `Recording::dropped()`, to stay
within it. (#855)
- On Linux, `Drop` stops the jobs a shell with job control started, each
in a process group of its own in the child's session. The kernel's
hang-up as the session leader exits reaches the foreground group only,
so a background job that ignored `SIGHUP`, typed at an interactive
`bash` or started by `sh` after `set -m`, ran on after the drop, on a
PTY nobody held. The session's other groups are now hung up and killed
with the child's. macOS and the other Unixes still stop the child's
group only, as `docs/LIMITATIONS.md` says. (#835)

## [0.11.11] - 2026-11-25

Expand Down
493 changes: 427 additions & 66 deletions crates/termlens/src/terminal.rs

Large diffs are not rendered by default.

194 changes: 194 additions & 0 deletions crates/termlens/tests/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,76 @@ fn an_unmatched_end_publishes_no_frame() {
);
}

/// An application stalled inside a DEC 2026 update, on a read or on a
/// query's reply, did emit one: the timeout names the open update, as
/// `wait_idle`'s does, rather than "never emitted" and advice to use
/// `wait_until` (#837).
#[test]
#[cfg_attr(
windows,
ignore = "ConPTY closes a DEC 2026 bracket before the content it wrapped, so no frame holds what was drawn (#149)"
)]
fn a_wait_frame_timeout_names_an_update_left_open_before_any_frame() -> termlens::Result<()> {
let mut t = emit(
Terminal::builder().timeout(Duration::from_secs(10)),
&["--raw", r"\e[?2026hhalf a frame", "--wait"],
)?;
t.wait_until(|s| s.contains("half a frame"))?;
let err = t
.wait_frame_for(|s| s.contains("half"), Duration::from_millis(300))
.unwrap_err();
let msg = err.to_string();
assert!(!msg.contains("never emitted"), "it emitted one: {msg}");
assert!(
msg.contains("began a DEC 2026 synchronized update and has not ended it"),
"the open update is named: {msg}"
);
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}

/// The same once a frame was returned: the next repaint is open, which is
/// not "has not completed a repaint ... if it does not repaint in response
/// to this input" (#837).
#[test]
#[cfg_attr(
windows,
ignore = "ConPTY closes a DEC 2026 bracket before the content it wrapped, so no frame holds what was drawn (#149)"
)]
fn a_wait_frame_timeout_names_an_update_left_open_after_a_frame() -> termlens::Result<()> {
let mut t = emit(
Terminal::builder().timeout(Duration::from_secs(10)),
&[
"--raw",
r"\e[?2026h\e[Hone\e[?2026l",
"--wait",
"--raw",
r"\e[?2026h\e[Htwo",
"--wait",
],
)?;
t.wait_frame(|s| s.contains("one"))?;
t.send(Key::Enter)?;
t.wait_until(|s| s.contains("two"))?;
let err = t
.wait_frame_for(|s| s.contains("two"), Duration::from_millis(300))
.unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("began a DEC 2026 synchronized update and has not ended it"),
"the open update is named: {msg}"
);
assert!(!msg.contains("use wait_until"), "{msg}");
assert!(
!msg.contains("assert on the screen with wait_until"),
"{msg}"
);
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}

#[test]
fn a_defensive_mode_reset_keeps_the_never_emitted_diagnosis() {
// Verbatim from a real crash handler: applications reset terminal modes
Expand Down Expand Up @@ -709,6 +779,130 @@ fn the_repaint_answering_a_resize_is_offered_to_wait_frame() -> termlens::Result
Ok(())
}

/// An update open when the resize lands was begun at the old size: the
/// application finishes that frame before it handles `SIGWINCH`, as one
/// with a spinner or a clock does, so its frame is not offered as the
/// repaint answering the resize (#836).
#[test]
#[cfg_attr(
windows,
ignore = "ConPTY closes a DEC 2026 bracket before the content it wrapped, so no frame holds what was drawn (#149)"
)]
fn a_resize_inside_an_update_does_not_offer_that_update() -> termlens::Result<()> {
let mut t = emit(
Terminal::builder()
.size(80, 24)
.timeout(Duration::from_secs(10)),
// Opens an update at 80x24 and waits for a line; then closes it, and
// repaints at the size the terminal has by then.
&[
"--raw-mode",
"--raw",
r"\e[?2026h\e[H\e[2Jlayout ",
"--winsize",
"--wait",
"--raw",
r"\e[?2026l",
"--sleep",
"200ms",
"--raw",
r"\e[?2026h\e[H\e[2Jlayout ",
"--winsize",
"--raw",
r"\e[?2026l",
"--wait",
],
)?;
t.wait_until(|s| s.contains("layout 80x24"))?; // inside the update
t.resize(100, 30)?;
t.send(Key::Enter)?;
let frame = t.wait_frame(|s| s.contains("layout"))?;
assert!(
frame.row_text(0).starts_with("layout 100x30"),
"the update begun before the resize is not offered:\n{frame}"
);
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}

/// The update open at the resize may never end: a hard reset (RIS) closes
/// it without a frame. The next frame was begun after the resize, so it is
/// the one offered, not skipped in the old update's place (#836).
#[test]
#[cfg_attr(
windows,
ignore = "ConPTY closes a DEC 2026 bracket before the content it wrapped, so no frame holds what was drawn (#149)"
)]
fn a_resize_inside_an_update_a_reset_abandons_offers_the_next_frame() -> termlens::Result<()> {
let mut t = emit(
Terminal::builder()
.size(80, 24)
.timeout(Duration::from_secs(10)),
&[
"--raw-mode",
"--raw",
r"\e[?2026h\e[H\e[2Jstale ",
"--winsize",
"--wait",
// RIS ends the open update with no frame.
"--raw",
r"\ec",
"--sleep",
"200ms",
"--raw",
r"\e[?2026h\e[H\e[2Jlayout ",
"--winsize",
"--raw",
r"\e[?2026l",
"--wait",
],
)?;
t.wait_until(|s| s.contains("stale 80x24"))?;
t.resize(100, 30)?;
t.send(Key::Enter)?;
let frame = t.wait_frame_for(|s| s.contains("layout"), Duration::from_secs(3))?;
assert!(frame.row_text(0).starts_with("layout 100x30"), "{frame}");
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}

/// An update still open at the timeout, begun before the resize, is named
/// as that: its frame will not be offered (#836).
#[test]
#[cfg_attr(
windows,
ignore = "ConPTY closes a DEC 2026 bracket before the content it wrapped, so no frame holds what was drawn (#149)"
)]
fn a_timeout_names_the_update_open_across_a_resize() -> termlens::Result<()> {
let mut t = emit(
Terminal::builder()
.size(80, 24)
.timeout(Duration::from_secs(10)),
&[
"--raw",
r"\e[?2026h\e[Hone\e[?2026l",
"--raw",
r"\e[?2026h\e[Htwo",
"--wait",
],
)?;
t.wait_until(|s| s.repaints() == 1 && s.contains("two"))?;
t.resize(100, 30)?;
let msg = t
.wait_frame_for(|s| s.contains("two"), Duration::from_millis(300))
.unwrap_err()
.to_string();
assert!(
msg.contains("began before the resize to 100x30 has not ended"),
"{msg}"
);
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}

/// `screen()` is the live grid even for an application that brackets
/// every repaint — the tear is real, documented, and wanted for
/// diagnosis. `wait_frame`'s return value is the frame-consistent read.
Expand Down
83 changes: 83 additions & 0 deletions crates/termlens/tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,89 @@ fn drop_stops_a_hangup_ignoring_helper_in_the_child_s_group() -> termlens::Resul
Ok(())
}

/// [`HUP_PROOF_HELPER`]'s body as a script, for a shell with job control
/// to start as a job: `sh job.sh &` puts it in a process group of its own,
/// in the child's session, and `$$` in it is its own pid.
#[cfg(unix)]
const HUP_PROOF_JOB: &str = "trap '' HUP; echo \"helper:$$;\"; \
while [ ! -e go ]; do sleep 0.02; done; : > seen; exec sleep 120\n";

/// A job a shell with job control started is in a process group of its
/// own, which the kernel's hang-up as the session leader exits never
/// reaches: only the foreground group is hung up. One that ignores
/// `SIGHUP` ran on after the drop, on a PTY nobody held (#835). Here the
/// shell is `sh` after `set -m`.
#[cfg(unix)]
#[test]
#[cfg_attr(
not(target_os = "linux"),
ignore = "a job in a group of its own outlives the drop off Linux (docs/LIMITATIONS.md, #835)"
)]
fn drop_stops_a_hangup_ignoring_job_of_a_job_control_shell() -> termlens::Result<()> {
let dir = helper_dir("session-set-m")?;
std::fs::write(dir.join("job.sh"), HUP_PROOF_JOB)?;
let mut t = sh_in(&dir, "set -m; sh job.sh & read _")?;
// Dropped, and so killed, whatever the assertion below decides.
let helper = Helper::armed(&mut t, &dir)?;
assert_ne!(
process_group(&helper.pid),
process_group(&t.pid().expect("a pid").to_string()),
"the job has a process group of its own, or this proves nothing"
);
drop(t);
assert!(
helper.gone(),
"job {} outlived the drop of a `set -m` shell",
helper.pid
);
Ok(())
}

/// The same with an interactive `bash`, the shell a suite most often
/// drives with job control: the job is typed at its prompt (#835).
#[cfg(unix)]
#[test]
#[cfg_attr(
not(target_os = "linux"),
ignore = "a job in a group of its own outlives the drop off Linux (docs/LIMITATIONS.md, #835)"
)]
fn drop_stops_a_hangup_ignoring_job_an_interactive_shell_started() -> termlens::Result<()> {
let dir = helper_dir("session-bash-i")?;
std::fs::write(dir.join("job.sh"), HUP_PROOF_JOB)?;
let mut t = Terminal::builder()
.timeout(Duration::from_secs(10))
.current_dir(&dir)
.env("PS1", "ready> ")
.args(["--norc", "--noprofile", "-i"])
.spawn("bash")?;
t.wait_until(|s| s.contains("ready>"))?;
t.send_str("sh job.sh &")?;
t.send(Key::Enter)?;
let helper = Helper::armed(&mut t, &dir)?;
assert_ne!(
process_group(&helper.pid),
process_group(&t.pid().expect("a pid").to_string()),
"the job has a process group of its own, or this proves nothing"
);
drop(t);
assert!(
helper.gone(),
"job {} outlived the drop of an interactive bash",
helper.pid
);
Ok(())
}

/// The process group of `pid`, as `ps` reports it.
#[cfg(unix)]
fn process_group(pid: &str) -> String {
let out = std::process::Command::new("ps")
.args(["-o", "pgid=", "-p", pid])
.output()
.expect("ps runs");
String::from_utf8_lossy(&out.stdout).trim().to_owned()
}

/// The same once the child has exited, and no wait has reaped it: it is a
/// zombie, which holds the group's id, so the group is killed and the
/// child reaped. Teardown used to reap such a child first and then signal
Expand Down
Loading
Loading