Skip to content

Commit 924010c

Browse files
authored
caddyhttp: close quic connections when server closes (#6202)
* close quic connections when server closes * fix lint * add comment about CloseGracefully
1 parent 74949fb commit 924010c

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

modules/caddyhttp/server.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,30 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error
568568
// create HTTP/3 server if not done already
569569
if s.h3server == nil {
570570
s.h3server = &http3.Server{
571-
Handler: s,
571+
// Currently when closing a http3.Server, only listeners are closed. But caddy reuses these listeners
572+
// if possible, requests are still read and handled by the old handler. Close these connections manually.
573+
// see issue: https://github.com/caddyserver/caddy/issues/6195
574+
// Will interrupt ongoing requests.
575+
// TODO: remove the handler wrap after http3.Server.CloseGracefully is implemented, see App.Stop
576+
Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
577+
select {
578+
case <-s.ctx.Done():
579+
if quicConn, ok := request.Context().Value(quicConnCtxKey).(quic.Connection); ok {
580+
//nolint:errcheck
581+
quicConn.CloseWithError(quic.ApplicationErrorCode(http3.ErrCodeRequestRejected), "")
582+
}
583+
default:
584+
s.ServeHTTP(writer, request)
585+
}
586+
}),
572587
TLSConfig: tlsCfg,
573588
MaxHeaderBytes: s.MaxHeaderBytes,
574589
// TODO: remove this config when draft versions are no longer supported (we have no need to support drafts)
575590
QuicConfig: &quic.Config{
576-
Versions: []quic.VersionNumber{quic.Version1, quic.Version2},
591+
Versions: []quic.Version{quic.Version1, quic.Version2},
592+
},
593+
ConnContext: func(ctx context.Context, c quic.Connection) context.Context {
594+
return context.WithValue(ctx, quicConnCtxKey, c)
577595
},
578596
}
579597
}
@@ -992,6 +1010,10 @@ const (
9921010
// For referencing underlying net.Conn
9931011
ConnCtxKey caddy.CtxKey = "conn"
9941012

1013+
// For referencing underlying quic.Connection
1014+
// TODO: export if needed later
1015+
quicConnCtxKey caddy.CtxKey = "quic_conn"
1016+
9951017
// For tracking whether the client is a trusted proxy
9961018
TrustedProxyVarKey string = "trusted_proxy"
9971019

0 commit comments

Comments
 (0)