Skip to content

fix: log expected websocket session terminations at Warn level in Client.Run - #175

Open
nudgebee-dev[bot] wants to merge 1 commit into
mainfrom
fix/fix-log-expected-websocket-session-termi-1793071803
Open

nudgebee-dev[bot] wants to merge 1 commit into
mainfrom
fix/fix-log-expected-websocket-session-termi-1793071803

Conversation

@nudgebee-dev

@nudgebee-dev nudgebee-dev Bot commented Oct 27, 2026

Copy link
Copy Markdown

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

  • 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

Click to expand the original error that triggered this fix
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)
}

How Has This Been Tested?

  • Unit tests
  • Manual testing

Checklist

  • CLA signed (the CLA bot will prompt on your first PR)
  • 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 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
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…

…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
nudgebee-dev Bot force-pushed the fix/fix-log-expected-websocket-session-termi-1793071803 branch from 2f62dd8 to 023a529 Compare October 27, 2026 03:37
@nudgebee-dev

nudgebee-dev Bot commented Oct 27, 2026

Copy link
Copy Markdown
Author

Nudgebee Automated Followup

Bumped 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: 023a5296cf54

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