APIServer: Cancel forked context after handler returns (#100504)

We currently cancel the context when the adapter function is done. We should wait for the entire handler we're wrapping
to finish before cancelling our context.
This commit is contained in:
Mariell Hoversholm
2025-02-13 08:54:58 +01:00
committed by GitHub
parent 2b2b19478a
commit 2dee9ccbbc

View File

@ -37,11 +37,12 @@ func WrapHandler(handler http.Handler) func(req *http.Request) (*http.Response,
if err != nil {
return nil, err
}
defer cancel()
// The cancel happens in the goroutine we spawn, so as to not cancel it too early.
req = req.WithContext(ctx) // returns a shallow copy, so we can't do it as part of the adapter.
w := NewAdapter(req)
go func() {
defer cancel()
handler.ServeHTTP(w, req)
if err := w.CloseWriter(); err != nil {
klog.Errorf("error closing writer: %v", err)