Skip to content

Conversation

@mohansinghi
Copy link

@mohansinghi mohansinghi commented Jan 15, 2026

Summary

Division by Zero in Leaderboard Calculation

Severity: Medium
File: crates/platform-server/src/db/queries.rs:341
Effort: Small (4 lines)

Current - can panic if scores vector is empty:

let consensus_score = scores.iter().sum::<f64>() / scores.len() as f64;

Impact: When scores vector is empty, division by zero causes panic and crashes the leaderboard update endpoint.

Fix: Add defensive check before division:

let consensus_score = if scores.is_empty() {
    0.0 // Should not happen due to check above, but defensive
} else {
    scores.iter().sum::<f64>() / scores.len() as f64
};

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=158349177

Summary by CodeRabbit

  • Bug Fixes
    • Improved leaderboard consensus-score calculation to defensively handle empty or missing evaluation data, avoiding errors and improving stability when processing incomplete data sets.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 15, 2026

📝 Walkthrough

Walkthrough

Updated update_leaderboard to return Ok(None) immediately when the evaluations list is empty; additionally, consensus_score computation now defensively handles an empty scores collection by using 0.0 as the fallback value.

Changes

Cohort / File(s) Summary
Database query update
crates/platform-server/src/db/queries.rs
Return Ok(None) early when evaluations is empty; compute consensus_score defensively by treating an empty scores collection as 0.0 to avoid invalid average calculations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A cautious hop, a careful stare,

If no evaluations, I'll wait right there.
Empty scores? I set them to zero,
Safe averages so nothing goes hero.
🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing a division-by-zero bug in leaderboard calculations, which is the core issue addressed in the pull request.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings


📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3bfd39 and e165d4c.

📒 Files selected for processing (1)
  • crates/platform-server/src/db/queries.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/platform-server/src/db/queries.rs

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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 and usage tips.

Add defensive check for empty scores vector before division to prevent
potential panic. While evaluations.is_empty() is checked above, this
adds an extra layer of safety in case the mapping produces an empty
vector.

Fixes division by zero bug in update_leaderboard function.
@mohansinghi mohansinghi force-pushed the fix/division-by-zero-leaderboard branch from a3bfd39 to e165d4c Compare January 15, 2026 04:11
@mohansinghi
Copy link
Author

@echobt Could you please review my pr?

@echobt echobt closed this Jan 18, 2026
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.

2 participants