Skip to content

Check whether request context is done before write - #19

Open
WindzCUHK wants to merge 3 commits into
AthenZ:masterfrom
falz-tino:checkcontextdone
Open

Check whether request context is done before write#19
WindzCUHK wants to merge 3 commits into
AthenZ:masterfrom
falz-tino:checkcontextdone

Conversation

@WindzCUHK

Copy link
Copy Markdown

This PR is to avoid a panic when writing response.

issue

When the HTTP connection is closed, the context of the request ends. Any following write action to the response writer will cause the program to panic.

solution

Check request context before the actual write.
The current solution is try to fix the problem in the simplest way. After the context checking, there may be a small chances that the context is closed before the writing action and causes panic again.

stack trace:

panic: Header called after Handler finished
goroutine 1366 [running]:
net/http.(*http2responseWriter).Header(0xc0003be038, 0xc0003c4140)
        /usr/local/go/src/net/http/h2_bundle.go:6158 +0x99
github.com/yahoo/k8s-athenz-webhook.writeJSON(0xc07c00, 0xc0004a4e10, 0xc05cc0, 0xc0003be038, 0xa14d80, 0xc0003c4140)
        /go/pkg/mod/github.com/yahoo/k8s-athenz-webhook@v0.0.0-20190725182459-949d9ed74720/http.go:26 +0x1cb
github.com/yahoo/k8s-athenz-webhook.(*authorizer).ServeHTTP(0xc000212280, 0xc05cc0, 0xc0003be038, 0xc0006cc600)
        /go/pkg/mod/github.com/yahoo/k8s-athenz-webhook@v0.0.0-20190725182459-949d9ed74720/authz.go:286 +0x47f
github.com/yahoo/k8s-athenz-webhook.wrapHandler.func2(0xc05cc0, 0xc0003be038, 0xc0006cc400)
        /go/pkg/mod/github.com/yahoo/k8s-athenz-webhook@v0.0.0-20190725182459-949d9ed74720/http.go:88 +0x14f
net/http.HandlerFunc.ServeHTTP(0xc00012c6c0, 0xc05cc0, 0xc0003be038, 0xc0006cc400)
        /usr/local/go/src/net/http/server.go:2007 +0x44
github.com/yahoojapan/garm/service.(*athenz).AthenzAuthorizer(0xc00014b0e0, 0xc05cc0, 0xc0003be038, 0xc0006cc400, 0xc0001fa1c0, 0xc05cc0)
        /go/src/github.com/yahoojapan/garm/service/athenz.go:88 +0x58
github.com/yahoojapan/garm/handler.(*handler).Authorize(0xc000289010, 0xc05cc0, 0xc0003be038, 0xc0006cc400, 0xc0003be001, 0xc000120800)
        /go/src/github.com/yahoojapan/garm/handler/handlers.go:59 +0x55
github.com/yahoojapan/garm/router.routing.func1.1(0xc0001250e0, 0xc000239120, 0xc05cc0, 0xc0003be038, 0xc0006cc300, 0xc07bc0, 0xc0006547e0)
        /go/src/github.com/yahoojapan/garm/router/router.go:81 +0xed
created by github.com/yahoojapan/garm/router.routing.func1
        /go/src/github.com/yahoojapan/garm/router/router.go:79 +0x491

similar issue: kubernetes/kubernetes#29001

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

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