Skip to content

Connecting sockets could be reported as SRTS_BROKEN - #3438

Open
maxolgi wants to merge 12 commits into
Haivision:devfrom
maxolgi:fix-connect-status
Open

maxolgi wants to merge 12 commits into
Haivision:devfrom
maxolgi:fix-connect-status

Conversation

@maxolgi

@maxolgi maxolgi commented Nov 13, 2026 •

Copy link
Copy Markdown

The status was lying.

A socket could be reported BROKEN while its handshake was still completing.
Three separate holes made that possible:

getStatus() synthesized SRTS_BROKEN whenever it saw SRTS_CONNECTING on a
socket whose core state was neither CONNECTING nor CONNECTED. That state
is not an error. It exists for a moment during every normal CONNECTING to
CONNECTED transition, so whether a poller sees it is pure timing.

postConnect made it worse: it stamped the core state SSS_INIT in the middle
of completing the connection, so the ambiguous pair was guaranteed to be
published, not just possible.

The blocking connect failure path stored SRTS_OPENED over whatever
terminal status was already stored, so a failed connect could end up
looking like it was never attempted.

The synthesis is gone. postConnect no longer leaves the connecting state
mid-completion. A failed blocking connect publishes its terminal status
before returning instead of overwriting it.

Every connect that started and failed now ends BROKEN - blocking or not,
rejection, timeout, rendezvous, all of it. Previously a blocking timeout
ended OPENED while non-blocking ended BROKEN. Failures from before the
process started keep OPENED and the socket reusable.

New test_sockstate_race.cpp runs a status poller against connecting sockets
throughout. test_enforced_encryption needed small adjustments because the
removed synthesis was hiding real states from it.

Fixes #3435

Comment thread srtcore/api.cpp
// Do not clobber that terminal publication with the pre-terminal
// SRTS_OPENED - revert to OPENED only if the status is still the
// pre-terminal SRTS_CONNECTING set above.
s->m_Status.compare_exchange(SRTS_CONNECTING, SRTS_OPENED);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If this is the situation, then it's completely wrong elsewhere.

The so far situation was that a caller socket gets into connecting state as initiated by startConnect, and may succeed, and resolve to CONNECTED, or fail, although the logic is not exactly consistent here (it's derived from the original UDT and was never changed - but the dev version is open for changes here). The so far situation is that the socket, in case of failure, remains in CONNECTING state, however the connection error is reported by:

  • The error result from srt_connect call, in case od blocking mode
  • The SRT_EPOLL_ERR flag set in the epoll set for that socket, in case of nonblocking mode

I can agree that CONNECTING state is not logical here, we can chance that to BROKEN, but then this must be done absolutely consistently. Important to know is that a caller socket that was used for connecting and this process has returned with failure, the socket is not usable anymore. So whether it's CONNECTING or BROKEN it doesn't really matter, just note that normally BROKEN meant that the socket was once successfully connected, just the connection was then terminated. Still, SRT has also introduced the term of "late rejection", in which it is possible for a short time for a socket to be connected and then get broken; for an average user there's not much of a difference if the socket was broken since the beginning, or was it connected for at least a short time. So the resolution that a socket that failed to connect resolves to BROKEN state is perfectly acceptable - the only rule that must be preserved is that this is happening in every case when the socket failed to connect. Note also that this happens only in case when the errors have happened during the connecting process, not when the process didn't even start because of other errors, in which case the socket shouldn't even transit to CONNECTING state, and should be also still reusable despite the error.

@maxolgi maxolgi Nov 18, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 680a2bb.

Two paths were broken: a blocking timeout ended OPENED while the same timeout non-blocking ended BROKEN — same failure, different result. And a rendezvous rejection could return before the worker stored anything. The wait loop in startConnect now stores BROKEN before the exception unwinds, so this compare_exchange only fires for failures before the process started. Those keep OPENED and the socket stays reusable.

Close-during-connect is exempt — it keeps CLOSING/CLOSED. A close is not a connect failure.

BlockingLoop checks strictly at return, no settling. Fails every iteration without the fix. The RDV test samples at return, in both threads.

Comment thread srtcore/core.cpp Outdated
// pair with a still-SRTS_CONNECTING status for no benefit. A socket
// removed in the meantime (locateSocket returns NULL) gets the
// SSS_INIT stamp on that failure path below, for closeEntity and
// the GC bookkeeping.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I get it, but this is now a historical note that is not necessary here, so please remove this, together with the above comment. If any of this is necessary to note, leave it at the place where you have moved the INIT state setting.

@maxolgi maxolgi Nov 18, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 27e9ad6 and ac81221. The notes state the invariant now, not the history. Same treatment for the copies in updateConnStatus and getStatus.

@ethouris ethouris added this to the v1.6.0 milestone Nov 18, 2026
@ethouris ethouris added Type: Bug Indicates an unexpected problem or unintended behavior [core] Area: Changes in SRT library core labels Nov 18, 2026
maxolgi added a commit to maxolgi/srt that referenced this pull request Nov 18, 2026
Per review on Haivision#3438: the NOTE block in postConnect, the comparative note
at its SSS_INIT stamp, the parenthetical in CRcvQueue::updateConnStatus
and the preamble on CUDTSocket::getStatus() told the story of this
series' own changes instead of the invariant. Keep only what is true
now, at the site that owns it; the history stays in the commit
messages.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
maxolgi added a commit to maxolgi/srt that referenced this pull request Nov 18, 2026
Per review on Haivision#3438: a caller socket that entered the connecting
process and failed must report SRTS_BROKEN in every case. Two paths
still violated the rule:

- A blocking connect that timed out exited its wait loop through the
  same catch as a rejection, but nothing had stored the terminal
  status on that path, so the revert of SRTS_CONNECTING to
  SRTS_OPENED in CUDTUnited::connectIn() succeeded and the socket
  ended SRTS_OPENED - while a non-blocking timeout resolves to
  SRTS_BROKEN through CRcvQueue::updateConnStatus. Same failure,
  different terminal status depending on the blocking mode.

- A blocking rendezvous rejection woke the blocked caller without
  publishing the terminal status first (the pre-wake store from the
  previous commit covers only the caller-listener rejection branch),
  so the status at srt_connect() return was decided by the race
  between the caller's revert and the worker's store.

The wait loop's catch now stores SRTS_BROKEN itself, before the
exception unwinds into connectIn(): every failure that exits the
blocking wait is published by the very thread that returns from
srt_connect(), and the CONNECTING->OPENED revert in connectIn()
therefore only ever succeeds for a connect that failed before it
started - which keeps that socket reusable, per the same review. A
socket closed on request while connecting is exempt: it keeps the
status its close path publishes.

Tests: TestConnectionTimeout.BlockingLoop checks the caller state
strictly, with no settling, right after a timed-out blocking
srt_connect(), and the Nonblocking case pins the worker-side
publication. BlockingRendezvousRejectionResolvesBroken samples the
state inside the connecting threads at return time. Proven
falsifiable: without this commit BlockingLoop fails on every
iteration, and with the worker's BROKEN store artificially delayed by
50 ms the rendezvous test fails 5/5 while it stays green with the
fix; the organic window was too narrow to hit on this machine. Full
suite: 344 cases pass.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
…/status pair

CUDTSocket::getStatus() mapped the pair (m_State outside
{CONNECTING, CONNECTED, BROKEN}, m_Status == SRTS_CONNECTING) to
SRTS_BROKEN. The two fields are separate atomics whose transition
stores happen one after the other (CUDTUnited::connect stores the
status before CUDT::startConnect stores the state; CUDT::postConnect
stamps SSS_INIT before the final SSS_CONNECTED), so a concurrently
polling reader can catch the cross-transition combination and see a
connection that is only starting or finishing its handshake reported
as broken. The QuicSRT gateway polls srt_getsockstate() for every
session on its ~2 ms registry tick and intermittently reaped freshly
connected initiator sessions this way: the reap closed the socket and
disposed its now-empty multiplexer (the receive worker exits, the
socket goes silent), and the healthy peer expired on its 5 s
peer-idle timeout.

Report the status as-is (only SSS_BROKEN still maps to SRTS_BROKEN),
and store SRTS_BROKEN explicitly in the async-connect failure
completion (CRcvQueue::updateConnStatus), which used to rely on the
synthesis to make failed connectors visible; that path also keeps its
SSS_INIT stamp for closeEntity and the GC.

Regression test: ConnectingStateNeverFalselyBroken in
test_sockstate_race - many asynchronous loopback connects sampled like
the gateway tick under global-lock churn fail deterministically
without the fix and pass with it.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
The first commit of this series removed the SRTS_BROKEN synthesis from CUDTSocket::getStatus(),
but two comments in CUDT::postConnect still described the deleted
mapping as the reason the state stays SSS_CONNECTING (and promised a
BROKEN report that the new getStatus no longer produces). State the
actual invariant instead: the state/status pair must read consistently
as "still connecting" for concurrent srt_getsockstate() samplers.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
…cking connect

After a rejected connect the terminal status is published by the queue thread (the explicit SRTS_BROKEN store in CRcvQueue::updateConnStatus) while the blocked connect call returns on the application thread, so on a slow or loaded machine (the QEMU-emulated s390x CI) srt_getsockstate() can still report the pre-terminal state here. Wait for the state to settle with a bounded poll before asserting, the same settle pattern the late-rejection check in the accept thread uses.

Upstream's s390x leg runs this very test green at this series' base (d5c97f9), so the visible window is exposed by this series: the removed read-time BROKEN synthesis used to report the terminal state synchronously, while the explicit publication settles asynchronously. The settle-wait keeps the test correct against the asynchronous publication.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
A failed blocking srt_connect() could return while the terminal socket
status was not yet published, or with it clobbered back to a
pre-terminal value. On the rejection path the receiver queue worker
woke the blocked caller (notifyBlockingConnect() in
CUDT::processAsyncConnectRequest and right after CUDT::postConnect)
BEFORE storing SRTS_BROKEN, which only happened later in
CRcvQueue::updateConnStatus when the failed connector was removed.
The woken application thread then threw out of startConnect() and the
catch in CUDTUnited::connectIn unconditionally stored SRTS_OPENED, so
the last writer decided between SRTS_OPENED and SRTS_BROKEN - the
BROKEN-vs-OPENED nondeterminism test_enforced_encryption hits on the
QEMU-slowed s390x CI leg (CASE_AA_2_Blocking). The removed read-time
BROKEN synthesis in CUDTSocket::getStatus() used to mask this by
reporting the terminal state synchronously at read time.

The worker now stores SRTS_BROKEN on the socket before every wake of
a blocked connect on the rejection path, and the catch in connectIn
only reverts SRTS_CONNECTING to SRTS_OPENED via compare_exchange, so
it can no longer clobber a terminal BROKEN already published by the
worker. SRTS_OPENED survives only for local failures with no terminal
publication (e.g. a connect timeout). A rejected blocking connect is
now deterministically SRTS_BROKEN by the time srt_connect() returns,
matching the success path, which publishes SRTS_CONNECTED before the
core state the blocked caller polls.

The regression test checks the caller state strictly, with no
settling, right after a failed blocking srt_connect(), and the
expectation table pins SRTS_BROKEN for those cases; the settle-wait
added earlier in this series stays as belt-and-braces for pathological scheduling.
Proven falsifiable: against the pre-fix core with an artificially
widened worker window (50ms sleep before the BROKEN store) the new
strict check fails 200/200 repeats while the settle-waited check
stays green, i.e. it catches exactly the window the settle-wait
masks; with the fix the same loop is green (VT and stock builds).

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
Per review on Haivision#3438: the NOTE block in postConnect, the comparative note
at its SSS_INIT stamp, the parenthetical in CRcvQueue::updateConnStatus
and the preamble on CUDTSocket::getStatus() told the story of this
series' own changes instead of the invariant. Keep only what is true
now, at the site that owns it; the history stays in the commit
messages.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
Per review on Haivision#3438: a caller socket that entered the connecting
process and failed must report SRTS_BROKEN in every case. Two paths
still violated the rule:

- A blocking connect that timed out exited its wait loop through the
  same catch as a rejection, but nothing had stored the terminal
  status on that path, so the revert of SRTS_CONNECTING to
  SRTS_OPENED in CUDTUnited::connectIn() succeeded and the socket
  ended SRTS_OPENED - while a non-blocking timeout resolves to
  SRTS_BROKEN through CRcvQueue::updateConnStatus. Same failure,
  different terminal status depending on the blocking mode.

- A blocking rendezvous rejection woke the blocked caller without
  publishing the terminal status first (the pre-wake store from the
  previous commit covers only the caller-listener rejection branch),
  so the status at srt_connect() return was decided by the race
  between the caller's revert and the worker's store.

The wait loop's catch now stores SRTS_BROKEN itself, before the
exception unwinds into connectIn(): every failure that exits the
blocking wait is published by the very thread that returns from
srt_connect(), and the CONNECTING->OPENED revert in connectIn()
therefore only ever succeeds for a connect that failed before it
started - which keeps that socket reusable, per the same review. A
socket closed on request while connecting is exempt: it keeps the
status its close path publishes.

Tests: TestConnectionTimeout.BlockingLoop checks the caller state
strictly, with no settling, right after a timed-out blocking
srt_connect(), and the Nonblocking case pins the worker-side
publication. BlockingRendezvousRejectionResolvesBroken samples the
state inside the connecting threads at return time. Proven
falsifiable: without this commit BlockingLoop fails on every
iteration, and with the worker's BROKEN store artificially delayed by
50 ms the rendezvous test fails 5/5 while it stays green with the
fix; the organic window was too narrow to hit on this machine. Full
suite: 344 cases pass.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
…us note

The sentence referenced the pre-atomic connection-state flag, a
representation that no longer exists; the note now states only what
the code does.

Signed-off-by: maxolgi <maxolgi@users.noreply.github.com>
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.22222% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.96%. Comparing base (679f83a) to head (34af4a8).
⚠️ Report is 407 commits behind head on dev.

Files with missing lines Patch % Lines
test/test_sockstate_race.cpp 83.13% 5 Missing and 9 partials ⚠️
srtcore/api.cpp 80.00% 1 Missing ⚠️
srtcore/core.cpp 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              dev    #3438       +/-   ##
===========================================
- Coverage   71.12%   58.96%   -12.16%     
===========================================
  Files         101      121       +20     
  Lines       19776    28571     +8795     
  Branches        0     4340     +4340     
===========================================
+ Hits        14065    16848     +2783     
- Misses       5711    10047     +4336     
- Partials        0     1676     +1676     

☔ 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.

State only what the code does: where SRTS_BROKEN is stored, what the
SRTS_OPENED revert covers, and that no status is derived from the
state/status pair. Comment-only change.

Signed-off-by: maxolgi <maxolgi@proton.me>
…tests

The strict check already ran before the settle loop, so the loop could
not turn a failing run into a passing one. The notes that described the
removed status synthesis now say what the tests rely on instead.

Signed-off-by: maxolgi <maxolgi@proton.me>
A connection attempt that started and failed leaves the socket
SRTS_BROKEN, blocking or not. The status table and the two passages
that still described SRTS_CONNECTING for that case now say so.

Signed-off-by: maxolgi <maxolgi@proton.me>
…-ups

The wait loop in CUDT::startConnect() stores SRTS_BROKEN before its
exception unwinds, so a blocking connect publishes its terminal status
on the thread that returns from srt_connect(). The stores that the
receiver worker made before waking that thread no longer decide the
status, and every asynchronous failure is published by
CRcvQueue::updateConnStatus(). Two owners remain: the application
thread for the blocking mode, the worker for the non-blocking one.

The store in CUDT::processConnectResponse() also fired on the
CONN_REJECT that postConnect() returns for a socket removed while
connecting (SRT_REJ_CLOSE), stamping SRTS_BROKEN over the status that
socket's close publishes. With both gone, a socket closed on request
keeps CLOSING/CLOSED in either mode.

Tests: TestConnectionTimeout.BlockingLoop and
TestSockstateRace.BlockingRendezvousRejectionResolvesBroken sample the
status right after srt_connect() returns; the blocking CASE_* of
TestEnforcedEncryption check it once the call has failed. All pass
unchanged, and TestConnectionTimeout.Nonblocking still finds
SRTS_BROKEN after epoll has reported the error.

Signed-off-by: maxolgi <maxolgi@proton.me>
…zvousHangs

srt_getsockstate() reports the status as stored, so SRTS_CONNECTING is
visible as soon as connectIn() stores it under m_ControlLock, before
the core leaves SSS_INIT. The close thread's wait therefore only orders
srt_close() behind that lock; CUDTUnited::close() supplies the rest by
re-checking the core state for as long as the lock is busy.
Comment-only change.

Signed-off-by: maxolgi <maxolgi@proton.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] srt_getsockstate() reports SRTS_BROKEN while a non-blocking connect is succeeding (regression of #508)

2 participants