Skip to content

fix: adjust server WriteTimeout for WebSocket compatibility#11

Merged
omattsson merged 2 commits intomainfrom
fix/websocket-write-timeout
Mar 14, 2026
Merged

fix: adjust server WriteTimeout for WebSocket compatibility#11
omattsson merged 2 commits intomainfrom
fix/websocket-write-timeout

Conversation

@omattsson
Copy link
Owner

Summary

The HTTP server's WriteTimeout was hardcoded to 10s by default. This caused WebSocket connections to be forcibly terminated by the server after 10 seconds of inactivity or mid-stream, making long-lived WebSocket connections impossible without manual configuration overrides.

Setting the default to 0 disables the write deadline entirely, which is the standard practice for servers that need to support WebSocket or streaming responses.

Changes

  • backend/internal/config/config.go — changed defaultWriteTimeout from 10s to 0; updated validation to reject only negative values (< 0 instead of <= 0); corrected doc comment on ServerConfig.WriteTimeout
  • backend/internal/config/config_test.go — updated default assertion; added two new test cases: zero value passes validation, negative value fails validation- backend/internal/config/config_test.go — updatCo- backend/internal/config/config_test.go — updated default
  • backend/api/main_integration_test.go — updated TestDatabaseInitialization to use time.Duration(0)

Testing

cd backend && go test ./... -v -short

Closes #4

The default WriteTimeout was 10s, which caused WebSocket connections to be
forcibly closed by the server after the timeout elapsed mid-stream. Setting
it to 0 disables the deadline entirely, allowing long-lived WebSocket
connections to remain open.

Validation is updated to reject only negative values (< 0) instead of
<= 0, so zero is now a valid, explicit configuration. Tests are updated
to assert the new default and cover both the zero-passes and
negative-fails cases.

Refs #4
Copilot AI review requested due to automatic review settings March 14, 2026 19:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts the backend HTTP server’s default WriteTimeout to support long-lived WebSocket connections by disabling the server-level write deadline while retaining per-write WebSocket deadlines.

Changes:

  • Set defaultWriteTimeout to 0 (disabled) and allow WriteTimeout == 0 in server config validation.
  • Update config tests to assert the new default and add validation cases for WriteTimeout being 0 and negative.
  • Update main tests/integration tests to expect WriteTimeout of 0.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
backend/internal/config/config.go Changes default WriteTimeout to 0, updates validation, and expands documentation comments.
backend/internal/config/config_test.go Updates default assertions and adds validation test cases for WriteTimeout.
backend/api/main_test.go Updates mocked config and server assertions to match new WriteTimeout default.
backend/api/main_integration_test.go Updates integration test config to use WriteTimeout: 0.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 122 to 126
assert.Empty(t, config.Server.Host)
assert.Equal(t, "8081", config.Server.Port)
assert.Equal(t, 10*time.Second, config.Server.ReadTimeout)
assert.Equal(t, 10*time.Second, config.Server.WriteTimeout)
assert.Equal(t, 30*time.Second, config.Server.ShutdownTimeout)
assert.Equal(t, time.Duration(0), config.Server.WriteTimeout) // disabled; per-write deadlines enforced in WebSocket write pump

Comment on lines +91 to +96
// WriteTimeout is 0 (disabled) at the server level. Per-write deadlines are
// enforced inside the WebSocket write pump (websocket/client.go), so a
// server-level write timeout must be disabled to prevent premature
// termination of idle WebSocket connections. Standard REST handlers complete
// synchronously with sub-millisecond response writes and are not a concern
// in practice; per-handler context deadlines can be applied if required.
@omattsson omattsson merged commit 73efe68 into main Mar 14, 2026
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.

Adjust server WriteTimeout for WebSocket compatibility

2 participants