Skip to content

fix(analytics): Infinite unbound loop in analytics processor - #57

Merged
khvn26 merged 1 commit into
mainfrom
fix/analytics-processor-busy-loop
Aug 27, 2026
Merged

fix(analytics): Infinite unbound loop in analytics processor#57
khvn26 merged 1 commit into
mainfrom
fix/analytics-processor-busy-loop

Conversation

@khvn26

@khvn26 khvn26 commented Aug 26, 2026

Copy link
Copy Markdown
Member

Closes #55.

In this PR, we:

  1. Fix a busy analytics processor loop by using recv_timeout.
  2. Make sure the processor is flushed on shutdown.

Measured on an idle processor with no features tracked, over one second of wall clock:

CPU consumed
before 0.95s (~95% of a core)
after 0.00s

The loop now blocks for whatever remains of the flush window, so it wakes only when a feature is tracked or a flush is due. Flush cadence is unchanged — a Timeout just falls through to the existing flush check. The write lock is taken only when there is data to record or a flush to perform.

Disconnected previously broke out of the loop immediately. With the default 10s timer, dropping a client could silently lose up to 10s of analytics. Shutdown now flushes unconditionally before exiting, on a best-effort guarantee (we don't want to add a handle as part of this PR — tracking in #58).

The remaining wait uses timer.saturating_sub(elapsed). Without a guard, a flush that overran its own window (a slow HTTP POST, say) makes timer - elapsed underflow: a panic in debug, and in release a ~584M year timeout, which would wedge the analytics thread permanently with no further flushes. That was reachable on the original code path too.

Added two tests to cover new code.

@khvn26
khvn26 requested a review from a team as a code owner August 26, 2026 16:42
@khvn26
khvn26 requested review from matthewelwell and removed request for a team August 26, 2026 16:42
@khvn26
khvn26 requested a review from gagantrivedi August 26, 2026 16:43
@matthewelwell

Copy link
Copy Markdown
Contributor

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The analytics worker now waits for incoming events or the next flush deadline instead of polling. It updates analytics data when events arrive, flushes on timer expiry or channel disconnect, clears flushed data, and exits after shutdown. Tests cover flushing pending events when the processor is dropped and handling an unreachable endpoint without panicking.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: 🟡 Moderate · up to 4ceea

The shutdown path may return before pending analytics are flushed, so applications that exit immediately can lose recent analytics data; merge should wait for the flush to complete or obtain explicit owner acceptance of this bounded risk.

Warning

Some tools did not complete. Review the errors below.

🔧 Clippy (1.97.1)

Clippy execution failed


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: a68a8d6b-8440-4680-9eea-a3d4ef6bf1ba

📥 Commits

Reviewing files that changed from the base of the PR and between a60bb7d and 4ceea3e.

📒 Files selected for processing (1)
  • src/flagsmith/analytics.rs

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.

Comment thread src/flagsmith/analytics.rs
@khvn26
khvn26 merged commit f7e79c5 into main Aug 27, 2026
5 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.

Infinite unbound loop in analytics processor lead to 100% CPU usage

3 participants