Repository navigation
libobs: Shut down encoders when an output stops before starting - #237
Merged
Merged
Conversation
Network outputs initialize their encoders in start() and connect on a separate thread. When the connection fails, the output reports it with obs_output_signal_stop() before data capture ever began, and libobs leaves the encoders initialized. A hardware encoder keeps its session (an NVENC session, for example) until the next successful start or until OBS exits. Add a cmocka test with a counting encoder and an output that fails the way rtmp_output does on a refused connection. It checks that no encoder instance is left after the failed start, also across repeated attempts, and after two outputs sharing the encoder have both failed. As controls, it checks that an encoder shared with a running output, or with an output that is still connecting, is left alone (the connecting output then starts and encodes normally), and that the encoder still starts normally after a failed start. obs_startup() needs a display on Linux, so the test skips without one (run it under xvfb-run). Reported upstream as obsproject/obs-studio#10651 by tt2468. Refs obs-rust#236. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Network outputs such as rtmp_output initialize their encoders in start() and connect on a separate thread. When the connection fails, the output calls obs_output_signal_stop() before data capture began, and obs_output_end_data_capture() returns early because the output is not active. The encoders it initialized were never shut down, so a hardware encoder kept its session (an NVENC session, for example) until the next successful start or until OBS exited. Shut those encoders down on that path, unless the encoder is still needed: it is running, or another output that uses it is active, reconnecting, delaying or still starting. To tell that an output is starting, outputs now have a starting flag, set before start() and cleared when data capture begins, when start() fails, and on every stop path. Without it, a second stream sharing the encoder that is still connecting would lose its encoder when the first one fails. The shutdown runs on the output's end-capture thread, as a normal stop does, so it waits on the video mixes and graphics locks in the same context; the next start and destroy join it as they already do. The stopping thread only creates it and never waits (if an end-capture thread is still running, the encoders are left to the next stop). On that thread the encoder's outputs_mutex and init_mutex are only tried: full_stop() holds outputs_mutex while force-stopping outputs, and a forced stop can join the thread. If either is busy the encoder stays initialized, as before, until the next normal stop. Failed reconnect attempts do not take this path; the encoders are shut down once reconnecting gives up. Upstream report: obsproject/obs-studio#10651, by tt2468. Fixes obs-rust#236. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=c383b75fa05e8cffbcc59427fc7c67c730acd365 secs=123 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791727567,"gate":"rust/all/fmt","host":"linux-x86_64","key":"a5418496cd7063f42c13101335cf6314600ca8fbfa8a1982305482e0e1bbad0f","lane":"lint","parents":["250c2e59346d9dd01b5fb406d001ac2141bdd38b"],"secs":50,"stamp":"8a953bb10b8159f1eca2a91e855e9e84","tree":"c383b75fa05e8cffbcc59427fc7c67c730acd365","v":1,"via":"run"} Ci-Attestation: {"at":1791727567,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"a5418496cd7063f42c13101335cf6314600ca8fbfa8a1982305482e0e1bbad0f","lane":"lint","parents":["250c2e59346d9dd01b5fb406d001ac2141bdd38b"],"secs":50,"stamp":"a49c687634fe4391da12de44d258bb43","tree":"c383b75fa05e8cffbcc59427fc7c67c730acd365","v":1,"via":"run"} Ci-Attestation: {"at":1791727567,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"09f8f4d408fc63b8c50b7106ddee0a36fddc247f3c2a8b0a3a76b221345b8e3e","lane":"rust-test","parents":["250c2e59346d9dd01b5fb406d001ac2141bdd38b"],"secs":29,"stamp":"7370e5481ed23a3ea073583a3e0b09f1","tree":"c383b75fa05e8cffbcc59427fc7c67c730acd365","v":1,"via":"run"} Ci-Attestation: {"at":1791727567,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"0757d12c2c08684283c901c2e9b7cfb61a092296a06b835c028f3369d218635a","lane":"tier2-linux","parents":["250c2e59346d9dd01b5fb406d001ac2141bdd38b"],"secs":44,"stamp":"5158b14f26a53320b5c6ac69a6de2a3a","tree":"c383b75fa05e8cffbcc59427fc7c67c730acd365","v":1,"via":"run"}
Member
|
Verified locally, rebased onto master and stamped. The test skips without a display, so I ran it with
As with #207 and #219, CI has no display, so this test only guards anything where Xvfb runs. Merging once CI is green. |
zackees
force-pushed
the
fix/236-encoders-failed-start
branch
from
October 11, 2026 14:06
f8b571a to
7041590
Compare
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.
Description
When a stream failed to start (for example the server refused the connection), the encoders it had initialized stayed initialized, and a hardware encoder kept its session (an NVENC session, for example) until the next successful start or until OBS exited. With this change the encoders are shut down when the failed start is reported.
The cause: network outputs such as
rtmp_outputinitialize their encoders instart()and connect on a separate thread (plugins/obs-outputs/rtmp-stream.c:1546). When the connection fails, that thread callsobs_output_signal_stop()before data capture began, andobs_output_end_data_capture_internal()returns early because the output is not active (libobs/obs-output.c:2885). The encoders are only stopped, and so shut down, on the normal path throughend_data_capture_thread(), which this early return skips.test: Encoders must shut down after a failed output start:test_output_failed_start_encodersregisters a counting encoder, an output whosestart()initializes its encoders and then fails from a separate thread withobs_output_signal_stop()(the wayrtmp_outputdoes on a refused connection), and an output that stays "connecting" until the test lets it connect or fail. 3 of 6 fail on this commit: a single failed start, repeated failed starts, and two outputs sharing the encoder that both fail (shared_encoder_shut_down_after_all_fail) all leave an encoder instance behind. The controls pass:shared_running_encoder_is_kept,connecting_shared_encoder_is_kept(a second output sharing the encoder is still connecting when the first fails; it must keep the encoder, then connect and encode normally) andencoder_still_starts_after_failed_start. It needs a display forobs_startup()and skips without one; it is not built on Windows and skips on macOS (it suppliesobs_get_video_info()itself, which only works on ELF platforms).libobs: Shut down encoders when an output stops before starting: on that early-return path, when the output stopped before it became active, its encoders are shut down unless still needed: the encoder is running, or an output using it is active, reconnecting, delaying or starting. Outputs get astartingflag for the last case (set beforestart(), cleared once data capture begins, whenstart()fails and on every stop path), so a second stream sharing the encoder that is still connecting keeps it. The shutdown runs on the output's existing end-capture thread, the context in which a normal stop already shuts encoders down; the next start and destroy join it as before, and the stopping thread only creates it and never waits (if an end-capture thread is still running, the encoders are left to the next stop). On that thread the encoder'soutputs_mutex, theninit_mutex, are only tried, sincefull_stop()holdsoutputs_mutexwhile force-stopping outputs; if one is busy the encoder stays initialized as before. Failed reconnect attempts do not take this path. The path for an output that did start is unchanged. All 6 tests pass.Motivation and Context
Fixes #236 (upstream obsproject/obs-studio#10651, reported by tt2468)
Why this one: a failed stream start is common (wrong server, network down, server refusing the key), and users often retry. Each failure left an NVENC session open, and consumer NVIDIA drivers cap concurrent NVENC sessions (NVIDIA's support matrix, "Max # of concurrent sessions"), so leaked sessions can make a later recording or stream fail to start its encoder. The fix stays in libobs, on a path that only runs when an output stops before it ever started.
How Has This Been Tested?
Automated:
test_output_failed_start_encodersfails 3 of 6 at the RED commit and passes 6 of 6 at the GREEN commit, and 10 of 10 repeated runs at GREEN pass. An earlier version of the fix without thestartingguard failedconnecting_shared_encoder_is_kept, so the test covers the shared-encoder case. The full cmocka suite passes at GREEN (79 of 79 under ctest). All runs used a private Xvfb with software GL. clang-format and gersemi are clean.Hardware: on an RTX 2070 desktop, a headless libobs program started the real
rtmp_outputwith the obs-nvenc H.264 encoder against a refused local port five times, then streamed to a listening local server as a control, counting NVENC sessions after each step. Without the fix, one NVENC session stayed open after every failed start, until the control stream stopped. With the fix (this branch), no session stayed open after any failed start; the control stream opened one session and closed it on stop.Not tested:
full_stop()) while an output sharing it is still connecting, and the skip when a lock is busy; neither is exercised by the test.ENABLE_RUST_LIBOBS=ON) and the Docker lanes of the local gate.Types of changes
Checklist:
🤖 Generated with Claude Code