Skip to content

Commit 3c53bf6

Browse files
committed
make yamux close with error non blocking
1 parent 0e1eb18 commit 3c53bf6

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ require (
3232
github.com/libp2p/go-nat v0.2.0
3333
github.com/libp2p/go-netroute v0.2.1
3434
github.com/libp2p/go-reuseport v0.4.0
35-
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241120100319-39abe7ed206a
35+
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241121144427-4ad80596076b
3636
github.com/libp2p/zeroconf/v2 v2.2.0
3737
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd
3838
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9t
194194
github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ=
195195
github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQscQm2s=
196196
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
197-
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241120100319-39abe7ed206a h1:zc7jPWFFQibZbACDyQdEAWg7yG/fjx5Jmg6djtpjKog=
198-
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241120100319-39abe7ed206a/go.mod h1:PGP+3py2ZWDKABvqstBZtMnixEHNC7U/odnGylzur5o=
197+
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241121144427-4ad80596076b h1:HWXTC6mwQRnPIgm0rgMAOIi/jebt/5AOa81EfU/fPPE=
198+
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241121144427-4ad80596076b/go.mod h1:PGP+3py2ZWDKABvqstBZtMnixEHNC7U/odnGylzur5o=
199199
github.com/libp2p/zeroconf/v2 v2.2.0 h1:Cup06Jv6u81HLhIj1KasuNM/RHHrJ8T7wOTS4+Tv53Q=
200200
github.com/libp2p/zeroconf/v2 v2.2.0/go.mod h1:fuJqLnUwZTshS3U/bMRJ3+ow/v9oid1n0DmyYyNO1Xs=
201201
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=

p2p/muxer/yamux/conn.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (c *conn) Close() error {
2323
return c.yamux().Close()
2424
}
2525

26-
func (c *conn) CloseWithError(errCode network.ConnErrorCode) error {
27-
return c.yamux().CloseWithError(uint32(errCode))
26+
func (c *conn) CloseWithErrorChan(errCode network.ConnErrorCode) chan error {
27+
return c.yamux().CloseWithErrorChan(uint32(errCode))
2828
}
2929

3030
// IsClosed checks if yamux.Session is in closed state.
@@ -36,7 +36,7 @@ func (c *conn) IsClosed() bool {
3636
func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) {
3737
s, err := c.yamux().OpenStream(ctx)
3838
if err != nil {
39-
return nil, parseResetError(err)
39+
return nil, err
4040
}
4141

4242
return (*stream)(s), nil
@@ -45,7 +45,7 @@ func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) {
4545
// AcceptStream accepts a stream opened by the other side.
4646
func (c *conn) AcceptStream() (network.MuxedStream, error) {
4747
s, err := c.yamux().AcceptStream()
48-
return (*stream)(s), parseResetError(err)
48+
return (*stream)(s), err
4949
}
5050

5151
func (c *conn) yamux() *yamux.Session {

p2p/net/upgrader/conn.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@ func (t *transportConn) ConnState() network.ConnectionState {
6565
}
6666

6767
func (t *transportConn) CloseWithError(errCode network.ConnErrorCode) error {
68-
defer t.scope.Done()
69-
if ce, ok := t.MuxedConn.(network.CloseWithErrorer); ok {
70-
return ce.CloseWithError(errCode)
68+
if ce, ok := t.MuxedConn.(interface {
69+
CloseWithErrorChan(errCode network.ConnErrorCode) chan error
70+
}); ok {
71+
err := t.scope.ReserveMemory(10_000, network.ReservationPriorityMedium)
72+
if err != nil {
73+
return t.Close()
74+
}
75+
errCh := ce.CloseWithErrorChan(errCode)
76+
go func() {
77+
defer t.scope.Done()
78+
<-errCh
79+
}()
80+
return nil
7181
}
7282
return t.Close()
7383
}

test-plans/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require (
4646
github.com/libp2p/go-nat v0.2.0 // indirect
4747
github.com/libp2p/go-netroute v0.2.1 // indirect
4848
github.com/libp2p/go-reuseport v0.4.0 // indirect
49-
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241120100319-39abe7ed206a // indirect
49+
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241121144427-4ad80596076b // indirect
5050
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
5151
github.com/mattn/go-isatty v0.0.20 // indirect
5252
github.com/miekg/dns v1.1.62 // indirect

test-plans/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9t
150150
github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ=
151151
github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQscQm2s=
152152
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
153-
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241120100319-39abe7ed206a h1:zc7jPWFFQibZbACDyQdEAWg7yG/fjx5Jmg6djtpjKog=
154-
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241120100319-39abe7ed206a/go.mod h1:PGP+3py2ZWDKABvqstBZtMnixEHNC7U/odnGylzur5o=
153+
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241121144427-4ad80596076b h1:HWXTC6mwQRnPIgm0rgMAOIi/jebt/5AOa81EfU/fPPE=
154+
github.com/libp2p/go-yamux/v4 v4.0.2-0.20241121144427-4ad80596076b/go.mod h1:PGP+3py2ZWDKABvqstBZtMnixEHNC7U/odnGylzur5o=
155155
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
156156
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
157157
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd h1:br0buuQ854V8u83wA0rVZ8ttrq5CpaPZdvrK0LP2lOk=

0 commit comments

Comments
 (0)