Skip to content

Commit

Permalink
Merge pull request #2971 from nirs/portfwd-fix-last-message
Browse files Browse the repository at this point in the history
portfwd: Fix handling the last UDP  message
  • Loading branch information
AkihiroSuda authored Dec 10, 2024
2 parents 1a17a7e + 7a5ddd7 commit 0d9572b
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/portfwd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,33 @@ func HandleUDPConnection(ctx context.Context, client *guestagentclient.GuestAgen
buf := make([]byte, 65507)
for {
n, addr, err := conn.ReadFrom(buf)
// We must handle n > 0 bytes before considering the error.
// https://pkg.go.dev/net#PacketConn
if n > 0 {
msg := &api.TunnelMessage{
Id: id + "-" + addr.String(),
Protocol: "udp",
GuestAddr: guestAddr,
Data: buf[:n],
UdpTargetAddr: addr.String(),
}
if err := stream.Send(msg); err != nil {
return err
}
}
if err != nil {
// https://pkg.go.dev/net#PacketConn does not mention io.EOF semantics.
if errors.Is(err, io.EOF) {
return nil
}
return err
}
msg := &api.TunnelMessage{
Id: id + "-" + addr.String(),
Protocol: "udp",
GuestAddr: guestAddr,
Data: buf[:n],
UdpTargetAddr: addr.String(),
}
if err := stream.Send(msg); err != nil {
return err
}
}
})

g.Go(func() error {
for {
// Not documented: when err != nil, in is always nil.
in, err := stream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
Expand Down Expand Up @@ -128,6 +133,7 @@ func (g GrpcClientRW) Write(p []byte) (n int, err error) {
}

func (g GrpcClientRW) Read(p []byte) (n int, err error) {
// Not documented: when err != nil, in is always nil.
in, err := g.stream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
Expand Down

0 comments on commit 0d9572b

Please sign in to comment.