Skip to content

Stabilize ICE-lite priorities like libwebrtc - #1060

Merged
JoTurk merged 1 commit into
v4from
jo/stabilize-ice-lite-priorities
Nov 9, 2026
Merged

JoTurk merged 1 commit into
v4from
jo/stabilize-ice-lite-priorities

Conversation

@JoTurk

@JoTurk JoTurk commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Description

History:
PR #959 let ICE-lite agents pick the best pair locally without sending checks. All lite host candidates had the same priority, so with several candidates the two sides could pick different pairs and fail to connect. #968 fixed that by stepping each new lite candidate down from the default until its priority was unique. That made priorities depend on gather order: the same address could get a different priority on another node, or depending on whether the node was single- or dual-stack.

The main problem is that Pion doesn't have local preference like libwebrtc, instead of doing other hacks this adds: (counter << 8) | address precedence. The low byte is the RFC 6724 precedence of the address (IPv6 40, IPv4 30, and so on), so address families never collide. The counter drops only to separate candidates of the same family. A candidate's priority no longer depends on gather order or on whether the agent is single- or dual-stack.

Edit: for what it's worth, Unlike libwebrtc, the high byte is not a network adapter preference, since Pion has no adapter types.

Reference issue

closes #1059

@JoTurk
JoTurk force-pushed the jo/stabilize-ice-lite-priorities branch from 519478e to 88558a6 Compare November 9, 2026 04:30
@JoTurk

JoTurk commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

I'll add tests shortly, I'll also add an e2e test in https://github.com/pion/browser-tests against browsers.

@JoTurk
JoTurk force-pushed the jo/stabilize-ice-lite-priorities branch 4 times, most recently from 3210a5b to 838e6dc Compare November 9, 2026 04:42
@JoTurk
JoTurk requested a balanced review from Copilot November 9, 2026 04:42

Copilot AI 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.

🟡 Changes recommended

The 257th UDP host candidate in a group receives a duplicate priority, making pair selection insertion-order dependent and potentially causing lite peers to disagree.

1 open issue
What changed in this PR

This PR refines ICE-lite candidate priorities to reduce migration and restart inconsistencies caused by IPv4/IPv6 gather order, building on the earlier lite-to-lite selection fixes.

Changes:

  • Partition local preference by address precedence, with a counter separating otherwise colliding candidates.
  • Preserve explicit candidate priority overrides.
  • Add tests for cross-family gather ordering, priority uniqueness, and override preservation.
File Description
candidate.go Adds an internal priority-override check to the candidate interface.
candidate_base.go Implements priority-override detection.
agent.go Uses address precedence and counter-based ICE-lite priority allocation.
agent_test.go Updates uniqueness expectations and tests ordering and overrides.
addr.go Adds address-precedence classification and family fallbacks.

Review effort: Balanced

Comment thread agent.go
Comment on lines +1387 to +1390
if priority&counterMask == 0 {
a.log.Warnf("No unique priority left for ICE-lite candidate %s, using %d", cand, priority)

break

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

an extremely unrealistic scenario, I copied this from libwebrtc anyways https://webrtc.googlesource.com/src/+/refs/heads/main/rtc_base/network.cc#517

@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.10526% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.32%. Comparing base (5efd703) to head (6063b76).
⚠️ Report is 3 commits behind head on v4.

Files with missing lines Patch % Lines
agent.go 80.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##               v4    #1060      +/-   ##
==========================================
- Coverage   88.42%   88.32%   -0.10%     
==========================================
  Files          47       48       +1     
  Lines        6600     6682      +82     
==========================================
+ Hits         5836     5902      +66     
- Misses        515      524       +9     
- Partials      249      256       +7     
Flag Coverage Δ
go 88.32% <92.10%> (-0.10%) ⬇️

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.

History:
PR #959 let ICE-lite agents pick the best pair locally without sending
checks. All lite host candidates had the same priority, so with
several candidates the two sides could pick different pairs and fail
to connect. #968 fixed that by stepping each new lite candidate down
from the default until its priority was unique. That made priorities
depend on gather order: the same address could get a different
priority on another node, or depending on whether the node was
single- or dual-stack.

The main problem is that Pion doesn't have local preference like
libwebrtc, instead of doing other hacks this adds:
`(counter << 8) | address precedence`. The low byte is the RFC 6724
precedence of the address (IPv6 40, IPv4 30, and so on), so address
families never collide. The counter drops only to separate candidates
of the same family. A candidate's priority no longer depends on gather
order or on whether the agent is single- or dual-stack.

Unlike libwebrtc, the high byte is not a network adapter preference,
since Pion has no adapter types.
@JoTurk
JoTurk force-pushed the jo/stabilize-ice-lite-priorities branch from 838e6dc to 6063b76 Compare November 9, 2026 04:53
@cnderrauber

Copy link
Copy Markdown
Member

@JoTurk Thank you for the quick reply, I'll test it soon!

@cnderrauber cnderrauber left a comment

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 works well, thank you!

@JoTurk

JoTurk commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Thank you for the report and the quick test, I'll merge and tag it now, and will add a migration test against browsers in pion/browser-tests, so we catch future issues :)

@JoTurk
JoTurk merged commit 6063b76 into v4 Nov 9, 2026
17 checks passed
@JoTurk
JoTurk deleted the jo/stabilize-ice-lite-priorities branch November 9, 2026 08:06
cnderrauber added a commit that referenced this pull request Nov 30, 2026
#### Description
#1060 Stabilize ICE-lite priorities like libwebrtc, but full-ice still
use the defaultLocalPreference(65535), so a candidate can has different
priority in different mode. This PR apply the local preference
calculation to both modes.
JoTurk added a commit that referenced this pull request Dec 2, 2026
PR #1060 gave ICE-lite candidates unique priorities and #1072
applied the same to full agents, but both skipped relay candidates.

part of #1074
JoTurk added a commit that referenced this pull request Dec 2, 2026
PR #1060 gave ICE-lite candidates unique priorities and #1072
applied the same to full agents, but both skipped relay candidates.

part of #1074
JoTurk added a commit that referenced this pull request Dec 2, 2026
PR #1060 gave ICE-lite candidates unique priorities and #1072
applied the same to full agents, but both skipped relay candidates.

part of #1074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants