Skip to content

[CI] Enable race detection for unit tests in CI#442

Merged
cendhu merged 9 commits intohyperledger:mainfrom
dean-amar:race-detection
Mar 24, 2026
Merged

[CI] Enable race detection for unit tests in CI#442
cendhu merged 9 commits intohyperledger:mainfrom
dean-amar:race-detection

Conversation

@dean-amar
Copy link
Copy Markdown
Contributor

@dean-amar dean-amar commented Mar 17, 2026

Type of change

  • Test update

Description

This PR enables Go's race detector for unit tests to catch data race conditions before code is merged.

  • Enabled race detection for make test-no-db and test-all-db
  • Excluded integration and container tests to maintain reasonable CI execution times
  • Fix race conditions

Related issues

@dean-amar dean-amar changed the title Enable race detection for unit tests in CI [CI] Enable race detection for unit tests in CI Mar 17, 2026
@cendhu
Copy link
Copy Markdown
Contributor

cendhu commented Mar 18, 2026

There would be a significant number of failing test due to race. As we are using t.Parallel(), many race occurs across packages. I decided not to pollute the production code to accomodate test run methods.

cendhu pushed a commit that referenced this pull request Mar 18, 2026
#### Type of change

- Bug fix
- Test update
 
#### Description

- The metrics unit test had apparent race condition when accessing the
URL
- This PR fix this by using atomic pointer
- This is an intermediate solution until we address #441 

#### Related issues

- discovered by #442

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
cendhu pushed a commit that referenced this pull request Mar 18, 2026
…k to prevent race condition (#445)

#### Type of change

- Bug fix
- Test update

#### Description

- PrepareBlockHeaderAndMetadata returns a copy of the block to prevent
race condition

#### Related issues

- discovered by #442

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
cendhu pushed a commit that referenced this pull request Mar 19, 2026
#### Type of change

- Bug fix
- Test update

#### Description

Root cause: The test was experiencing a race condition when accessing
command output buffers. In `UnitTestRunner`, the command execution runs
in a goroutine that continuously writes output to `bytes.Buffer`
instances (cmdStdOut and cmdStdErr), while simultaneously another
goroutine in `assert.Eventually` reads from these buffers via
`cmdStdOut.String()` to check for expected output. Since `bytes.Buffer`
is not thread-safe for concurrent read/write operations, this caused
data races detected by the race detector when multiple goroutines
accessed the buffer simultaneously.

#### Related issues

- discovered by #442

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
liran-funaro added a commit that referenced this pull request Mar 19, 2026
#### Type of change

- Bug fix

#### Description

**Root cause:** The `Ready.Reset()` method was experiencing a race
condition due to non-atomic struct field replacement. The original
implementation used `*r = *NewReady()` to reset the struct, which
performs a multi-step memory copy operation that replaces all fields
(channels `ready`, `closed`, and `once`). This struct copy is not
atomic.

Concurrently, other goroutines calling `WaitForReady()` or
`WaitForAllReady()` would read from the same channel fields (`r.ready`
and `r.closed`) via select statements. This created a classic data race
where one goroutine was writing to struct fields while others were
simultaneously reading from them.

**Race scenario:**
1. Goroutine A calls `Reset()` and begins copying new struct fields
(non-atomic multi-step operation)
2. Goroutine B calls `WaitForReady()` and attempts to read `r.ready` or
`r.closed` channels
3. Data race occurs: Goroutine A writes to memory while Goroutine B
reads from the same memory locations

**The fix:** Uses atomic pointer indirection by moving channels into an
inner `ready` struct and storing only an `atomic.Pointer[ready]` in the
outer `Ready` struct. This enables atomic swapping of the entire
internal state via `CompareAndSwap()`, eliminating the race condition
while maintaining the same external API.

#### Related issues

- discovered by #442

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
Enable Go race detection for unit tests only (excluding integration/container tests) to catch data races while keeping CI times reasonable. Race detection is enabled via ENABLE_RACE environment variable in CI workflows but disabled by default for local development.

Signed-off-by: Dean Amar <Dean.Amar@ibm.com>
Add test_flags Makefile variable to pass flags to test commands.
Enable race detection in CI for unit tests by passing test_flags="-race".
Race detection is disabled by default locally but enabled in CI to catch
data races while maintaining fast local development workflow.

Signed-off-by: Dean Amar <Dean.Amar@ibm.com>
Signed-off-by: Dean Amar <Dean.Amar@ibm.com>
cendhu pushed a commit that referenced this pull request Mar 23, 2026
#### Type of change

- Improvement (improvement to code, performance, etc)
 
#### Description

- Add support for test_flags in Makefile

#### Related issues

- helps #442

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
@liran-funaro
Copy link
Copy Markdown
Contributor

@dean-amar Please rebase

dean-amar and others added 3 commits March 23, 2026 13:04
Signed-off-by: Dean Amar <Dean.Amar@ibm.com>
Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 90.387% (-0.1%) from 90.509%
when pulling 4b0123b on dean-amar:race-detection
into 5561b8a on hyperledger:main.

@liran-funaro liran-funaro marked this pull request as ready for review March 24, 2026 12:12
@liran-funaro
Copy link
Copy Markdown
Contributor

liran-funaro commented Mar 24, 2026

@cendhu I've updated this PR and fixed all the currently known races. Please review.

@cendhu
Copy link
Copy Markdown
Contributor

cendhu commented Mar 24, 2026

@liran-funaro Great effort.

Copy link
Copy Markdown
Contributor

@cendhu cendhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cendhu cendhu merged commit 887ff5e into hyperledger:main Mar 24, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[verifier] Data race in BCCSP [signature-test] Data race in TestBlsSignatureVerificationErrors [test] enable race detecting in go test

4 participants