Skip to content

Commit 36e9a79

Browse files
committed
close shared pool before goleak
CI intermittently failed in `test/race` with a `goleak` report for `github.com/jackc/pgx/v5/pgconn/internal/bgreader.(*BGReader).bgRead`: goleak: Errors on successful test run: found unexpected goroutines: [Goroutine ... with github.com/jackc/pgx/v5/pgconn/internal/bgreader.(*BGReader).bgRead on top of the stack ...] created by github.com/jackc/pgx/v5/pgconn/internal/bgreader.(*BGReader).Start Failing run: https://github.com/riverqueue/river/actions/runs/23095300730/job/67086685003 That stack does not point to a River-managed goroutine. It means a pgx connection was still open when leak checking ran. In our test helpers, the shared Postgres pool was intentionally kept alive until process exit, but `goleak.Find` runs before exit. That allowed idle pgx background reader goroutines to still be blocked in socket reads during teardown even when package tests had otherwise finished cleanly. Fix this by explicitly closing the shared test pool before running `goleak.Find` in the shared `TestMain` wrapper. Internal tests now reuse that shared wrapper directly so there is only one leak-checking path to maintain. This addresses the flake at its source and keeps `goleak` useful, rather than globally ignoring all `bgRead` goroutines and potentially hiding real connection leaks.
1 parent 8da91e9 commit 36e9a79

2 files changed

Lines changed: 4 additions & 14 deletions

File tree

internal/riverinternaltest/riverinternaltest.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
package riverinternaltest
44

55
import (
6-
"fmt"
7-
"os"
86
"sync"
97
"testing"
108
"time"
119

12-
"go.uber.org/goleak"
13-
1410
"github.com/riverqueue/river/rivershared/riversharedtest"
1511
)
1612

@@ -103,14 +99,5 @@ func DrainContinuously[T any](drainChan <-chan T) func() []T {
10399
// WrapTestMain performs some common setup and teardown that should be shared
104100
// amongst all packages. e.g. Checks for no goroutine leaks on teardown.
105101
func WrapTestMain(m *testing.M) {
106-
status := m.Run()
107-
108-
if status == 0 {
109-
if err := goleak.Find(riversharedtest.IgnoredKnownGoroutineLeaks...); err != nil {
110-
fmt.Fprintf(os.Stderr, "goleak: Errors on successful test run: %v\n", err)
111-
status = 1
112-
}
113-
}
114-
115-
os.Exit(status)
102+
riversharedtest.WrapTestMain(m)
116103
}

rivershared/riversharedtest/riversharedtest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ var IgnoredKnownGoroutineLeaks = []goleak.Option{ //nolint:gochecknoglobals
356356
// and checks for no goroutine leaks on teardown.
357357
func WrapTestMain(m *testing.M) {
358358
status := m.Run()
359+
if dbPool != nil {
360+
dbPool.Close()
361+
}
359362

360363
if status == 0 {
361364
if err := goleak.Find(IgnoredKnownGoroutineLeaks...); err != nil {

0 commit comments

Comments
 (0)