Log session_id on engine session close - #293
Conversation
"Session closed" only logged session.logger's bound "session" field, which is the user's short ID (not this session's own) whenever authenticated - deliberately shared across every reconnect for that user so log lines group together by grep. Without an explicit session_id (unlike "Session established", which already logs one), two rapid reconnects for the same user produced two indistinguishable "Session closed" lines, which read as a session-eviction/map-corruption bug in a live triage when the actual cause was simply frequent reconnects from a duplicate-client bug elsewhere. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014CUkUYCxTRRRrvVWSGS5dj
There was a problem hiding this comment.
Pull request overview
This PR improves observability for WebSocket engine sessions by including an explicit session_id field in the "Session closed" log line, matching the existing "Session established" logging and making rapid reconnect/close sequences distinguishable in production logs.
Changes:
- Add
zap.String("session_id", session.ID)to the"Session closed"log entry inManager.unregisterSession. - Add an explanatory comment clarifying why
session.logger’s bound"session"field can’t be relied on to uniquely identify a specific session instance.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
leifj
left a comment
There was a problem hiding this comment.
This needs a config gate and should only be logged in debug-mode to avoid leaking privacy sensitive data



Summary
internal/engine/session.goclosely:registerSession/unregisterSession's map handling is correct (thecurrent == sessionpointer-equality guard inunregisterSessionalready prevents an evicted connection's cleanup from deleting the replacement session's entry). The real defect is purely in logging:session.logger's bound"session"field is set to the user's short ID (not the session's own) whenever authenticated, specifically so log lines from the same user's repeated reconnects grep together - but this means two rapid reconnects for one user produce two"Session closed"lines with no way to tell which session actually closed, unlike"Session established"which already logs an explicitsession_id.session_idfield to the"Session closed"log line, so a real reconnect storm (driven by a duplicate-client bug on the SDK side, tracked separately) can actually be distinguished from a server-side eviction bug in logs going forward.Test plan
GOWORK=off go build ./internal/engine/...GOWORK=off go test ./internal/engine/...- passing🤖 Generated with Claude Code
https://claude.ai/code/session_014CUkUYCxTRRRrvVWSGS5dj