Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Files terminal: close without asking when the shell is idle

oxydizer0c62af8b0a7e
diff --git a/src/files/AGENTS.md b/src/files/AGENTS.md
index fe4ec1a..f4ad7b2 100644
--- a/src/files/AGENTS.md
+++ b/src/files/AGENTS.md
@@ -40,8 +40,9 @@ and headless fixtures. The pinned library is compiled only into Files/tests.
title, foreground PID or quiet output alone never authorizes a directory
change. Unsupported and nested shells keep pending following suspended.
* Spawn the fresh helper with `posix_spawn`, so PTY setup and scope placement
- never run in a forked multithreaded Files child. Confirm every live session
- on close; bound asynchronous cleanup and retain the session leader until
+ never run in a forked multithreaded Files child. Close asks only when
+ `ft_active` finds a foreground job or another session member (an idle shell
+ is just hung up); bound asynchronous cleanup and retain the session leader until
session members have been signaled through pidfds and it can be reaped.
* Cell metrics use the exact shared mono advance. Paint non-ASCII/wide cells
separately so fallback shaping cannot move later cells. Preserve the
diff --git a/src/files/app_terminal.zig b/src/files/app_terminal.zig
index dddd731..c2a3409 100644
--- a/src/files/app_terminal.zig
+++ b/src/files/app_terminal.zig
@@ -468,8 +468,10 @@ pub fn terminalRequestClose(self: *App) void {
if (self.terminal.close_at != null) return;
if (self.terminal.session) |session| if (tc.ft_running(session)) {
if (self.dialog != null) return;
- // A snapshot cannot prove that no job began since it arrived. Confirm
- // every live session, including hidden and unintegrated shells.
+ // Asked of the kernel now, not of the integration snapshot, which
+ // cannot prove that no job began since it arrived. Hidden and
+ // unintegrated shells count; an idle shell is just hung up.
+ if (!tc.ft_active(session)) return self.terminalConfirmClose();
self.dialog = .{ .kind = .terminal_close, .title = tr("Close terminal session?") };
self.invalidate();
return;
diff --git a/src/files/terminal/engine.c b/src/files/terminal/engine.c
index a1103bc..a9534b3 100644
--- a/src/files/terminal/engine.c
+++ b/src/files/terminal/engine.c
@@ -329,6 +329,26 @@ void ft_io(FT *t, bool read_pty, bool write_pty, bool channel, bool exited) {
}
bool ft_changed(FT *t) { bool changed=t->changed; t->changed=false; return changed; }
bool ft_running(const FT *t) { return t->pid>0 && !t->exited; }
+/* Whether closing would end work: a foreground job on the PTY, or any other
+ * process of the session (background jobs, nested shells, ssh). An idle shell
+ * at its prompt is not work. Read from the kernel at the moment of asking, so
+ * it cannot be stale, and anything that cannot be established counts as
+ * active. Runs only when the window is asked to close. */
+bool ft_active(const FT *t) {
+ if (!ft_running(t)) return false;
+ if (t->master<0 || tcgetpgrp(t->master)!=t->pid || getsid(t->pid)!=t->pid) return true;
+ DIR *proc=opendir("/proc");
+ if (!proc) return true;
+ bool active=false;
+ struct dirent *entry;
+ while (!active && (entry=readdir(proc))) {
+ char *end; long value=strtol(entry->d_name,&end,10);
+ if (!*entry->d_name || *end || value<=0 || value>INT_MAX || value==t->pid) continue;
+ active=getsid((pid_t)value)==t->pid;
+ }
+ closedir(proc);
+ return active;
+}
bool ft_exited(const FT *t) { return t->exited; }
int ft_exit_status(const FT *t) { return t->exit_status; }
const char *ft_error(const FT *t) { return t->error; }
diff --git a/src/files/terminal/engine.h b/src/files/terminal/engine.h
index 8fd2a1c..258fc68 100644
--- a/src/files/terminal/engine.h
+++ b/src/files/terminal/engine.h
@@ -33,6 +33,7 @@ bool ft_wants_request(const FT *t);
void ft_io(FT *t, bool read_pty, bool write_pty, bool channel, bool exited);
bool ft_changed(FT *t);
bool ft_running(const FT *t);
+bool ft_active(const FT *t);
bool ft_exited(const FT *t);
int ft_exit_status(const FT *t);
const char *ft_error(const FT *t);
diff --git a/tests/files_terminal.py b/tests/files_terminal.py
index c11ad67..cb55d14 100644
--- a/tests/files_terminal.py
+++ b/tests/files_terminal.py
@@ -442,6 +442,20 @@ def ui_tests(tmp, home, env):
other_shell = wait_for(lambda: int(pidfile.read_text()) if pidfile.read_text() != str(shell) else None, 'independent terminal')
assert other_shell != shell
ipc.focus_window(ident)
+ # An idle shell is not work: only a job asks for confirmation.
+ def session_members():
+ members = []
+ for entry in Path('/proc').iterdir():
+ try:
+ if entry.name.isdigit() and int(entry.name) != shell and os.getsid(int(entry.name)) == shell:
+ members.append(int(entry.name))
+ except OSError:
+ pass
+ return members
+ assert not session_members(), 'idle shell has no other session members'
+ command('sleep 60')
+ wait_for(session_members, 'foreground job started')
+ key(62)
# Close confirmation also covers the hidden terminal; cancellation preserves it.
ipc.action('close_window', {'id': ident})
time.sleep(.15)
@@ -454,7 +468,11 @@ def ui_tests(tmp, home, env):
first.wait(timeout=4)
assert not Path(f'/proc/{shell}').exists(), 'shell reaped on close'
assert second.poll() is None and Path(f'/proc/{other_shell}').exists()
- print('Files: F4/repeat, input, copy/paste, selection, external launch, following, divider/resize, idle, workspaces, minimize, independent windows and close/cancel', flush=True)
+ # Opened once and left alone: closing needs no confirmation.
+ ipc.action('close_window', {'id': second_window['id']})
+ second.wait(timeout=4)
+ assert not Path(f'/proc/{other_shell}').exists(), 'idle shell hung up on close'
+ print('Files: F4/repeat, input, copy/paste, selection, external launch, following, divider/resize, idle, workspaces, minimize, independent windows and close/cancel and idle close', flush=True)
except Exception:
for path in tmp.glob('*.log'):
print(path.name, path.read_text()[-6000:])