Skip to content

Commit

Permalink
Fix goroutine leakage
Browse files Browse the repository at this point in the history
Added reproducible leakage check along with a fix.
  • Loading branch information
enobufs authored and Sean-Der committed Jan 6, 2024
1 parent 1abdf2d commit daee3aa
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2093,8 +2093,28 @@ func TestAssocDelayedAck(t *testing.T) {
})
}

func checkGoroutineLeaks(t *testing.T) {
// Get the count of goroutines at the start of the test.
initialGoroutines := runtime.NumGoroutine()
// Register a cleanup function to run after the test completes.
t.Cleanup(func() {
// Allow for up to 1 second for all goroutines to finish.
for i := 0; i < 10; i++ {
time.Sleep(100 * time.Millisecond)
if goroutines := runtime.NumGoroutine(); goroutines <= initialGoroutines {
return
}
}

// If we've gotten this far, not all goroutines have finished.
t.Errorf("leaked %d goroutines", runtime.NumGoroutine()-initialGoroutines)
})
}

func TestAssocReset(t *testing.T) {
t.Run("Close one way", func(t *testing.T) {
checkGoroutineLeaks(t)

lim := test.TimeOut(time.Second * 10)
defer lim.Stop()

Expand Down Expand Up @@ -2156,6 +2176,8 @@ func TestAssocReset(t *testing.T) {
})

t.Run("Close both ways", func(t *testing.T) {
checkGoroutineLeaks(t)

lim := test.TimeOut(time.Second * 10)
defer lim.Stop()

Expand All @@ -2173,14 +2195,16 @@ func TestAssocReset(t *testing.T) {

assert.Equal(t, 0, a0.bufferedAmount(), "incorrect bufferedAmount")

// send a message from s0 to s1
n, err := s0.WriteSCTP([]byte(msg), PayloadTypeWebRTCBinary)
if err != nil {
assert.FailNow(t, "failed due to earlier error")
}
assert.Equal(t, len(msg), n, "unexpected length of received data")
assert.Equal(t, len(msg), a0.bufferedAmount(), "incorrect bufferedAmount")

err = s0.Close() // send reset
// close s0 as soon as the message is sent
err = s0.Close()
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -2213,7 +2237,8 @@ func TestAssocReset(t *testing.T) {
}
}

err = s1.Close() // send reset
// send reset from s1
err = s1.Close()
if err != nil {
t.Error(err)
}
Expand All @@ -2222,7 +2247,10 @@ func TestAssocReset(t *testing.T) {
for {
_, _, err = s0.ReadSCTP(buf)
assert.Equal(t, io.EOF, err, "should be EOF")
doneCh <- err
if err != nil {
doneCh <- err
return
}
}
}()

Expand Down

0 comments on commit daee3aa

Please sign in to comment.