Skip to content

[Bug]: race UT flake in TestConnectToLogServiceAddressesClosesSuccessfulLosers #28381

Closed

Description

@iamlinjunhong

Summary

The Ubuntu/x86 unit-test job for PR #28139 failed on one unrelated, race-sensitive logservice test. The PR's changed production and test files do not include pkg/logservice; the exact failing function's runtime call path is the logservice connect/close path and does not invoke the changed IVFFLAT/ObjectIO symbols.

Failure

The workflow ran make ut UT_CONFIGURED=1 UT_PARALLEL=8 UT_TIMEOUT=40 UT_SHARD=all. Its race stages use -mod=readonly -vet=off -short -v -json -tags matrixone_test -race.

The only failed package was github.com/matrixorigin/matrixone/pkg/logservice. The stable signature was:

[TEST]: TestConnectToLogServiceAddressesClosesSuccessfulLosers
client_test.go:349: expected: "first", actual: "second"
WARNING: DATA RACE
Write: pkg/logservice/client_test.go:305
Previous read: pkg/logservice/client.go:587
--- FAIL: TestConnectToLogServiceAddressesClosesSuccessfulLosers
testing.go:1712: race detected during execution of test

The full failed-job log was inspected with gh run view 34183828404 --log-failed; no compile, static-analysis, or changed-package failure was reported. The job completed its other setup and post-processing stages and exited with go test failed with status 1 followed by process exit code 2.

Comparison evidence

The exact test selection was non-empty. With the repository mo-cgo-test wrapper, -race, -short, -tags matrixone_test, CGO_ENABLED=1, and one process with -count=3:

  • PR head 7ae4090054c8747dd05a069f546495b5a595f785: pass, 3/3.
  • Clean official up/main b2f0f3fe69b23cde495c59d226c647aba555cfdc: pass, 3/3.

Focused command:

.agents/skills/mo-dev/scripts/mo-cgo-test -race -vet=off -short -tags matrixone_test -p 8 -count=3 -timeout 120s -run '^TestConnectToLogServiceAddressesClosesSuccessfulLosers$' ./pkg/logservice

The PR diff's Go packages are pkg/objectio, pkg/objectio/ioutil, pkg/vectorindex/ivfflat, pkg/vectorindex/sqlexec, pkg/vm/engine, pkg/vm/engine/readutil, and pkg/vm/engine/tae/blockio; the exact failing logservice connect/close path does not call changed symbols.

Reproduction and impact

  1. Check out PR head 7ae4090054c8747dd05a069f546495b5a595f785.
  2. Use the repository's CGo test wrapper and the focused command above, or run the full CI-equivalent make ut UT_CONFIGURED=1 UT_PARALLEL=8 UT_TIMEOUT=40 UT_SHARD=all with the matrixone_test race configuration.
  3. Observe the nondeterministic ordering assertion and race report in pkg/logservice.

This blocks the PR's Ubuntu/x86 UT check when the race window occurs. No product-code change is proposed in this issue; the purpose is to track and triage the test race/flaky assertion.

Planned validation

The failed run will be rerun once with gh run rerun 34183828404 --failed after this issue is recorded. The rerun result will be compared by the same check, job, test name, and race signature.

Activity

  1. aron-intframe commented on Sep 8, 2026

    @aron-intframe

    The flake is in the test, not in pkg/logservice. client_test.go:349 asserts an ordering that connectToLogServiceAddresses never establishes, and the WARNING: DATA RACE is a consequence of that assertion failing, not a second independent bug. That is why PR #28139 is innocent and why -count=3 on an idle box passes on both revisions.

    1. expected: "first", actual: "second"

    require.Equal(t, "first", readCalledAddress(t, called)) assumes the two attempt goroutines reach called <- addr in launch order. Nothing enforces that. The loop launches "first", then launches "second" one logServiceConnectFallbackDelay later — and the test sets that delay to 1 * time.Millisecond. A 1 ms head start is not a happens-before edge. Under -race with UT_PARALLEL=8 on a loaded runner, goroutine "first" can sit in a run queue longer than 1 ms and "second" sends first.

    2. The data race is caused by #1

    require → t.FailNow() → runtime.Goexit(), which immediately runs the deferred restore registered at client_test.go:305 (connectToLogServiceAddressFn = origConnect). At that moment the goroutine that called connectToLogServiceAddresses is still alive and close(release) (line 351) has not run, so both attempt goroutines are still parked inside the stub, having already read that same package-level var at client.go:587. Write at 305 vs previous read at 587 — exactly the pair in the CI log. Note connectToLogServiceAddresses does wg.Wait() on every return path, so nothing leaks on the passing path; the leak exists only after the assertion aborts the test goroutine.

    How I checked

    I could not build the tree here (no CGo toolchain), so I lifted connectToLogServiceAddresses, closeUnusedConnectResultClients and stopConnectFallbackTimer verbatim into a standalone module plus a faithful copy of the test, with require.Equal replaced by t.Fatalf (same FailNow/Goexit semantics). Line mapping: my client.go:48 = upstream client.go:587, my client_test.go:47 = upstream 305, my client_test.go:95 = upstream 349.

    $ go version
    go version go1.23.4 linux/amd64
    
    # 1) idle box, no contention -- passes, which is why this only shows up in CI
    $ go test -race -count=200 -run '^TestConnectToLogServiceAddressesClosesSuccessfulLosers$' ./...
    ok  	repro	1.251s
    
    # 2) CI-like contention: 20 cores, 64 concurrent race binaries, GOMAXPROCS=2, 400 iterations each
    $ go test -c -race -o repro.test .
    $ for i in $(seq 1 64); do GOMAXPROCS=2 ./repro.test \
          -test.run '^TestConnectToLogServiceAddressesClosesSuccessfulLosers$' \
          -test.count=400 > o.$i 2>&1 & done; wait
    
    iterations total: 25600
    ordering-flake occurrences:  2
    data-race blocks:            2
    loser-close flake:          72
    
    # one failing iteration, verbatim (o.56):
    --- FAIL: TestConnectToLogServiceAddressesClosesSuccessfulLosers (0.00s)
    ==================
    WARNING: DATA RACE
    Write at 0x000000758518 by goroutine 417:
      repro.TestConnectToLogServiceAddressesClosesSuccessfulLosers.func1()
          /.../repro/client_test.go:47 +0x39            <-- upstream client_test.go:305
      runtime.Goexit()
          /home/aron/sdk/go/src/runtime/panic.go:629 +0x5d
      testing.(*common).Fatalf()
          /home/aron/sdk/go/src/testing/testing.go:1090 +0xae
      repro.TestConnectToLogServiceAddressesClosesSuccessfulLosers()
          /.../repro/client_test.go:95 +0x519           <-- upstream client_test.go:349
      testing.tRunner()
          /home/aron/sdk/go/src/testing/testing.go:1690 +0x226
    
    Previous read at 0x000000758518 by goroutine 419:
      repro.connectToLogServiceAddresses.func1.1()
          /.../repro/client.go:48 +0x13e                <-- upstream client.go:587
    
    Goroutine 419 (running) created at:
      repro.connectToLogServiceAddresses.func1()
          /.../repro/client.go:45 +0x205
      repro.connectToLogServiceAddresses()
          /.../repro/client.go:69 +0x305
    ==================
    --- FAIL: TestConnectToLogServiceAddressesClosesSuccessfulLosers (0.00s)
        client_test.go:95: expected: "first", actual: "second"
        testing.go:1399: race detected during execution of test
    
    # 3) instrumented rerun -- counts loser attempts that returned ctx.Err() instead of a client
    $ cat p.* | grep 'loser attempts aborted' | sort | uniq -c
         16     client_test.go:120: expected 1 loser closed, got 0 (loser attempts aborted by winner's cancel(): 1)
    
    # 4) same 25600 iterations with the three test-only changes below
    iterations: 25600
    FAIL lines: 0
    DATA RACE: 0
    PASS procs: 64
    

    2 ordering failures in 25600 iterations, and exactly 2 data races — the race never fired without the ordering assertion failing first. The stack trace above shows the causal chain directly: the write at 305 happens inside runtime.Goexit() called from testing.(*common).Fatalf at 349.

    3. A third flake in the same test, 36x more frequent (72 / 25600)

    assert.Equal(t, 1, closeCount) (upstream ~line 365) can see 0. After the winner's result is received, connectToLogServiceAddresses calls cancel(). The losing attempt may still be parked in the stub's select { case <-release: case <-ctx.Done(): return nil, ctx.Err() }. Once cancel() lands, both cases are ready and Go picks uniformly at random, so the loser returns an error instead of a *client — there is no connection to close and closeCount stays 0 (then got 1 instead of 2 on the next assert). Run 3 above confirms it: every occurrence had exactly one loser attempt aborted by the winner's cancel(). This one hasn't bitten the Ubuntu/x86 job yet, but it is the same test and it is more likely than the race you filed.

    Fix (test-only, no production change)

    // 1. don't assert send order between two independently scheduled goroutines
    seen := map[string]bool{
        readCalledAddress(t, called): true,
        readCalledAddress(t, called): true,
    }
    require.True(t, seen["first"] && seen["second"])
    
    // 2. once release is closed, a cancelled attempt ctx must not turn into an error
    select {
    case <-release:
    case <-ctx.Done():
        select {
        case <-release: // release already won; the attempt succeeded
        default:
            return nil, ctx.Err()
        }
    }
    
    // 3. never let a failed assertion abandon the connect goroutine while the
    //    deferred restore of the package-level vars is running.
    //    Register this AFTER the restore defer so it runs BEFORE it.
    finished := make(chan struct{})
    var releaseOnce sync.Once
    releaseAll := func() { releaseOnce.Do(func() { close(release) }) }
    defer func() {
        releaseAll()
        select {
        case <-finished:
        case <-time.After(2 * time.Second):
        }
    }()
    go func() { defer close(finished); ... }()

    With those three, the same 64 x 400 = 25600 race iterations gave 0 failures and 0 race reports (run 4 above). Item 3 alone is worth applying to every test in this file that swaps connectToLogServiceAddressFn or logServiceConnectFallbackDelay: TestConnectToLogServiceAddressesFallsBack and the hanging-address test at 265-284 have the same shape, so any future assertion failure there will also be reported as a data race and send triage after the wrong file.

    A test that only passes because the scheduler happened to cooperate is a green tick that isn't evidence; that failure family is the whole subject of our own catalog (ours): https://github.com/aron-intframe/agent-guards

  2. added a commit that references this issue on Sep 8, 2026
    f8a690c
  3. added 3 commits that reference this issue on Sep 17, 2026
    30bd7ef
    d3aef49
    1aa7199
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions