Repository navigation
fix: log expected websocket session terminations at Warn level in Client.Run - #175
Open
nudgebee-dev[bot] wants to merge 1 commit into
Open
nudgebee-dev[bot] wants to merge 1 commit into
nudgebee-dev[bot] wants to merge 1 commit into
Conversation
…ent.Run
# Description
In `pkg/ws/client.go`, `Client.Run` unconditionally logs all errors returned by `c.connectAndServe(ctx)` at `Error` level using `c.logger.Error("websocket session ended", "err", err)`. During normal client shutdown or expected network reconnect cycles (such as normal disconnects, abnormal close 1006, `net.ErrClosed`, `io.EOF`, or context cancellation), this causes false error alerts in log monitors.
To resolve this, this change:
1. Adds helper function `isExpectedDisconnect(err error) bool` to check if an error matches `context.Canceled`, `net.ErrClosed`, `io.EOF`, or WebSocket close codes `CloseNormalClosure`, `CloseGoingAway`, or `CloseAbnormalClosure` (1006) using `errors.Is` and `errors.As`.
2. Updates `Client.Run` to log expected disconnections at `Warn` level (`c.logger.Warn("websocket session ended", "err", err)`) while continuing to log unexpected or unrecoverable errors at `Error` level.
## 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>
```
In `pkg/ws/client.go`, modify `Client.Run` to log session terminations at `Info` or `Warn` level rather than `Error` when the error is a normal disconnect, abnormal close (1006), `net.ErrClosed`, `io.EOF`, or context cancellation. This prevents expected reconnect cycles from tripping log error alerts.
Files located during the investigation: pkg/ws/client.go
The investigation already read this code:
**Code examined**
- `pkg/ws/client.go`:120-123 — Unconditional error-level logging in the auto-reconnection loop
```
err := c.connectAndServe(ctx)
if err != nil {
c.logger.Error("websocket session ended", "err", err)
}
```
- `pkg/ws/client.go`:218-224 — Closing the connection on context cancellation triggers a read error on the blocking ReadMessage call
```
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
_ = conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
_ = conn.Close()
}()
```
- `pkg/ws/client.go`:269-275 — Only CloseNormalClosure and CloseGoingAway are ignored; all other disconnections return an error
```
_, 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=e899cb94-8e78-46b2-b90f-584f609be0d7)**
*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
go: downloading github.com/spf13/viper v1.21.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/ClickHouse/clickhouse-go/v2 v2.48.0
go: downloading github.com/go-sql-driver/mysql v1.10.0
go: downloading github.com/jackc/pgx/v5 v5.10.0
go: downloading github.com/microsoft/go-mssqldb v1.10.0
go: downloading github.com/sijms/go-ora/v2 v2.9.0
go: downloading github.com/go-ldap/ldap/v3 v3.4.14
go: downloading golang.org/x/crypto v0.57.0
go: downloading golang.org/x/time v0.15.0
go: downloading github.com/IBM/sarama v1.60.1
go: downloading github.com/xdg-go/scram v1.2.0
go: downloading go.mongodb.org/mongo-driver/v2 v2.8.0
go: downloading github.com/redis/go-redis/v9 v9.22.0
go: downloading github.com/pkg/sftp v1.13.11
go: downloading cloud.google.com/go/auth v0.23.1
go: downloading cloud.google.com/go/secretmanager v1.21.0
go: downloading github.com/Azure/azure-sdk-for-go/sdk/azcore v1.23.0
go: downloading github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.14.0
go: downloading github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.5.0
go: downloading github.com/aws/aws-sdk-go-v2/config v1.32.37
go: downloading github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.44.6
go: downloading github.com/aws/aws-sdk-go-v2 v1.43.6
go: downloading github.com/google/uuid v1.6.0
go: downloading google.golang.org/api v0.293.0
go: downloading github.com/fsnotify/fsnotify v1.9.0
go: downloading github.com/go-viper/mapstructur…
```
</details>
nudgebee-dev
Bot
force-pushed
the
fix/fix-log-expected-websocket-session-termi-1793071803
branch
from
October 27, 2026 03:37
2f62dd8 to
023a529
Compare
Author
Nudgebee Automated FollowupBumped Go directive in go.mod to 1.26.10 to resolve standard library vulnerability GO-2026-6809 in the CI vuln check. Added unit tests in pkg/ws/client_test.go covering isExpectedDisconnect. Amended the changes into the bot commit and pushed to fix/fix-log-expected-websocket-session-termi-1793071803 with force-with-lease. Commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
In
pkg/ws/client.go,Client.Rununconditionally logs all errors returned byc.connectAndServe(ctx)atErrorlevel usingc.logger.Error("websocket session ended", "err", err). During normal client shutdown or expected network reconnect cycles (such as normal disconnects, abnormal close 1006,net.ErrClosed,io.EOF, or context cancellation), this causes false error alerts in log monitors.To resolve this, this change:
isExpectedDisconnect(err error) boolto check if an error matchescontext.Canceled,net.ErrClosed,io.EOF, or WebSocket close codesCloseNormalClosure,CloseGoingAway, orCloseAbnormalClosure(1006) usingerrors.Isanderrors.As.Client.Runto log expected disconnections atWarnlevel (c.logger.Warn("websocket session ended", "err", err)) while continuing to log unexpected or unrecoverable errors atErrorlevel.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)
}
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
_ = conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
_ = conn.Close()
}()
_, 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