Repository navigation
fix: classify websocket session termination errors to log routine network drops at Warn instead of Error - #173
Conversation
0920f30 to
0f632ad
Compare
Nudgebee Automated FollowupFixed the staticcheck linter failure S1008 in Commit: |
…work drops at Warn instead of Error
# Description
In `pkg/ws/client.go` within `Client.Run`, any error returned by `c.connectAndServe(ctx)` was unconditionally logged at `Error` level (`c.logger.Error("websocket session ended", "err", err)`). Routine network drops, dial failures, abnormal websocket closures, and temporary connection resets are expected during normal operation and are automatically handled by the client's exponential backoff reconnect loop. Logging these transient events at `Error` level causes excessive alert noise and increments error log metrics unnecessarily.
This change introduces error classification via `isRoutineDisconnect(err)`:
1. Routine network/dial issues (e.g. `net.Error`, connection resets/refusals, EOF, context cancellations, websocket close errors, and 5xx dial errors) are logged at `Warn` level (`c.logger.Warn("websocket session ended", "err", err)`).
2. Persistent or unrecoverable issues (such as client 4xx authentication/authorization failures on dial like 401/403) and unexpected errors remain logged at `Error` level.
3. Introduces a structured `dialHTTPError` type returned by `connectAndServe` to inspect HTTP response status codes on dial failures.
## Type of change
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Enhancement (non-breaking change which improves existing functionality)
- [ ] Refactor (non-breaking change which improves code structure)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation
- [ ] CI/CD
## Original Error/Logs
<details>
<summary>Click to expand the original error that triggered this fix</summary>
```
Classify errors returned by connectAndServe in Client.Run so that routine network read/dial drops log at Warn or Info, and only unrecoverable or persistent errors log at Error level.
Files located during the investigation: pkg/ws/client.go
The investigation already read this code:
**Code examined**
- `pkg/ws/client.go`:120-124 — Client reconnection loop unconditionally logging any non-nil connectAndServe error at Error level
```
err := c.connectAndServe(ctx)
if err != nil {
c.logger.Error("websocket session ended", "err", err)
}
```
- `pkg/ws/client.go`:148-159 — Dial errors returned from connectAndServe
```
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, c.relayURL, header)
if err != nil {
if resp != nil {
var body []byte
if resp.Body != nil {
body, _ = io.ReadAll(io.LimitReader(resp.Body, 1024))
resp.Body.Close() // nolint:errcheck
}
return fmt.Errorf("dial failed: %w (status=%d body=%q)", err, resp.StatusCode, body)
}
return fmt.Errorf("dial failed: %w", err)
}
```
- `pkg/ws/client.go`:269-275 — Read errors returned from readLoop unless closure is normal or going away
```
_, msg, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
return nil
}
return fmt.Errorf("read error: %w", err)
}
```
```
</details>
# How Has This Been Tested?
- [x] Unit tests
- [ ] Manual testing
# Checklist
- [ ] CLA signed (the CLA bot will prompt on your first PR)
- [x] `make validate` passes (fmt + lint + test)
- [ ] Docs updated if the wire shape, config surface, or proxy module behavior changed
---
🤖 **This PR was automatically generated by [NudgeBee](https://nudgebee.com) AI coding agent**
*Powered by AI-driven code analysis and automated fix generation*
🔍 **[View Original Investigation](https://dev.nudgebee.pollux.in/investigate?id=9c83bb94-6850-478d-b2d1-0d4453857fd0)**
*Click the link above to see the full investigation and analysis that led to this fix*
View Detailed **[Nubi Conversation](https://dev.nudgebee.pollux.in/ask-nudgebee?accountId=f13ddc45-07f3-42b4-bd0e-19606c852d18&session_id=event-f48d9d60d6537ce9)**
---
### ✅ Verification
Build verification passed for the affected modules.
<details><summary>Commands run</summary>
```
PASSED (cd . && go build ./...) exit=0
```
</details>
0f632ad to
4219456
Compare
Nudgebee Automated FollowupUpdated go.mod to specify Go 1.26.10 instead of 1.26.9, which resolves the CI govulncheck vulnerability failure (GO-2026-6809 in crypto/x509). Verified locally with govulncheck that 0 vulnerabilities are reported. Amended the changes into the bot commit and pushed with --force-with-lease to origin/fix/fix-classify-websocket-session-terminati-1792979518. Commit: |
Description
In
pkg/ws/client.gowithinClient.Run, any error returned byc.connectAndServe(ctx)was unconditionally logged atErrorlevel (c.logger.Error("websocket session ended", "err", err)). Routine network drops, dial failures, abnormal websocket closures, and temporary connection resets are expected during normal operation and are automatically handled by the client's exponential backoff reconnect loop. Logging these transient events atErrorlevel causes excessive alert noise and increments error log metrics unnecessarily.This change introduces error classification via
isRoutineDisconnect(err):net.Error, connection resets/refusals, EOF, context cancellations, websocket close errors, and 5xx dial errors) are logged atWarnlevel (c.logger.Warn("websocket session ended", "err", err)).Errorlevel.dialHTTPErrortype returned byconnectAndServeto inspect HTTP response status codes on dial failures.Type of change
Original Error/Logs
Click to expand the original error that triggered this fix
err := c.connectAndServe(ctx)
if err != nil {
c.logger.Error("websocket session ended", "err", err)
}
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, c.relayURL, header)
if err != nil {
if resp != nil {
var body []byte
if resp.Body != nil {
body, _ = io.ReadAll(io.LimitReader(resp.Body, 1024))
resp.Body.Close() // nolint:errcheck
}
return fmt.Errorf("dial failed: %w (status=%d body=%q)", err, resp.StatusCode, body)
}
return fmt.Errorf("dial failed: %w", err)
}
_, msg, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
return nil
}
return fmt.Errorf("read error: %w", err)
}
How Has This Been Tested?
Checklist
make validatepasses (fmt + lint + test)🤖 This PR was automatically generated by NudgeBee AI coding agent
Powered by AI-driven code analysis and automated fix generation
🔍 View Original Investigation
Click the link above to see the full investigation and analysis that led to this fix
View Detailed Nubi Conversation
✅ Verification
Build verification passed for the affected modules.
Commands run