Skip to content

guard: fix flaky lapse test (EINTR, fake daemon fd reuse); doctor: assurance for new sessions - #1

Merged
edimarlnx merged 3 commits into
mainfrom
fix-lapse-test-race
Nov 4, 2026
Merged

edimarlnx merged 3 commits into
mainfrom
fix-lapse-test-race

Conversation

@edimarlnx

Copy link
Copy Markdown
Contributor

Flaky TestSessionLapseIsPermanent

Stressing it turned up three causes:

  1. Test race. check marks the lapse before it sends the new protect request, so the test could read the daemon's protect count too early. The test now waits for the request, then for a later check the daemon confirms. Then it asserts the session is not strong. No fixed sleeps.
  2. EINTR in the client (real bug). The client socket has SO_RCVTIMEO/SO_SNDTIMEO, so recvmsg/sendmsg return EINTR on a signal even with SA_RESTART (signal(7)). The Go runtime sends SIGURG for preemption, so Protect could fail with "interrupted system call". WriteMsg/ReadMsg now retry on EINTR. Resending is safe because a SOCK_SEQPACKET message is sent whole or not at all.
  3. Fake daemon fd reuse (test harness). stop() closed sockets while the fake's goroutines could still use them. A stale goroutine then read from the descriptor number reused by the next test's connection and took the session's protect request. stop() now shuts the sockets down, waits for the goroutines, then closes them.

Verification (host, unit only):

  • go test ./internal/guard -run TestSessionLapseIsPermanent -count=1000 -cpu 1,2,4: pass
  • the same in 6 parallel processes: all pass
  • go test -race -count=3 ./internal/guard: pass
  • CGO_ENABLED=0 go build ./..., go vet ./..., gofmt -l ., go test ./...: clean

cotty doctor summary

Before, the summary printed only the layer 2 level (confined) even with cotty-guard running. It now prints that level, labelled as layer 2, and adds a line for new sessions:

  • strong available for new sessions (cotty-guard running), with a note that sessions created before the guard started stay at their level
  • confined (cotty-guard not running) / (cotty-guard not answering)
  • weak when the cotty binary can be replaced by the user, unavailable without user namespaces

--json gains sessions and sessions_note; assurance keeps its meaning. A unit test covers sessionsSummary, and docs/confinement.md describes the new line.

🤖 Generated with Claude Code

edimarlnx and others added 3 commits November 4, 2026 19:04
…anent

The test read the daemon's protect count right after seeing the lapse,
but check marks the lapse before it sends the new protect request. Wait
for the request and for a later confirmed check instead of sleeping.

Stressing it also showed Protect failing with "interrupted system call":
the client socket has SO_RCVTIMEO/SO_SNDTIMEO, so recvmsg and sendmsg
return EINTR on a signal even with SA_RESTART, and the Go runtime sends
SIGURG to preempt goroutines. WriteMsg and ReadMsg now retry on EINTR
(a SOCK_SEQPACKET message is sent whole or not at all).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The summary printed only the layer 2 level ("confined") even with
cotty-guard running. It now also names the level a session created now
can reach (strong with the guard running, confined without it, weak with
a replaceable binary) and notes that sessions created before the guard
started keep their level. --json gains sessions and sessions_note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fake daemon in the session tests closed its sockets while its accept
and connection goroutines could still be about to use them. A goroutine
of a stopped fake then read from the descriptor number the next test's
connection reused, took the session's protect request and counted it on
the old fake: TestSessionLapseIsPermanent timed out waiting for it. stop
now shuts the sockets down, waits for the goroutines and only then
closes the descriptors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@edimarlnx
edimarlnx merged commit 6ef7747 into main Nov 4, 2026
1 check passed
@edimarlnx
edimarlnx deleted the fix-lapse-test-race branch November 4, 2026 22:51
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.

1 participant