Skip to content
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

Check if conn is closed before writing to it. #504

Merged
merged 3 commits into from
Jan 12, 2023
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 AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Meelap Shah <[email protected]>
Michael MacDonald <[email protected]>
Michael MacDonald <[email protected]>
Mikhail Bragin <[email protected]>
Miroslav Šedivý <[email protected]>
Nevio Vesic <[email protected]>
Ori Bernstein <[email protected]>
Robert Eperjesi <[email protected]>
Expand Down
8 changes: 8 additions & 0 deletions agent_udpmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func TestMuxAgent(t *testing.T) {
require.NoError(t, conn.Close())
require.NoError(t, muxedConn.Close())
require.NoError(t, udpMux.Close())

// expect error when reading from closed mux
_, err = muxedConn.Read(data)
require.Error(t, err)

// expect error when writing to closed mux
_, err = muxedConn.Write(data)
require.Error(t, err)
})
}
}
4 changes: 4 additions & 0 deletions candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ func (c *candidateBase) close() error {
func (c *candidateBase) writeTo(raw []byte, dst Candidate) (int, error) {
n, err := c.conn.WriteTo(raw, dst.addr())
if err != nil {
// If the connection is closed, we should return the error
if errors.Is(err, io.ErrClosedPipe) {
return n, err
}
c.agent().log.Infof("%s: %v", errSendPacket, err)
m1k1o marked this conversation as resolved.
Show resolved Hide resolved
return n, nil
}
Expand Down
43 changes: 43 additions & 0 deletions candidate_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ice

import (
"net"
"testing"
"time"

"github.com/pion/logging"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCandidatePriority(t *testing.T) {
Expand Down Expand Up @@ -337,3 +340,43 @@ func TestCandidateMarshal(t *testing.T) {
assert.Equal(t, test.marshaled, actualCandidate.Marshal())
}
}

func TestCandidateWriteTo(t *testing.T) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IP{127, 0, 0, 1},
Port: 0,
})
require.NoError(t, err, "error creating test tcp listener")

conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
require.NoError(t, err, "error dialing test tcp conn")

loggerFactory := logging.NewDefaultLoggerFactory()
packetConn := newTCPPacketConn(tcpPacketParams{
ReadBuffer: 2048,
Logger: loggerFactory.NewLogger("tcp-packet-conn"),
})

err = packetConn.AddConn(conn, nil)
require.NoError(t, err, "error adding test tcp conn to packet conn")

c1 := &candidateBase{
conn: packetConn,
currAgent: &Agent{
log: loggerFactory.NewLogger("agent"),
},
}

c2 := &candidateBase{
resolvedAddr: listener.Addr(),
}

_, err = c1.writeTo([]byte("test"), c2)
assert.NoError(t, err, "writing to open conn")

err = packetConn.Close()
require.NoError(t, err, "error closing test tcp conn")

_, err = c1.writeTo([]byte("test"), c2)
assert.Error(t, err, "writing to closed conn")
}