mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-04 23:52:20 -08:00
Since 133489986 (#22076), the session monitor keeps a terminated
leader unreaped until its SIGKILL deadline so its PID and process
group can't be reused. But it also withheld the exit notification
until then, so every canceled monitored session waited out the full
HUP escalation (5s by default) after its shell had already exited.
Report the exit as soon as the leader dies, still reaping only after
SIGKILL. On Linux, read the zombie's wait status with waitid(WNOWAIT),
not from /proc/<pid>/stat, whose exit_code reads as 0 when the monitor
may not ptrace the child, such as the root login process of a non-root
user's PTY session. On macOS and FreeBSD, get it from a kqueue
EVFILT_PROC NOTE_EXIT event registered at spawn and drained when the
monitor sweeps on SIGCHLD. OpenBSD keeps the old behavior until it can
be tested there.
That same commit took TestSessionMonitorCancellation, which sets a 30s
escalation, from 0.1s to 60s, making it one of our slowest tests.
Split it into pipe and PTY top-level tests so the test runner can
schedule them independently, and they now take 2s each on Linux and
macOS. Add TestMonitorTerminateReportsExitEarly, which also runs on
FreeBSD, and TestZombieWaitStatus for the Linux siginfo decoding.
Updates #deflake
Change-Id: I6ccdff33a8d4e4c96a84a27c0dbb64ac74954f47
Signed-off-by: Brad Fitzpatrick <[email protected]>
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build (darwin && !ios) || freebsd
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// exitWatcher learns session children's wait statuses without reaping
|
|
// them, so a terminated session's exit can be reported while its leader
|
|
// stays unreaped until group SIGKILL.
|
|
//
|
|
// It registers an EVFILT_PROC NOTE_EXIT filter per child on a kqueue,
|
|
// which the kernel triggers at exit with the wait status in the event
|
|
// data. The monitor loop wakes on the child's SIGCHLD, posted after the
|
|
// NOTE_EXIT event, and drains the kqueue without blocking from
|
|
// [monitor.sweep], so no goroutine reads it.
|
|
//
|
|
// The zero value is ready to use. The kqueue is created on first use.
|
|
type exitWatcher struct {
|
|
kq int // kqueue descriptor, valid if open
|
|
open bool // kq is open
|
|
failed bool // kqueue creation failed; watch nothing
|
|
}
|
|
|
|
// watch registers for pid's exit. Failure leaves pid to be reported
|
|
// when it is reaped.
|
|
func (w *exitWatcher) watch(pid int) {
|
|
if !w.open {
|
|
if w.failed {
|
|
return
|
|
}
|
|
kq, err := unix.Kqueue()
|
|
if err != nil {
|
|
w.failed = true
|
|
return
|
|
}
|
|
unix.CloseOnExec(kq)
|
|
w.kq, w.open = kq, true
|
|
}
|
|
ev := unix.Kevent_t{
|
|
Ident: uint64(pid),
|
|
Filter: unix.EVFILT_PROC,
|
|
Flags: unix.EV_ADD | unix.EV_ONESHOT,
|
|
Fflags: noteExitFflags,
|
|
}
|
|
// ESRCH means pid is already gone, so it is reaped and reported
|
|
// by the next sweep.
|
|
unix.Kevent(w.kq, []unix.Kevent_t{ev}, nil, nil)
|
|
}
|
|
|
|
// drain records the wait status of each exited child in children
|
|
// without blocking. Events for pids not in children, already reaped,
|
|
// are discarded.
|
|
func (w *exitWatcher) drain(children map[int]*monChild) {
|
|
if !w.open {
|
|
return
|
|
}
|
|
var evs [16]unix.Kevent_t
|
|
var zero unix.Timespec
|
|
for {
|
|
n, err := unix.Kevent(w.kq, nil, evs[:], &zero)
|
|
if err != nil || n <= 0 {
|
|
return
|
|
}
|
|
for _, ev := range evs[:n] {
|
|
// On macOS, Data holds the wait status only if the event
|
|
// reports NOTE_EXITSTATUS.
|
|
if ev.Filter != unix.EVFILT_PROC || ev.Fflags¬eExitFflags != noteExitFflags || ev.Flags&unix.EV_ERROR != 0 {
|
|
continue
|
|
}
|
|
if c, ok := children[int(ev.Ident)]; ok {
|
|
c.exitWS, c.exited = unix.WaitStatus(ev.Data), true
|
|
}
|
|
}
|
|
if n < len(evs) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// close releases the kqueue.
|
|
func (w *exitWatcher) close() {
|
|
if w.open {
|
|
unix.Close(w.kq)
|
|
w.open = false
|
|
}
|
|
}
|
|
|
|
// zombieWaitStatus reports false. These platforms learn exit statuses
|
|
// from [exitWatcher] instead.
|
|
func zombieWaitStatus(pid int) (ws unix.WaitStatus, ok bool) {
|
|
return 0, false
|
|
}
|