Skip to content

[Bug]: logservice shutdown races detached HAKeeper SyncRead with Dragonboat destruction #26487

Closed

Description

@XuPeng-SH

Problem

pkg/embed.TestBaseClusterCanWorkWithNewCluster can race during logservice shutdown: Dragonboat destroys a state machine while a detached HAKeeper task-scheduling goroutine is still executing SyncRead.

Observed in run 30603896298 / job 91072145422 at exact head 8410ce20f36f321a3ea8da32770d459feba34503:

WARNING: DATA RACE
Write:
  dragonboat/internal/rsm.(*OffloadedStatus).SetDestroyed
  dragonboat/internal/rsm.(*NativeSM).Close
  dragonboat.(*node).destroy

Previous read:
  dragonboat/internal/rsm.(*NativeSM).Lookup
  dragonboat.(*NodeHost).SyncRead
  logservice.(*store).read                    store.go:705
  logservice.(*store).getCheckerState        store_hakeeper_check.go:483
  logservice.(*store).tickerForTaskSchedule  store.go:1174

The test fails with race detected during execution of test.

Relationship to PR #26473

This is not a regression introduced by #26473:

  • fix: stabilize lockservice RPC lifecycle under CI load #26473 does not modify pkg/embed, pkg/logservice, or the Dragonboat dependency.
  • The race stack contains no lockservice or MORPC frame.
  • The same CI run passed both pkg/lockservice and pkg/logservice; the race is exposed by embed cluster teardown.
  • On the exact PR head, the test passed -race -count=3 in 67.51s.
  • On pre-PR main cf6dca31107769ad7855ebb45b1a6b48298284d6, it passed -race -count=3 in 67.37s.
  • Existing [Bug]:ut TestBaseClusterCanWorkWithNewCluster failed #20604 has the same test name but a different root cause (port already in use), so it is not a duplicate.

Root cause

store.ticker starts task scheduling with an untracked goroutine:

go l.tickerForTaskSchedule(ctx, l.cfg.HAKeeperCheckInterval.Duration)

store.close calls:

l.tickerStopper.Stop()
l.stopper.Stop()
l.nh.Close()

Stopping tickerStopper waits for the parent ticker task, but not for the raw child goroutine. Cancellation also does not immediately fence its current read because getCheckerState() creates a new timeout context from context.Background(). Consequently l.nh.Close() may destroy the state machine while the detached goroutine is inside SyncRead.

Suggested fix

  1. Put tickerForTaskSchedule under structured ownership (the same stopper or an explicit wait group); do not launch it as an untracked goroutine.
  2. Thread the ticker's cancellation context into getCheckerStateWithContext so shutdown cancels an in-flight SyncRead.
  3. Ensure store.close joins all NodeHost users before calling l.nh.Close().
  4. Add an unhappy-path race test that blocks task-scheduler SyncRead, initiates close, and verifies close waits or cancels before NodeHost destruction.
  5. Audit other raw goroutines that access l.nh for the same close-order invariant.

Acceptance criteria

  • TestBaseClusterCanWorkWithNewCluster -race -count=50 is stable.
  • A deterministic test covers shutdown while task-schedule state lookup is in flight.
  • No goroutine can enter or remain in NodeHost operations after the close sequence reaches NodeHost.Close.

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions