Skip to content

Replace mustDiscard panic with error return (#1442)#2150

Open
Nicolas0315 wants to merge 1 commit intovalyala:masterfrom
Nicolas0315:fix/replace-mustDiscard-panic-with-error
Open

Replace mustDiscard panic with error return (#1442)#2150
Nicolas0315 wants to merge 1 commit intovalyala:masterfrom
Nicolas0315:fix/replace-mustDiscard-panic-with-error

Conversation

@Nicolas0315
Copy link
Copy Markdown

Summary

Replace mustDiscard() which panics on bufio.Reader.Discard() failure with discardHeader() that returns an error, preventing server crashes under high concurrency.

Problem

Under high concurrency, mustDiscard() in header.go panics when a connection times out during header reading:

panic: bufio.Reader.Discard(517) failed: read tcp4 ...: i/o timeout

This panic occurs in the worker goroutine and cannot be recovered by any middleware (RouterPanic, Recovery, etc.), causing the entire server to crash.

Fix

  • Rename mustDiscard() to discardHeader() and return an error instead of panicking
  • Update all three call sites in header.go to propagate the error
  • All call sites already return error, so the change is straightforward

Testing

go test -run TestResponseHeader -count=1  # PASS
go test -run TestRequestHeader -count=1   # PASS

Fixes #1442

mustDiscard() panics when bufio.Reader.Discard() fails, which
crashes the entire server under high concurrency when connections
time out during header reading. This panic cannot be recovered by
any middleware since it occurs in the worker goroutine.

Replace mustDiscard() with discardHeader() that returns an error,
and propagate the error from all three call sites in header.go.
This allows the server to gracefully handle connection timeouts
instead of crashing.

Fixes valyala#1442
@erikdubbelboer
Copy link
Copy Markdown
Collaborator

erikdubbelboer commented Mar 2, 2026

This looks like an LLM finding a problem that doesn't exist. Do you have an example test case that triggers this bug?

@gaby
Copy link
Copy Markdown
Contributor

gaby commented Mar 7, 2026

@erikdubbelboer still a good idea to avoid calling panic in such a hot path.

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.

mustDiscard() panics and the whole server crashes

3 participants