Skip to content

Conversation

@aa1ex
Copy link
Contributor

@aa1ex aa1ex commented Nov 21, 2025

Summary

  • Fix memory leak in getCheckResult() caused by time.After() in select loop
  • Add test demonstrating the leak and benchmarks comparing allocations

Problem

time.After() in a select loop creates a new timer on each iteration that won't be garbage collected until it fires. With frequent watch events, this causes ~280 bytes leak per event.

Solution

Replace time.After() with time.NewTimer() + Reset() pattern:

  • Timer created once before the loop
  • Reset after each event to preserve original timeout semantics
  • Properly stopped on exit via defer

Benchmark Results

Pattern Memory Allocations
time.After() (before) 283 B/op 3 allocs/op
time.NewTimer (after) 0 B/op 0 allocs/op

Add test that demonstrates the memory leak caused by using time.After()
in a select loop. Each iteration allocates ~280 bytes that are not freed
until the timer fires.

Benchmark results:
- time.After(): 283 B/op, 3 allocs/op
- time.NewTimer with Stop/Reset: 0 B/op, 0 allocs/op

This test will fail after the fix is applied (allocations will be 0).
…me.NewTimer

time.After() in select loop allocates ~280 bytes per iteration that are
not freed until the timer fires. Replace with time.NewTimer + Reset pattern.
@aa1ex aa1ex requested a review from dkhachyan November 21, 2025 14:55
@aa1ex aa1ex merged commit c7e505c into kaasops:main Nov 24, 2025
4 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.

1 participant