Skip to content

fix: Use buffered reader in peer to fix ShortBuffer #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"tfexec",
"tfstate",
"unconvert",
"webrtc",
"xerrors",
"yamux"
]
Expand Down
21 changes: 19 additions & 2 deletions peer/channel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package peer

import (
"bufio"
"context"
"io"
"net"
Expand Down Expand Up @@ -78,7 +79,8 @@ type Channel struct {
dc *webrtc.DataChannel
// This field can be nil. It becomes set after the DataChannel
// has been opened and is detached.
rwc datachannel.ReadWriteCloser
rwc datachannel.ReadWriteCloser
reader io.Reader

closed chan struct{}
closeMutex sync.Mutex
Expand Down Expand Up @@ -130,6 +132,21 @@ func (c *Channel) init() {
_ = c.closeWithError(xerrors.Errorf("detach: %w", err))
return
}
// pion/webrtc will return an io.ErrShortBuffer when a read
// is triggerred with a buffer size less than the chunks written.
//
// This makes sense when considering UDP connections, because
// bufferring of data that has no transmit guarantees is likely
// to cause unexpected behavior.
//
// When ordered, this adds a bufio.Reader. This ensures additional
// data on TCP-like connections can be read in parts, while still
// being bufferred.
Comment on lines +142 to +144
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for documenting this, very helpful. Wouldn't have been clear to me why we need this otherwise 👍

if c.opts.Unordered {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooo, wonder if we have the same issue with wsnet 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was really confused why wsnet doesn't have this problem, but I think it's because we pipe using net.Dial for everything. So the problem exists, but it's hidden behind the TCP buffering there maybe?

ssh is a specific protocol in the agent, so we're exchanging much smaller payloads here, which made me notice this bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for context, I was getting weird short buffer errors (that I had no idea how to reproduce easily or diagnose) when I tried to remove the envagent proxying, so I wonder whether this would've fixed it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this almost certainly would have!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if we use unordered connections via WebRTC in the product today?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, we don't!

c.reader = c.rwc
} else {
c.reader = bufio.NewReader(c.rwc)
}
close(c.opened)
})

Expand Down Expand Up @@ -181,7 +198,7 @@ func (c *Channel) Read(bytes []byte) (int, error) {
}
}

bytesRead, err := c.rwc.Read(bytes)
bytesRead, err := c.reader.Read(bytes)
if err != nil {
if c.isClosed() {
return 0, c.closeError
Expand Down
21 changes: 21 additions & 0 deletions peer/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,27 @@ func TestConn(t *testing.T) {
_, err := client.Ping()
require.NoError(t, err)
})

t.Run("ShortBuffer", func(t *testing.T) {
t.Parallel()
client, server, _ := createPair(t)
exchange(client, server)
go func() {
channel, err := client.Dial(context.Background(), "test", nil)
require.NoError(t, err)
_, err = channel.Write([]byte{1, 2})
require.NoError(t, err)
}()
channel, err := server.Accept(context.Background())
require.NoError(t, err)
data := make([]byte, 1)
_, err = channel.Read(data)
require.NoError(t, err)
require.Equal(t, uint8(0x1), data[0])
_, err = channel.Read(data)
require.NoError(t, err)
require.Equal(t, uint8(0x2), data[0])
})
}

func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.Router) {
Expand Down