Skip to content

server: close the losing incoming connection when its OPEN arrives - #3877

Open
MaxRink wants to merge 1 commit into
osrg:masterfrom
MaxRink:collision-close-loser-at-open
Open

MaxRink wants to merge 1 commit into
osrg:masterfrom
MaxRink:collision-close-loser-at-open

Conversation

@MaxRink

@MaxRink MaxRink commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Since b841cb7, when the peer's OPEN arrives on our outgoing connection
while the incoming connection is in OpenSent and has no OPEN yet, and we
have the higher BGP Identifier, opensent() sends Cease/Connection
Collision Resolution on the incoming connection at once and closes it.

RFC 4271 Section 6.8 detects a collision when an OPEN arrives:

Upon receipt of an OPEN message, the local system MUST examine all of
its connections that are in the OpenConfirm state.

The peer has not sent its OPEN on that connection yet, so it does not
see the close as the losing side of a collision. FRR connects again.
With graceful restart negotiated, the new connection arrives while the
session on our connection is Established and is handled as a restart of
the peer (RFC 4724 Section 4.2), so the session goes down a second time.
In a lab with FRR 10.x, every carrier fault caused two session drops
instead of one (confirmed with packet captures).

Keep the losing incoming connection open until the peer's first message
arrives on it. trackCollisionLoser takes over its recv goroutine, so a
message that is partly read is not cut, and answers that message as
OpenSent does (RFC 4271 Section 8.2.2): the message error, the OPEN
Message Error, the FSM Error, no answer to a version error, but a valid
OPEN gets Cease/Connection Collision Resolution. The BGP Identifiers
were compared at the OPEN on our connection; both OPENs of a real peer
carry the same one. What it sends is not counted for the session, which
runs on the other connection; the message it receives is still counted
in the session's received message counters, as recvMessageWithError()
counts every message it reads. The connection is also closed when the
peer closes it, when the OpenSent hold time expires and when the handler
stops.

twoByteAsTrans and isTreatAsWithdraw become atomic: the recv goroutine
of the losing connection outlives OpenSent and reads them in
recvMessageWithError, while stateChange() writes them under fsm.lock on
the way to Established.

TestFSMHandlerOpensent_OutgoingOpenFirst now expects the Cease on the
incoming connection after the peer's OPEN on it.

Fixes #3863

Notes for review:

  • The losing connection is kept for at most the OpenSent hold time (240 s) if the peer never sends its OPEN on it, and its goroutine is not part of the FSM's WaitGroup (it ends on its own when the handler stops).
  • twoByteAsTrans and isTreatAsWithdraw became atomic.Bool: stateChange() writes them on the way to Established while the losing connection's reader may read them. The race detector cannot show this through the socket, so there is no test for it.

Tests: collision_test.go (answer to each first message incl. bad peer AS, close only after the OPEN, close when the peer closes, handler stop, hold time; goroutine leaks checked with goleak) fails on master and passes with -race -count=3; TestFSMHandlerOpensent_OutgoingOpenFirst now expects the Cease after the peer's OPEN. Four pkg/server tests fail on my machine with and without this change (TestDisableMrt, TestEBGPRouteStuck, TestRTCDeferralTimerRaceCondition, TestTcpAoUpdatePeerWithClosedConnection, macOS).

Did you use an AI tool?

Yes. Claude Opus 5.5 helped write and review the change; the commit has an Assisted-by trailer. I have reviewed it and can answer questions about it.

@MaxRink
MaxRink force-pushed the collision-close-loser-at-open branch from 5703fa0 to 881cc87 Compare October 30, 2026 15:00
@MaxRink
MaxRink marked this pull request as ready for review October 30, 2026 15:00
@MaxRink
MaxRink force-pushed the collision-close-loser-at-open branch 2 times, most recently from d1ab39e to 80dbe27 Compare December 2, 2026 01:42
Copilot AI balanced review requested due to automatic review settings December 2, 2026 01:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The connection tracker resets the OpenSent hold timer instead of preserving its expiry, potentially keeping a silent incoming connection open for nearly twice the intended timeout.

1 open issue
What changed in this PR

Delays closing a losing incoming BGP connection until the peer’s first message arrives, addressing the FRR reconnection and extra session drop described in #3863.

Changes:

  • Transfers the losing connection’s reader to a background handler.
  • Makes shared parsing flags atomic.
  • Adds collision-response and cleanup tests and updates existing expectations.
File Description
pkg/​server/​fsm.go Defers collision-loser closure and synchronizes shared flags.
pkg/​server/​fsm_test.go Updates atomic flag assignments and collision timing expectations.
pkg/​server/​collision_test.go Tests delayed closure, message responses, and connection cleanup.

Review effort: Balanced

Comment thread pkg/server/fsm.go Resolved
Since b841cb7, when the peer's OPEN arrives on our outgoing connection
while the incoming connection is in OpenSent and has no OPEN yet, and we
have the higher BGP Identifier, opensent() sends Cease/Connection
Collision Resolution on the incoming connection at once and closes it.

RFC 4271 Section 6.8 detects a collision when an OPEN arrives:

  Upon receipt of an OPEN message, the local system MUST examine all of
  its connections that are in the OpenConfirm state.

The peer has not sent its OPEN on that connection yet, so it does not
see the close as the losing side of a collision. FRR connects again.
With graceful restart negotiated, the new connection arrives while the
session on our connection is Established and is handled as a restart of
the peer (RFC 4724 Section 4.2), so the session goes down a second time.
In a lab with FRR 10.x, every carrier fault caused two session drops
instead of one (confirmed with packet captures).

Keep the losing incoming connection open until the peer's first message
arrives on it. trackCollisionLoser takes over its recv goroutine, so a
message that is partly read is not cut, and answers that message as
OpenSent does (RFC 4271 Section 8.2.2): the message error, the OPEN
Message Error, the FSM Error, no answer to a version error, but a valid
OPEN gets Cease/Connection Collision Resolution. The BGP Identifiers
were compared at the OPEN on our connection; both OPENs of a real peer
carry the same one. What it sends is not counted for the session, which
runs on the other connection; the message it receives is still counted
in the session's received message counters, as recvMessageWithError()
counts every message it reads. The connection is also closed when the
peer closes it, when its OpenSent hold timer expires and when the handler
stops. The hold timer is the one opensent() started for the connection;
it is not restarted at the collision.

twoByteAsTrans and isTreatAsWithdraw become atomic: the recv goroutine
of the losing connection outlives OpenSent and reads them in
recvMessageWithError, while stateChange() writes them under fsm.lock on
the way to Established.

TestFSMHandlerOpensent_OutgoingOpenFirst now expects the Cease on the
incoming connection after the peer's OPEN on it.

Fixes osrg#3863

Signed-off-by: Maximilian Rink <maximilian.rink@telekom.de>
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
@MaxRink
MaxRink force-pushed the collision-close-loser-at-open branch from 80dbe27 to c591236 Compare December 2, 2026 15:51

This branch has not been deployed

No deployments
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.

Collision in OpenSent: the losing incoming connection is closed before the peer's OPEN, so FRR connects again

2 participants