Skip to content

Count a terminal as a session only while someone is typing in it (#953) - #960

Merged
scttfrdmn merged 4 commits into
mainfrom
fix/953
Oct 17, 2026
Merged

scttfrdmn merged 4 commits into
mainfrom
fix/953

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #953. Stacked on #955 (fix/937), which is stacked on #936 (fix/919). The base is fix/937, so the diff shows only #953. Merge #936 and #955 first.

What was wrong

countActiveSessions is documented to count only sessions with input in the last 5 minutes, "so idle SSH/RDP connections don't block idle detection". On Linux, three checks later in isIdle undid that:

  • sysHasActiveTerminals: any numeric entry in /dev/pts meant "Not idle: Active terminals present". A PTY exists for every open SSH session, detached tmux/screen, docker exec -t and SSM shell, however long it has been idle.
  • sysHasRecentUserActivity: last -s -5min -w lists logins that are still open, whenever they started.
  • hasLoggedInUsers called countActiveSessions again.

So leaving an SSH session open, or a tmux session behind after a job finished, kept --idle-timeout from ever firing.

The follow-up comment's concern was also real. The session parser read w -h -s fields[3] as IDLE, which is the WHAT column when FROM is not printed (procps-ng's default build). isRecentActivity then read an unknown format as "recent", so every session would have counted as active once the PTY check was gone.

What changed

  • Session idle time is the terminal's atime (pkg/agent/ttyidle.go, untagged). The tty layer sets the device node's atime on every read of input, rounded to 8 s. That is what w reports as IDLE, read directly instead of parsed from w's columns. The terminals examined are every PTY in /dev/pts plus each who LINE (console and serial logins), deduplicated, with lines that would escape /dev dropped. A terminal that has gone between listing and stat is skipped. One whose stat fails for another reason counts as active.
  • w is no longer parsed, so the column-layout problem goes away and isRecentActivity is removed.
  • The presence checks are removed from isIdle, along with their sys* implementations on Linux, Windows and the darwin stub and the unused exported wrappers (HasActiveTerminals, HasLoggedInUsers, HasRecentUserActivity; pkg/agent is not in the public surface). On Windows both checks were countActiveSessions() > 0, so Windows behaviour does not change.
  • atime_{linux,darwin,other}.go read the atime, so the logic is tested with real Chtimes fixtures on darwin too.

Kernel behaviour, measured

A probe in an AL2023 container (podman, arm64) opened /dev/ptmx and its /dev/pts/N:

created /dev/pts/0 atime 18:29:33 mtime 18:29:33
after output atime 18:29:33 mtime 18:29:43     <- job output moves mtime only
after input  atime 18:29:53 mtime 18:29:43     <- typed input moves atime

So a fresh login counts as active (atime = creation time) for 5 minutes, and a detached tmux pane printing progress does not count.

Tests (written from the doc comment, rule 5)

  • TestSessionTTYs_OnlyRecentInputCounts: "PTY open, idle 2h, so idle" (the issue's fixture), a detached tmux pane with fresh mtime and stale atime, typing 30 s ago, the 5-minute boundary, a serial-console login found through who with a duplicate PTY line, and no terminals.
  • TestCountTTYSessions_IdlePTYIsNotASession: the production entry point with its seams pointed at a fixture.
  • TestSessionTTYs_DiscoversPTYsAndRejectsEscapes, TestCountActiveTTYs_ErrorHandling, TestParseWhoLines.
  • TestIsIdle_IdlePTYDoesNotBlockIdleStop (linux-tagged): the whole idle decision with an idle PTY and every other signal idle returns idle, and becomes not idle once the PTY has input.

Revert checks. Each revert compiled, and the test ran and failed:

revert failing test
count presence (if true || now.Sub(t) < maxIdle) OnlyRecentInputCounts (4 cases), IdlePTYIsNotASession
re-add a /dev/pts presence check to isIdle TestIsIdle_IdlePTYDoesNotBlockIdleStop ("Not idle: Active terminals present")

The linux-tagged tests were run from a GOOS=linux GOARCH=arm64 test binary inside an AL2023 container, and they pass there. make check passes except for the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. make check-nested passes: all 9 nested modules are clean.

Caveats

  • This has not been observed on an EC2 instance. The atime behaviour was measured in a container on the same kernel tty code path.
  • This is the documented intent, and it is also a behaviour change. Someone watching output without typing (top, tail -f) for longer than 5 minutes, on an instance whose CPU, network, disk and GPU are all idle, is now idle. That matches w's definition of idle and the doc comment. The user is still warned by wall 5 minutes before the stop.
  • A session's 5 minutes start from its last input, and the idle timeout counts from the last tick that saw activity. So an abandoned SSH session stops the instance after --idle-timeout plus up to 5 minutes.

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.38462% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/ttyidle.go 91.48% 2 Missing and 2 partials ⚠️
pkg/agent/atime_linux.go 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

countActiveSessions' contract is "input within the last 5 minutes, so idle
SSH/RDP connections don't block idle detection". On Linux three later checks
in isIdle undid it: any numeric entry in /dev/pts, any still-open login in
`last -s -5min`, and a second call to countActiveSessions. A PTY exists for
every open SSH session, detached tmux/screen, docker exec -t and SSM shell,
so leaving one behind blocked --idle-timeout until TTL.

- Session idle time is now the atime of each terminal's device node, which
  the tty layer sets on every read of input (what w(1) prints as IDLE). The
  set is every PTY under /dev/pts plus each `who` line (console and serial
  logins), deduplicated. Pure, untagged code in ttyidle.go, tested on darwin
  with Chtimes fixtures; atime_{linux,darwin,other}.go read the stat.
- `w -h -s` is no longer parsed, so the FROM-column layout concern from the
  issue's follow-up (fields[3] is WHAT on a default procps-ng build, and an
  unknown format read as "recent") is gone with it. isRecentActivity is
  removed.
- isIdle's presence checks (hasActiveTerminals, hasLoggedInUsers,
  hasRecentUserActivity) and their sys* implementations and unused exported
  wrappers are removed on every OS. On Windows they were already
  countActiveSessions() > 0, so behaviour there is unchanged.

Verified in an AL2023 container that a new PTY's atime is its creation
time, output moves only mtime, and input moves atime.

(#953)
@scttfrdmn
scttfrdmn merged commit 3b7c028 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/953 branch October 17, 2026 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux idle check: any /dev/pts entry or still-open login blocks idle-stop forever, defeating countActiveSessions' 'idle SSH sessions don't block' design

1 participant