Skip to content

libobs: Shut down encoders when an output stops before starting - #237

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/236-encoders-failed-start
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/236-encoders-failed-start

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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_output initialize their encoders in start() and connect on a separate thread (plugins/obs-outputs/rtmp-stream.c:1546). When the connection fails, that thread calls obs_output_signal_stop() before data capture began, and obs_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 through end_data_capture_thread(), which this early return skips.

  1. test: Encoders must shut down after a failed output start: test_output_failed_start_encoders registers a counting encoder, an output whose start() initializes its encoders and then fails from a separate thread with obs_output_signal_stop() (the way rtmp_output does 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) and encoder_still_starts_after_failed_start. It needs a display for obs_startup() and skips without one; it is not built on Windows and skips on macOS (it supplies obs_get_video_info() itself, which only works on ELF platforms).
  2. 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 a starting flag for the last case (set before start(), cleared once data capture begins, when start() 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's outputs_mutex, then init_mutex, are only tried, since full_stop() holds outputs_mutex while 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_encoders fails 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 the starting guard failed connecting_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_output with 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:

  • The full frontend, and other network outputs such as WHIP; only RTMP was run.
  • An encoder failing to encode (full_stop()) while an output sharing it is still connecting, and the skip when a lock is busy; neither is exercised by the test.
  • The stream-delay start path with a shared encoder.
  • The NVENC texture (GPU) path: under Xvfb the encoder fell back to its non-texture path.
  • Windows and macOS.
  • The Rust lane (ENABLE_RUST_LIBOBS=ON) and the Docker lanes of the local gate.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

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]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 13:57
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"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The test skips without a display, so I ran it with xvfb-run ctest on each commit's Tier 2 build:

  • RED (250c2e593): 3 of 6 passed (the controls), with ENABLE_RUST_LIBOBS OFF and ON alike. failed_start_shuts_down_encoders, repeated_failed_starts_leave_nothing and shared_encoder_shut_down_after_all_fail failed (attempt 1 left 1 encoder instance(s) (expected 0)).
  • GREEN (f8b571a3e): 6 of 6 passed OFF and 6 of 6 ON. Plain Tier 2 passed 79 of 79 OFF and 79 of 79 ON, and the local gate passed.

As with #207 and #219, CI has no display, so this test only guards anything where Xvfb runs. Merging once CI is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs: encoders (and their NVENC session) stay initialized after a stream fails to start (upstream #10651)

2 participants