rtmp/rtsp: unblock a sender stuck in a socket write on disconnect - #2195
Merged
pedroSG94 merged 1 commit intoSep 11, 2026
Merged
Conversation
With TCP backpressure (server ACKs not arriving, send window full) the sender blocks in a java.io socket write, which ignores coroutine cancellation. disconnect() stopped the sender first (BaseSender.stop -> job.cancelAndJoin) and closed the socket only afterwards, so the join waited until the network recovered or TCP gave up. A reConnect hung for the whole time. Try a cooperative stop first (keeps the graceful close in the normal case) and, if the sender does not stop within 1 s, close the socket to unblock the write and stop again. RTSP over TCP (interleaved) writes to the same socket and has the same issue. SRT/UDP are not affected (UDP sends do not block on ACKs).
Owner
|
Hello, Thank you for the fix. This is equivalent but avoid repeat code in the future if other protocol need control it. |
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.
rtmp/rtsp: unblock a sender stuck in a socket write on disconnect
Problem
With TCP backpressure (server ACKs no longer arriving, send window full),
RtmpSenderblocks in a plainjava.iosocket write. AreConnect()(or a userdisconnect()) then hangs until the network recovers or TCP gives up, which can take minutes.Thread dump taken during the hang (two dumps, 12 s apart, identical):
Why it deadlocks:
RtmpClient.disconnect()callsrtmpSender.stop()first, andBaseSender.stop()ends withjob?.cancelAndJoin().java.net.Socketwrite never checks it.Thread.interrupt()would not help either, andjava.net.Sockethas no send timeout.closeConnection()runs afterstop(), so the join waits forever.Fix
FCUnpublish/deleteStream, RTSPTEARDOWN).TcpStreamSocketJavaBase.close()only doesshutdownOutput/shutdownInput/close, so it does not take the stream lock the writer holds.stop()has already setrunning = falseand reset the packets. The secondstop()completes the join and clears the queue.sendCloseafterwards fails fast and is already wrapped inrunCatching.onConnectionFailed: the woken writer'sonMainThreadruns in an already cancelled job, so it throwsCancellationExceptionwithout running the callback.RtpSocketTcp) writes to the same socket and gets the same change.Test
Android emulator (root image), RTMP to MediaMTX, backpressure produced with
iptables -A INPUT -p tcp --sport 1935 -j DROP. The app requests areConnectafter detecting the stall.reConnect→onConnectionStartedsender blocked in socket write, closing socket to unblock it;onConnectionStartedfollows 3.8 s after the failure (1 s stop timeout + 1.5 s retry delay). Retries then continue every ~7 s (connect timeout) and reconnect about 3 s after the backpressure is lifted.Normal stop/start and reconnect without backpressure: the new path is not taken (the warning appears exactly once in the run, only during the backpressure). The user stop at the end still closes gracefully (server log:
closed: EOF).Limitations
SSLSocket.close()sendsclose_notifyand could itself block under the same backpressure. It is not covered here and was not verified. Plainrtmp://and RTSP over TCP are covered.