Skip to content

Validate minimum samples for slope calculation - #53

Merged
arjunshajitech merged 1 commit into
pion:mainfrom
arjunshajitech:fix-slope
Aug 24, 2026
Merged

Validate minimum samples for slope calculation#53
arjunshajitech merged 1 commit into
pion:mainfrom
arjunshajitech:fix-slope

Conversation

@arjunshajitech

Copy link
Copy Markdown
Contributor

Description

This PR adds a small guard in fitSlope to handle cases where fewer than two packets are available. This avoids calculating a slope with insufficient data and returns safely instead.

@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.20%. Comparing base (46407eb) to head (c7368e5).

Additional details and impacted files
@@             Coverage Diff             @@
##              main      #53      +/-   ##
===========================================
- Coverage   100.00%   99.20%   -0.80%     
===========================================
  Files            7        7              
  Lines          251      253       +2     
===========================================
  Hits           251      251              
- Misses           0        1       +1     
- Partials         0        1       +1     
Flag Coverage Δ
go 99.20% <100.00%> (-0.80%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

if len(packets) < 2 {
return 0, false
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this actually do anything?

denominator should be 0 for len < 2 because (x - avgX) is 0 for len 1, and for len zero we don't run the calculation loop at all

https://github.com/arjunshajitech/bwe/blob/be8c36cb457cac63e8dd0aedd90b2b499110a734/gcc/trendline_estimator.go#L106-L108

I'm all for adding an early return to make the code explicit but does it actually fix anything?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The early return doesn't fix a functional issue, but it makes the minimum input requirement explicit and avoids unnecessary calculations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we amend the commit message then? :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was reading the implementation and, while comparing it with the WebRTC implementation (https://webrtc.googlesource.com/src/+/refs/heads/main/modules/congestion_controller/goog_cc/trendline_estimator.cc#58), I added the check mainly for simplicity and clarity.

If this doesn't make sense, feel free to close the PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can we amend the commit message then? :)

sure

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's fine to add an early return, i think it will make the code easier to follow, it's just that we use commit messages in releases and "Fix prevent invalid slope calculation" would be misleading for people who read releases :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@arjunshajitech arjunshajitech changed the title Fix prevent invalid slope calculation Validate minimum samples for slope calculation Aug 24, 2026
@arjunshajitech
arjunshajitech merged commit 1dcabf0 into pion:main Aug 24, 2026
18 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.

2 participants