Skip to content

fix(test): force GC behaviour for determinism#2807

Merged
arajasek merged 1 commit intomainfrom
asr/deflake-gc
Feb 5, 2026
Merged

fix(test): force GC behaviour for determinism#2807
arajasek merged 1 commit intomainfrom
asr/deflake-gc

Conversation

@arajasek
Copy link
Contributor

@arajasek arajasek commented Feb 5, 2026

Cursor-supplied change, aims to prevent failures like this one https://github.com/sei-protocol/sei-chain/actions/runs/21715802000/job/62631307540?pr=2805

@github-actions
Copy link

github-actions bot commented Feb 5, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedFeb 5, 2026, 3:40 PM

@codecov
Copy link

codecov bot commented Feb 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.63%. Comparing base (bcfab9b) to head (82b4495).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2807      +/-   ##
==========================================
+ Coverage   56.62%   56.63%   +0.01%     
==========================================
  Files        2031     2031              
  Lines      165929   165929              
==========================================
+ Hits        93962    93982      +20     
+ Misses      63734    63721      -13     
+ Partials     8233     8226       -7     
Flag Coverage Δ
sei-chain 41.47% <ø> (+0.01%) ⬆️
sei-cosmos 48.12% <ø> (+<0.01%) ⬆️
sei-db 68.72% <ø> (ø)
sei-tendermint 58.15% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 36 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

_ = el.Next()
els[i] = nil // Clear reference to allow GC
}
els = nil // Clear the slice to allow GC of all elements
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would probably rewrite this entire test to use runtime.ReadMemStats. That should be more reliable and inherently less flaky. Because, it won't depend on the finalizer timing.

Something like this:

func TestGCRandom(t *testing.T) {
	const numElements = 10000

	// Force GC and get baseline
	runtime.GC()
	runtime.GC() // twice to clean up finalizers from previous tests
	var before runtime.MemStats
	runtime.ReadMemStats(&before)

	l := New()
	type value struct {
		Data [128]byte // Make it large enough to measure
	}

	for i := 0; i < numElements; i++ {
		v := new(value)
		l.PushBack(v)
	}

	els := make([]*CElement, 0, numElements)
	for el := l.Front(); el != nil; el = el.Next() {
		els = append(els, el)
	}

	// Remove in random order
	for _, i := range mrand.Perm(numElements) {
		l.Remove(els[i])
		els[i] = nil
	}
	els = nil

	// Force GC and measure
	runtime.GC()
	runtime.GC()
	var after runtime.MemStats
	runtime.ReadMemStats(&after)

	// Check that memory was reclaimed (allow some slack for runtime overhead)
	leaked := int64(after.HeapAlloc) - int64(before.HeapAlloc)
	maxAcceptableLeak := int64(numElements * 64) // Much less than numElements * sizeof(value)

	if leaked > maxAcceptableLeak {
		t.Errorf("potential memory leak: %d bytes not reclaimed", leaked)
	}
}

@arajasek arajasek merged commit c50da24 into main Feb 5, 2026
45 of 46 checks passed
@arajasek arajasek deleted the asr/deflake-gc branch February 5, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants