@@ -568,12 +568,30 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error
568
568
// create HTTP/3 server if not done already
569
569
if s .h3server == nil {
570
570
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
+ }),
572
587
TLSConfig : tlsCfg ,
573
588
MaxHeaderBytes : s .MaxHeaderBytes ,
574
589
// TODO: remove this config when draft versions are no longer supported (we have no need to support drafts)
575
590
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 )
577
595
},
578
596
}
579
597
}
@@ -992,6 +1010,10 @@ const (
992
1010
// For referencing underlying net.Conn
993
1011
ConnCtxKey caddy.CtxKey = "conn"
994
1012
1013
+ // For referencing underlying quic.Connection
1014
+ // TODO: export if needed later
1015
+ quicConnCtxKey caddy.CtxKey = "quic_conn"
1016
+
995
1017
// For tracking whether the client is a trusted proxy
996
1018
TrustedProxyVarKey string = "trusted_proxy"
997
1019
0 commit comments