Skip to content
Snippets Groups Projects
Commit 7f51675d authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

prevent go routine managing reconnects to leak on fsm stop

parent 86f17a1f
No related branches found
No related tags found
No related merge requests found
package server package server
import ( import (
"context"
"fmt" "fmt"
"io"
"net" "net"
"sync" "sync"
"time" "time"
...@@ -70,6 +72,8 @@ type FSM struct { ...@@ -70,6 +72,8 @@ type FSM struct {
stateMu sync.RWMutex stateMu sync.RWMutex
reason string reason string
active bool active bool
connectionCancelFunc context.CancelFunc
} }
// NewPassiveFSM2 initiates a new passive FSM // NewPassiveFSM2 initiates a new passive FSM
...@@ -105,8 +109,11 @@ func newFSM2(peer *peer) *FSM { ...@@ -105,8 +109,11 @@ func newFSM2(peer *peer) *FSM {
} }
func (fsm *FSM) start() { func (fsm *FSM) start() {
ctx, cancel := context.WithCancel(context.Background())
fsm.connectionCancelFunc = cancel
go fsm.run() go fsm.run()
go fsm.tcpConnector() go fsm.tcpConnector(ctx)
return return
} }
...@@ -115,6 +122,8 @@ func (fsm *FSM) activate() { ...@@ -115,6 +122,8 @@ func (fsm *FSM) activate() {
} }
func (fsm *FSM) run() { func (fsm *FSM) run() {
defer fsm.cancelRunningGoRoutines()
next, reason := fsm.state.run() next, reason := fsm.state.run()
for { for {
newState := stateName(next) newState := stateName(next)
...@@ -141,6 +150,12 @@ func (fsm *FSM) run() { ...@@ -141,6 +150,12 @@ func (fsm *FSM) run() {
} }
} }
func (fsm *FSM) cancelRunningGoRoutines() {
if fsm.connectionCancelFunc != nil {
fsm.connectionCancelFunc()
}
}
func stateName(s state) string { func stateName(s state) string {
switch s.(type) { switch s.(type) {
case *idleState: case *idleState:
...@@ -166,7 +181,7 @@ func (fsm *FSM) cease() { ...@@ -166,7 +181,7 @@ func (fsm *FSM) cease() {
fsm.eventCh <- Cease fsm.eventCh <- Cease
} }
func (fsm *FSM) tcpConnector() error { func (fsm *FSM) tcpConnector(ctx context.Context) error {
for { for {
select { select {
case <-fsm.initiateCon: case <-fsm.initiateCon:
...@@ -186,6 +201,8 @@ func (fsm *FSM) tcpConnector() error { ...@@ -186,6 +201,8 @@ func (fsm *FSM) tcpConnector() error {
case <-time.NewTimer(time.Second * 30).C: case <-time.NewTimer(time.Second * 30).C:
c.Close() c.Close()
continue continue
case <-ctx.Done():
return nil
} }
} }
} }
...@@ -271,6 +288,23 @@ func (fsm *FSM) sendKeepalive() error { ...@@ -271,6 +288,23 @@ func (fsm *FSM) sendKeepalive() error {
return nil return nil
} }
func recvMsg(c net.Conn) (msg []byte, err error) {
buffer := make([]byte, packet.MaxLen)
_, err = io.ReadFull(c, buffer[0:packet.MinLen])
if err != nil {
return nil, fmt.Errorf("Read failed: %v", err)
}
l := int(buffer[16])*256 + int(buffer[17])
toRead := l
_, err = io.ReadFull(c, buffer[packet.MinLen:toRead])
if err != nil {
return nil, fmt.Errorf("Read failed: %v", err)
}
return buffer, nil
}
func stopTimer(t *time.Timer) { func stopTimer(t *time.Timer) {
if !t.Stop() { if !t.Stop() {
select { select {
......
...@@ -2,13 +2,11 @@ package server ...@@ -2,13 +2,11 @@ package server
import ( import (
"fmt" "fmt"
"io"
"net" "net"
"strings" "strings"
"sync" "sync"
"github.com/bio-routing/bio-rd/config" "github.com/bio-routing/bio-rd/config"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable/locRIB" "github.com/bio-routing/bio-rd/routingtable/locRIB"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
...@@ -125,20 +123,3 @@ func (b *bgpServer) AddPeer(c config.Peer, rib *locRIB.LocRIB) error { ...@@ -125,20 +123,3 @@ func (b *bgpServer) AddPeer(c config.Peer, rib *locRIB.LocRIB) error {
return nil return nil
} }
func recvMsg(c net.Conn) (msg []byte, err error) {
buffer := make([]byte, packet.MaxLen)
_, err = io.ReadFull(c, buffer[0:packet.MinLen])
if err != nil {
return nil, fmt.Errorf("Read failed: %v", err)
}
l := int(buffer[16])*256 + int(buffer[17])
toRead := l
_, err = io.ReadFull(c, buffer[packet.MinLen:toRead])
if err != nil {
return nil, fmt.Errorf("Read failed: %v", err)
}
return buffer, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment