mirror of
https://github.com/libp2p/go-libp2p.git
synced 2026-04-23 00:27:05 +08:00
fix(basic_host): set read deadline before multistream Close to prevent blocking
streamWrapper.Close() can block indefinitely when the remote peer is slow or unresponsive during the multistream-select handshake completion. The lazy multistream protocol negotiation defers reading the handshake response until Close() is called. If the remote peer doesn't respond, the read blocks forever, causing goroutine leaks. This is particularly problematic for bitswap servers where taskWorkers can get stuck trying to close streams after sending blocks. The fix sets a read deadline (using DefaultNegotiationTimeout) before calling the multistream Close(), ensuring the operation will time out rather than block indefinitely. Related: https://github.com/multiformats/go-multistream/issues/47 Related: https://github.com/multiformats/go-multistream/pull/48
This commit is contained in:
committed by
Marco Munizaga
parent
479b24baab
commit
bcc2bf1866
@@ -683,6 +683,11 @@ func (s *streamWrapper) Write(b []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (s *streamWrapper) Close() error {
|
||||
// Set a read deadline to prevent Close() from blocking indefinitely
|
||||
// waiting for the multistream-select handshake to complete.
|
||||
// This can happen when the remote peer is slow or unresponsive.
|
||||
// See: https://github.com/multiformats/go-multistream/issues/47
|
||||
_ = s.Stream.SetReadDeadline(time.Now().Add(DefaultNegotiationTimeout))
|
||||
return s.rw.Close()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user