Skip to content

Commit

Permalink
Merge pull request #542 from dedis/infinite_loop_1851
Browse files Browse the repository at this point in the history
Drop connection if an unknown error occurs
  • Loading branch information
Jeff R. Allen authored May 6, 2019
2 parents 06b1fe7 + 847de9c commit 8eb7fa5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions network/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ func (r *Router) handleConn(remote *ServerIdentity, c Conn) {
r.triggerConnectionErrorHandlers(remote)
return
}
if err == ErrUnknown {
// The error might not be recoverable so the connection is dropped
log.Lvlf5("%v drops %v connection: unknown", r.ServerIdentity, remote)
r.triggerConnectionErrorHandlers(remote)
return
}
// Temporary error, continue.
log.Lvl3(r.ServerIdentity, "Error with connection", address, "=>", err)
continue
Expand Down
45 changes: 45 additions & 0 deletions network/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,48 @@ func waitTimeout(timeout time.Duration, repeat int,
}

}

type testConn struct{}

func (c *testConn) Receive() (*Envelope, error) {
return nil, ErrUnknown
}

func (c *testConn) Close() error {
return nil
}

func (c *testConn) Local() Address {
return ""
}

func (c *testConn) Remote() Address {
return ""
}

func (c *testConn) Rx() uint64 {
return 0
}

func (c *testConn) Tx() uint64 {
return 0
}

func (c *testConn) Send(msg Message) (uint64, error) {
return 0, nil
}

func (c *testConn) Type() ConnType {
return "testConn"
}

// This test insures that an unknown error cannot end up as an infinite loop
// when handling a connection
func TestRouterHandleUnknownError(t *testing.T) {
router, err := NewTestRouterTCP(0)
require.NoError(t, err)

router.wg.Add(1)
// The test will leak 1 goroutine if the connection is not dropped
go router.handleConn(router.ServerIdentity, &testConn{})
}
2 changes: 2 additions & 0 deletions network/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func handleError(err error) error {
if netErr.Timeout() {
return ErrTimeout
}

log.Errorf("Unknown error caught: %s", err.Error())
return ErrUnknown
}

Expand Down

0 comments on commit 8eb7fa5

Please sign in to comment.