From 09580cde4b8b5101ce14ae7c381f2bd2df5a89f1 Mon Sep 17 00:00:00 2001 From: Philipp Hancke Date: Fri, 16 Oct 2026 19:33:29 +0200 Subject: [PATCH] Keep candidate ID when copying GetSelectedCandidatePair returns copies of the selected candidates. These were created by unmarshaling and got new random IDs, so they no longer matched the IDs in candidate and candidate pair stats. --- agent_test.go | 4 ++++ candidate_base.go | 16 ++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/agent_test.go b/agent_test.go index c871d223..b7f22b56 100644 --- a/agent_test.go +++ b/agent_test.go @@ -2511,6 +2511,10 @@ func TestGetSelectedCandidatePair(t *testing.T) { require.True(t, bAgentPair.Local.Equal(aAgentPair.Remote)) require.True(t, bAgentPair.Remote.Equal(aAgentPair.Local)) + aSelected := aAgent.getSelectedPair() + require.Equal(t, aSelected.Local.ID(), aAgentPair.Local.ID()) + require.Equal(t, aSelected.Remote.ID(), aAgentPair.Remote.ID()) + require.NoError(t, wan.Stop()) } diff --git a/candidate_base.go b/candidate_base.go index 9aa49436..0be0f027 100644 --- a/candidate_base.go +++ b/candidate_base.go @@ -675,18 +675,22 @@ func (c *candidateBase) copy() (Candidate, error) { if err != nil { return nil, err } - if address == c.Address() { - return copied, nil - } + var base *candidateBase switch candidate := copied.(type) { case *CandidateHost: - candidate.address = c.Address() + base = &candidate.candidateBase case *CandidateServerReflexive: - candidate.address = c.Address() + base = &candidate.candidateBase + case *CandidatePeerReflexive: + base = &candidate.candidateBase case *CandidateRelay: - candidate.address = c.Address() + base = &candidate.candidateBase + default: + return copied, nil } + base.id = c.id + base.address = c.Address() return copied, nil }