Skip to content

Commit

Permalink
network: fix tests data race
Browse files Browse the repository at this point in the history
s.Shutdown() does not wait for all goroutines of the node server to
stop, which causes logs to attempt to write after the test has ended.

Signed-off-by: Ekaterina Pavlova <[email protected]>
  • Loading branch information
AliceInHunterland committed Feb 17, 2024
1 parent ea23772 commit 373700d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ type (
// lastRequestedHeader contains a height of the last requested header.
lastRequestedHeader atomic.Uint32

register chan Peer
unregister chan peerDrop
handshake chan Peer
quit chan struct{}
relayFin chan struct{}
register chan Peer
unregister chan peerDrop
handshake chan Peer
quit chan struct{}
relayFin chan struct{}
runFin chan struct{}
broadcastTxFin chan struct{}
runProtoFin chan struct{}

transactions chan *transaction.Transaction

Expand Down Expand Up @@ -182,6 +185,9 @@ func newServerFromConstructors(config ServerConfig, chain Ledger, stSync StateSy
config: chain.GetConfig().ProtocolConfiguration,
quit: make(chan struct{}),
relayFin: make(chan struct{}),
runFin: make(chan struct{}),
broadcastTxFin: make(chan struct{}),
runProtoFin: make(chan struct{}),
register: make(chan Peer),
unregister: make(chan peerDrop),
handshake: make(chan Peer),
Expand Down Expand Up @@ -318,7 +324,10 @@ func (s *Server) Shutdown() {
s.notaryRequestPool.StopSubscriptions()
}
close(s.quit)
<-s.broadcastTxFin
<-s.runProtoFin
<-s.relayFin
<-s.runFin

_ = s.log.Sync()
}
Expand Down Expand Up @@ -434,6 +443,7 @@ func (s *Server) run() {
addrTimer = time.NewTimer(peerCheckTime)
peerTimer = time.NewTimer(s.ProtoTickInterval)
)
defer close(s.runFin)
defer addrTimer.Stop()
defer peerTimer.Stop()
go s.runProto()
Expand Down Expand Up @@ -532,6 +542,7 @@ func (s *Server) run() {

// runProto is a goroutine that manages server-wide protocol events.
func (s *Server) runProto() {
defer close(s.runProtoFin)
pingTimer := time.NewTimer(s.PingInterval)
for {
prevHeight := s.chain.BlockHeight()
Expand Down Expand Up @@ -1650,6 +1661,7 @@ func (s *Server) broadcastTxLoop() {
batchSize = 42
)

defer close(s.broadcastTxFin)
txs := make([]util.Uint256, 0, batchSize)
var timer *time.Timer

Expand Down

0 comments on commit 373700d

Please sign in to comment.