mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 16:51:18 -08:00
Monitor teardown SIGHUPed and SIGKILLed every remembered child group when the transport closed. sshd signals nothing at a non-PTY session end; background jobs with redirected stdio survive and reparent to init. Clients rely on that: VS Code Remote-SSH starts its remote server as a plain background job in the session's group and expects it to outlive connection drops, and tailscale upgrades run as non-PTY execs whose work should survive a daemon restart. Now only SSHTerminationError causes (revocation, session-duration, recording failure) kill the process tree. Everything else — client disconnect, daemon shutdown — closes I/O without signals: PTY masters close and the driver SIGHUPs the foreground group, non-PTY processes exit on EOF and SIGPIPE or keep running. Monitor teardown kills only forwarding children, unless revocation armed the new kill-all op first, since its per-session kicks race the close. No zombie leak: the monitor reaps its table, and reparented survivors are reaped by init. Fixes #22076 Signed-off-by: James Tucker <[email protected]> Change-Id: I70b4ef40035330dd542d256ba79d889a12426a74
119 lines
3.8 KiB
Go
119 lines
3.8 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux && !android
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestMonitorWatchdogHelperProcess is re-exec'd as a monitor-owned session
|
|
// or one-shot forwarding child. Both ignore parent-death SIGHUP so only
|
|
// orderly monitor teardown (including its SIGKILL pass) can stop them.
|
|
func TestMonitorWatchdogHelperProcess(t *testing.T) {
|
|
for i, arg := range os.Args {
|
|
if arg != "--" || i+2 >= len(os.Args) || os.Args[i+1] != "watchdog-helper" {
|
|
continue
|
|
}
|
|
signal.Ignore(syscall.SIGHUP)
|
|
if err := os.WriteFile(os.Args[i+2], []byte(strconv.Itoa(os.Getpid())), 0600); err != nil {
|
|
os.Exit(3)
|
|
}
|
|
time.Sleep(25 * time.Second)
|
|
os.Exit(0)
|
|
}
|
|
}
|
|
|
|
// TestMonitorCloseWatchdogForwardChild reproduces the case where a stalled
|
|
// forwarding op consumes the watchdog before the monitor can tear down its
|
|
// HUP-immune session and forwarding child groups.
|
|
func TestMonitorCloseWatchdogForwardChild(t *testing.T) {
|
|
markerDir := t.TempDir()
|
|
forwardMarker := filepath.Join(markerDir, "forward-pid")
|
|
t.Setenv("TS_TEST_SSH_FWD_PREFIX", strings.Join([]string{
|
|
"-test.run=^TestMonitorWatchdogHelperProcess$", "-test.timeout=0", "--", "watchdog-helper", forwardMarker,
|
|
}, " "))
|
|
mc := startTestMonitor(t, os.Getuid(), os.Getgid(), testGroups(t))
|
|
|
|
// The session must be running and ignoring HUP before Close starts.
|
|
sessionMarker := filepath.Join(markerDir, "session-pid")
|
|
ctx, cancel := testCtx()
|
|
session, err := mc.Exec(ctx, []string{monitorExe(t), "-test.run=^TestMonitorWatchdogHelperProcess$", "-test.timeout=0", "--", "watchdog-helper", sessionMarker}, nil, "/", false, nil)
|
|
cancel()
|
|
if err != nil {
|
|
t.Fatalf("start session: %v", err)
|
|
}
|
|
defer func() {
|
|
// Only signal this test's own session group, never a reused PID.
|
|
if b, err := os.ReadFile(sessionMarker); err == nil && strings.TrimSpace(string(b)) == strconv.Itoa(session.PID) && !processDead(session.PID) {
|
|
_ = syscall.Kill(-session.PID, syscall.SIGKILL)
|
|
}
|
|
}()
|
|
waitWatchdogMarker(t, sessionMarker, session.PID)
|
|
|
|
requestDone := make(chan error, 1)
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 16*time.Second)
|
|
defer cancel()
|
|
_, err := mc.Listen(ctx, "tcp", "127.0.0.1:0", false)
|
|
requestDone <- err
|
|
}()
|
|
forwardPID := waitWatchdogMarker(t, forwardMarker, 0)
|
|
defer func() {
|
|
if !processDead(forwardPID) {
|
|
_ = syscall.Kill(forwardPID, syscall.SIGKILL)
|
|
}
|
|
}()
|
|
|
|
start := time.Now()
|
|
kctx, kcancel := context.WithTimeout(context.Background(), 16*time.Second)
|
|
if err := mc.KillAll(kctx); err != nil {
|
|
t.Errorf("arming kill-all: %v", err)
|
|
}
|
|
kcancel()
|
|
err = mc.Close()
|
|
t.Logf("Close duration=%v error=%v; session=%d forward=%d", time.Since(start), err, session.PID, forwardPID)
|
|
if err != nil {
|
|
t.Errorf("monitor failed to complete teardown: %v", err)
|
|
}
|
|
select {
|
|
case err := <-requestDone:
|
|
t.Logf("inflight Listen returned: %v", err)
|
|
case <-time.After(2 * time.Second):
|
|
t.Error("inflight Listen did not return after Close")
|
|
}
|
|
if !processDead(forwardPID) {
|
|
t.Errorf("HUP-immune forwarding child %d survived Close", forwardPID)
|
|
}
|
|
if !processDead(session.PID) {
|
|
t.Errorf("HUP-immune session child %d survived Close", session.PID)
|
|
}
|
|
}
|
|
|
|
func waitWatchdogMarker(t *testing.T, path string, wantPID int) int {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if b, err := os.ReadFile(path); err == nil {
|
|
pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
|
|
if err != nil || pid <= 0 || (wantPID > 0 && pid != wantPID) {
|
|
t.Fatalf("invalid helper PID marker %q: %q", path, b)
|
|
}
|
|
return pid
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
t.Fatalf("helper did not write PID marker %q", path)
|
|
return 0
|
|
}
|