Skip to content

Commit

Permalink
try to fix stream close
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Aug 22, 2022
1 parent 1592141 commit 0b38a8e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (c *Client) openStreamWithReconnect() (quic.Connection, quic.Stream, error)
stream, err := c.quicSession.OpenStream()
if err == nil {
// All good
return c.quicSession, &wrappedQUICStream{stream}, nil
return c.quicSession, &wrappedQUICStream{true, stream}, nil
}
// Something is wrong
if nErr, ok := err.(net.Error); ok && nErr.Temporary() {
Expand All @@ -180,7 +180,7 @@ func (c *Client) openStreamWithReconnect() (quic.Connection, quic.Stream, error)
}
// We are not going to try again even if it still fails the second time
stream, err = c.quicSession.OpenStream()
return c.quicSession, &wrappedQUICStream{stream}, err
return c.quicSession, &wrappedQUICStream{true, stream}, err
}

func (c *Client) DialTCP(addr string) (net.Conn, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/server_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *serverClient) Run() error {
c.ConnGauge.Inc()
}
go func() {
stream := &wrappedQUICStream{stream}
stream := &wrappedQUICStream{false, stream}
c.handleStream(stream)
_ = stream.Close()
if c.ConnGauge != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/core/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
// Handle stream close properly
// Ref: https://github.com/libp2p/go-libp2p-quic-transport/blob/master/stream.go
type wrappedQUICStream struct {
Stream quic.Stream
isClient bool
Stream quic.Stream
}

func (s *wrappedQUICStream) StreamID() quic.StreamID {
Expand All @@ -34,6 +35,9 @@ func (s *wrappedQUICStream) Write(p []byte) (n int, err error) {
}

func (s *wrappedQUICStream) Close() error {
if s.isClient {
s.Stream.CancelWrite(0)
}
s.Stream.CancelRead(0)
return s.Stream.Close()
}
Expand Down

0 comments on commit 0b38a8e

Please sign in to comment.