Skip to content

middleware: handle compressor encoder failure - #1192

Open
lowgame wants to merge 1 commit into
go-chi:masterfrom
lowgame:fix-compressor-encoder-failure-11cbcb45
Open

lowgame wants to merge 1 commit into
go-chi:masterfrom
lowgame:fix-compressor-encoder-failure-11cbcb45

Conversation

@lowgame

@lowgame lowgame commented Sep 27, 2026

Copy link
Copy Markdown

Summary

  • treat a custom compressor encoder returning nil as an unavailable encoding
  • avoid panicking when a pooled encoder cannot be constructed
  • add regression coverage for both direct and pooled encoder failures

Reproduction

Compressor.SetEncoder documents that an encoder function may return nil on failure. Before this change, the direct path still selected that encoding and emitted its Content-Encoding value, while the pooled path could panic on an unchecked type assertion when sync.Pool.New returned nil.

The added tests reproduce both cases with custom encoders that fail deterministically.

Root cause

selectEncoder assumed every configured encoder invocation returned a usable writer. That contradicted the documented EncoderFunc failure contract.

Solution

Validate encoder results in both selection paths. If construction fails, continue negotiating another accepted encoder; when none succeeds, preserve the identity response without a Content-Encoding header.

Tests

  • GOTOOLCHAIN=go1.25.1 go test ./middleware -run '^TestCompressor.*EncoderFailure$' -count=20
  • GOTOOLCHAIN=go1.25.1 go test -race ./middleware -run '^TestCompressor.*EncoderFailure$' -count=3
  • GOTOOLCHAIN=go1.25.1 go vet ./middleware
  • GOTOOLCHAIN=go1.25.1 make test

This branch has not been deployed

No deployments
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