From 288868c0d90e2d72a124da26caa14c77d35dbbdd Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 20 Nov 2024 16:51:55 -0800 Subject: [PATCH] fix(libp2phttp): Return ErrServerClosed on Close (#3050) --- p2p/http/libp2phttp.go | 1 + p2p/http/libp2phttp_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/p2p/http/libp2phttp.go b/p2p/http/libp2phttp.go index d2a544d224..4dad47dd43 100644 --- a/p2p/http/libp2phttp.go +++ b/p2p/http/libp2phttp.go @@ -359,6 +359,7 @@ func (h *Host) Serve() error { expectedErrCount := len(h.httpTransport.listeners) select { case <-h.httpTransport.closeListeners: + err = http.ErrServerClosed case err = <-errCh: expectedErrCount-- } diff --git a/p2p/http/libp2phttp_test.go b/p2p/http/libp2phttp_test.go index fa8687e308..ae3285b9a3 100644 --- a/p2p/http/libp2phttp_test.go +++ b/p2p/http/libp2phttp_test.go @@ -30,6 +30,7 @@ import ( httpping "github.com/libp2p/go-libp2p/p2p/http/ping" libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic" ma "github.com/multiformats/go-multiaddr" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -996,3 +997,20 @@ func TestImpliedHostIsSet(t *testing.T) { } } + +func TestErrServerClosed(t *testing.T) { + server := libp2phttp.Host{ + InsecureAllowHTTP: true, + ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")}, + } + + done := make(chan struct{}) + go func() { + err := server.Serve() + assert.Equal(t, http.ErrServerClosed, err) + close(done) + }() + + server.Close() + <-done +}