fix: skip RTCP sends when connection is closed or failed#246
Open
kingdomcoding wants to merge 1 commit intoelixir-webrtc:masterfrom
Open
fix: skip RTCP sends when connection is closed or failed#246kingdomcoding wants to merge 1 commit intoelixir-webrtc:masterfrom
kingdomcoding wants to merge 1 commit intoelixir-webrtc:masterfrom
Conversation
TWCC feedback, sender/receiver reports, and NACK timers are self-rescheduling and cannot be cancelled. When PeerConnection.close/1 is called, messages already queued in the mailbox for these timers still fire and attempt to send RTCP via DTLSTransport, which logs a debug warning because DTLS is no longer connected. Add guard clauses (matching the existing pattern used by send_rtp) to short-circuit these three handle_info handlers when conn_state is :closed or :failed, preventing spurious RTCP sends and the resulting "Attempted to send RTCP in wrong DTLS state" warning. Fixes elixir-webrtc#232
sgfn
reviewed
Mar 2, 2026
Comment on lines
+1682
to
+1685
| @impl true | ||
| def handle_info(:send_twcc_feedback, %{conn_state: conn_state} = state) | ||
| when conn_state in [:failed, :closed], | ||
| do: {:noreply, state} |
Member
There was a problem hiding this comment.
Let's keep the clause order consistent for all of the :send_ handlers -- the happy path with when conn_state not in [:failed, :closed] should be first
|
|
||
| @impl true | ||
| def handle_info(:send_twcc_feedback, %{twcc_recorder: twcc_recorder} = state) do | ||
| Process.send_after(self(), :send_twcc_feedback, @twcc_interval) |
Member
There was a problem hiding this comment.
Please add a comment in the new fallback clause that we're not scheduling any more TWCC feedback sends.
At this point, it shouldn't be an issue since conn_states :closed and :failed are permanent, but this seems like something that can easily be overlooked in the future were this to change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After calling
PeerConnection.close/1, the TWCC feedback, sender/receiver report, and NACK timers continue to fire because they are self-rescheduling and their already-queued mailbox messages cannot be cancelled. Each fired message attempts to callDTLSTransport.send_rtcp/2, which logs a debug warning since DTLS is no longer connected:This is a race condition between the DTLS/ICE teardown completing and in-flight timer messages being processed. Fixes #232.
Solution
Add guard clauses before each of the three affected
handle_infohandlers to short-circuit whenconn_stateis:closedor:failed. This matches the existing pattern already used by thesend_rtpcast handler:The three new guard clauses follow the same convention:
Testing
Manually verified by starting a WebRTC stream, stopping it, and confirming the debug message no longer appears in logs.