Repository navigation
Conversation
This was referenced Oct 15, 2026
Codecov Report❌ Patch coverage is
📢 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)
…aves unused (#953)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
countActiveSessionsis 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 inisIdleundid that:sysHasActiveTerminals: any numeric entry in/dev/ptsmeant "Not idle: Active terminals present". A PTY exists for every open SSH session, detached tmux/screen,docker exec -tand SSM shell, however long it has been idle.sysHasRecentUserActivity:last -s -5min -wlists logins that are still open, whenever they started.hasLoggedInUserscalledcountActiveSessionsagain.So leaving an SSH session open, or a tmux session behind after a job finished, kept
--idle-timeoutfrom ever firing.The follow-up comment's concern was also real. The session parser read
w -h -sfields[3]as IDLE, which is the WHAT column when FROM is not printed (procps-ng's default build).isRecentActivitythen read an unknown format as "recent", so every session would have counted as active once the PTY check was gone.What changed
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 whatwreports as IDLE, read directly instead of parsed fromw's columns. The terminals examined are every PTY in/dev/ptsplus eachwhoLINE (console and serial logins), deduplicated, with lines that would escape/devdropped. A terminal that has gone between listing and stat is skipped. One whose stat fails for another reason counts as active.wis no longer parsed, so the column-layout problem goes away andisRecentActivityis removed.isIdle, along with theirsys*implementations on Linux, Windows and the darwin stub and the unused exported wrappers (HasActiveTerminals,HasLoggedInUsers,HasRecentUserActivity;pkg/agentis not in the public surface). On Windows both checks werecountActiveSessions() > 0, so Windows behaviour does not change.atime_{linux,darwin,other}.goread the atime, so the logic is tested with realChtimesfixtures on darwin too.Kernel behaviour, measured
A probe in an AL2023 container (podman, arm64) opened
/dev/ptmxand its/dev/pts/N: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 throughwhowith 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:
if true || now.Sub(t) < maxIdle)OnlyRecentInputCounts(4 cases),IdlePTYIsNotASession/dev/ptspresence check toisIdleTestIsIdle_IdlePTYDoesNotBlockIdleStop("Not idle: Active terminals present")The linux-tagged tests were run from a
GOOS=linux GOARCH=arm64test binary inside an AL2023 container, and they pass there.make checkpasses except for the known Docker-onlyTestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker.make check-nestedpasses: all 9 nested modules are clean.Caveats
top,tail -f) for longer than 5 minutes, on an instance whose CPU, network, disk and GPU are all idle, is now idle. That matchesw's definition of idle and the doc comment. The user is still warned bywall5 minutes before the stop.--idle-timeoutplus up to 5 minutes.