Skip to content

What causes "response.Write on hijacked connection" when using CompressHandler? #589

@andig

Description

@andig

I realize this is old (#212, #494). I understand

The warnings indicate that the application is calling http.ResponseWriter methods after the call to Upgrade.

Yet I'm failing to identify the cause. I have a web socket on /ws and a router HandleFunc on / for the index. There is also

router.Use(handlers.CompressHandler)

The CompressHandler causes the problem and gives the following stack trace:

runtime/debug.Stack(0x155047c, 0x108e8d0, 0xc0002307e0)
        /usr/local/Cellar/go/1.14.1/libexec/src/runtime/debug/stack.go:24 +0xab
runtime/debug.PrintStack()
        /usr/local/Cellar/go/1.14.1/libexec/src/runtime/debug/stack.go:16 +0x34
github.com/andig/evcc/api.debugLogger.Write(0x0, 0x0, 0xc0002307e0, 0x5d, 0x60, 0xc0002cf2d8, 0x108af29, 0xc0002307e0)
        /Users/andig/htdocs/evcc/api/log.go:70 +0x12c
log.(*Logger).Output(0xc0001a4ff0, 0x2, 0xc000230720, 0x5c, 0x0, 0x0)
        /usr/local/Cellar/go/1.14.1/libexec/src/log/log.go:181 +0x39e
log.(*Logger).Printf(0xc0001a4ff0, 0x1b73c74, 0x3b, 0xc0002cf518, 0x3, 0x3)
        /usr/local/Cellar/go/1.14.1/libexec/src/log/log.go:188 +0x90
net/http.(*Server).logf(0xc0001409a0, 0x1b73c74, 0x3b, 0xc0002cf518, 0x3, 0x3)
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:3059 +0xa6
net/http.(*response).write(0xc0004ec9a0, 0xa, 0xc0002d4089, 0xa, 0xa, 0x0, 0x0, 0x1156659, 0xc0002cf640, 0xc0000200f0)
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:1559 +0x28c
net/http.(*response).Write(0xc0004ec9a0, 0xc0002d4089, 0xa, 0xa, 0xe7791f700, 0x243b640, 0xc000020000)
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:1547 +0x71
compress/gzip.(*Writer).Write(0xc0002d4000, 0x0, 0x0, 0x0, 0x2f707d0, 0x0, 0xc0000200f0)
        /usr/local/Cellar/go/1.14.1/libexec/src/compress/gzip/gzip.go:168 +0x518
compress/gzip.(*Writer).Close(0xc0002d4000, 0xc0002d4000, 0x0)
        /usr/local/Cellar/go/1.14.1/libexec/src/compress/gzip/gzip.go:237 +0x4b3
github.com/gorilla/handlers.CompressHandlerLevel.func1(0x1ec5aa0, 0xc0004ec9a0, 0xc00017e300)
        /Users/andig/go/pkg/mod/github.com/gorilla/handlers@v1.4.2/compress.go:149 +0x1cd
net/http.HandlerFunc.ServeHTTP(0xc0002ea0c0, 0x1ec5aa0, 0xc0004ec9a0, 0xc00017e300)
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:2012 +0x52
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0001c20c0, 0x1ec5aa0, 0xc0004ec9a0, 0xc00017e100)
        /Users/andig/go/pkg/mod/github.com/gorilla/mux@v1.7.4/mux.go:210 +0x13f
net/http.serverHandler.ServeHTTP(0xc0001409a0, 0x1ec5aa0, 0xc0004ec9a0, 0xc00017e000)
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:2807 +0xcf
net/http.(*conn).serve(0xc0004bab40, 0x1ec78e0, 0xc00011c180)
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:1895 +0x838
created by net/http.(*Server).Serve
        /usr/local/Cellar/go/1.14.1/libexec/src/net/http/server.go:2933 +0x5b7
http: response.Write on hijacked connection from compress/gzip.(*Writer).Write (gzip.go:168)

Versions

Go version: go version go1.14.1 darwin/amd64

The websocket conn is closed when the client goes away. The http part is only dealing with HandlerFuncs:

return func(w http.ResponseWriter, r *http.Request) {...}

...but not with connections. I'm never writing to a connection explicitly and only write bodies after writing headers. I'm totally stumped where or how I might be writing to a hijacked connection?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions