entwine: fix connect/drain/shutdown review findings + backfill tests
Commit
Addresses the verified findings from the xhigh code review of the thin first slice. Grouped by file:
shutdown.go
- Recover panics in ShutdownGroup.Go and log them with a stack instead of crashing the process, restoring the per-subsystem containment the origin runSubsystems provided.
- Detect a loop that returns while the context is still live (dead-man) and log it at ERROR.
- Guard Go against Shutdown: register wg.Add under the same lock Shutdown takes before wg.Wait, eliminating the "Add called concurrently with Wait" panic; refuse (and log) loops registered after shutdown begins.
- Drain registered connections concurrently (one goroutine each) so worst-case shutdown is one backstop, not the sum across connections.
- Size each connection's drain backstop from its own nats.DrainTimeout (honoring WithDrainTimeout) plus publish-flush headroom, and route drain diagnostics through the group's configured logger.
drain.go
- Wait for CLOSED via StatusChanged(CLOSED) instead of hijacking the connection's ClosedHandler, so a handler installed by Connect or the caller is preserved and concurrent Drains no longer clobber each other's signal.
- Take a *slog.Logger so diagnostics no longer bypass the caller's logger.
- Document timeout as the total time-to-CLOSED backstop (must exceed nats.DrainTimeout by the ~5s publish flush).
connect.go
- Make the initial dial fail-fast by default (RetryOnFailedConnect off) so a misconfigured connection crash-loops visibly instead of booting into a silently-reconnecting state reported as "connected"; add WithRetryOnFailedConnect to opt back in, and log honestly when not yet connected.
- Make mTLS fully rotation-aware: verify the server against a CA pool re-read from disk per handshake (VerifyConnection) rather than a pool pinned once at startup.
- Log lifecycle events through a cancellation-detached context so a context-aware handler does not drop mid-life disconnect/reconnect records, and label every lifecycle/connected line with the connection name.
README.md
- Fix the canonical fetch-loop example to observe ctx cancellation (so it exits before its connection is drained) and back off on real errors instead of busy-spinning.
tests
- Add an embedded nats-server (test-only dep) and restore Drain's blocking-wait coverage: waits for CLOSED, no-op on closed conn, preserves the caller ClosedHandler, concurrent Drains both complete, logs to the provided logger.
- Add shutdown supervision tests: panic recovery, dead-man detection, refusal after shutdown, concurrent Go/Shutdown (race), per-connection drain backstop, drains all connections.
- Add connect tests: fail-fast on unreachable server, retry-on-failed-connect opt-in, connection-name logging, and CA rotation via VerifyConnection.
Co-Authored-By: Claude Fable 5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01Uaw7J8gEb6Rf2fPBWWYrWm