MCPcopy Create free account
hub / github.com/keploy/keploy / watchParentProcess

Function watchParentProcess

cli/watch_parent_unix.go:42–69  ·  view source on GitHub ↗

watchParentProcess terminates this agent process when its parent keploy client (clientPID) goes away. clientPID is a real process id: the launcher passes the client's os.Getpid() via --client-pid (see pkg/platform/http/agent.go and docker/util.go), and it is consumed as a pid elsewhere too (/proc/<

(ctx context.Context, logger *zap.Logger, clientPID int)

Source from the content-addressed store, hash-verified

40// the agent is usually launched under (whose death PDEATHSIG would otherwise
41// track instead of the real CLI). No-op when clientPID <= 0.
42func watchParentProcess(ctx context.Context, logger *zap.Logger, clientPID int) {
43 if logger != nil {
44 logger.Debug("arming parent-death watchdog", zap.Int("clientPID", clientPID))
45 }
46 if clientPID <= 0 {
47 return
48 }
49 go func() {
50 ticker := time.NewTicker(watchParentInterval)
51 defer ticker.Stop()
52 for {
53 select {
54 case <-ctx.Done():
55 return
56 case <-ticker.C:
57 if !parentAlive(clientPID) {
58 if logger != nil {
59 logger.Info("parent client process exited; self-terminating to release eBPF hooks, DNS and ports",
60 zap.Int("clientPID", clientPID))
61 }
62 // Reuse the normal signal-driven graceful shutdown.
63 _ = syscall.Kill(os.Getpid(), syscall.SIGTERM)
64 return
65 }
66 }
67 }
68 }()
69}
70
71// parentAlive reports whether process pid still exists. Signal 0 performs error
72// checking without sending a signal: ESRCH => the process is gone; any other

Callers 2

AgentFunction · 0.70

Calls 3

parentAliveFunction · 0.85
StopMethod · 0.80
DebugMethod · 0.65

Tested by 1