Repository navigation
libobs: Stop resetting encoder packet times under a running encoder (#3788) - #3791
Merged
Merged
Conversation
obs_encoder_stop() resets encoder_packet_times from the stopping thread while do_encode() appends on the delivering thread with no shared lock. Refs #3788
obs_encoder_stop() set encoder_packet_times.num to 0 before disconnecting, under init_mutex, while do_encode() appends on the delivering thread with no lock. Between the increment and the read in da_push_back_new() that gives element -1, the reported heap-buffer-overflow. A stop that was not the last also dropped entries the running encoder still needed. Reset on the first start instead, next to cur_pts and before add_connection, where no thread can be delivering: a stop waits for the frame in video_output_disconnect() or stop_gpu_encode(), and start and stop are serialized by init_mutex. Fixes #3788
zackees
marked this pull request as ready for review
November 7, 2026 01:38
zackees
force-pushed
the
fix/3788-encoder-packet-times-stop
branch
from
November 7, 2026 01:38
2d5253d to
9f8e31e
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.
Fixes #3788.
Cause
obs_encoder_stop()setencoder_packet_times.num = 0underinit_mutex, before it disconnected the encoder.do_encode()appends on the delivering thread and does not takeinit_mutexor any other lock for that array, so nothing ordered the two. A reset between the increment and the element read inda_push_back_new()gives element -1.The report's numbers fit that: the element is 40 bytes, and a 2592-byte block is capacity 64 (2560) plus
bmalloc's 32 bytes of alignment padding, which puts the array at block + 32. Element -1 starts at array - 40, which is block - 8, the reported address. Its later fields fall inside the padding, soptsis the only write ASan can flag. This is arithmetic from the report; the overflow itself was not reproduced on demand.A stop that was not the last had a second problem: it dropped entries the running encoder still needed.
Fix
Reset in the first-start branch of
obs_encoder_start_internal, next tocur_pts = 0and beforeadd_connection. No thread can be delivering there: a raw stop waits for the frame invideo_output_disconnect(the input list lock is held across delivery), a GPU stop waits ongpu_encode_inactive, andinit_mutexserializes start and stop. The shared C code means both C reference and hybrid builds take the same fix. No sanitizer known-list entry.Tests
New
test_encoder_stop_packet_times; existing C tests are unchanged. It pins the stopping thread's write as seen from the encode callback, rather than the few-instruction overflow window.stopping_one_of_two_outputs_keeps_the_arrayfailed, andstopping_the_last_output_waits_for_the_framefailed with "from 3 to 0 during a frame".restarting_starts_with_an_empty_arraypassed, as a guard for the new reset.test_encoding_lag_rescale: all cases passed with no heap-buffer-overflow. Every ctest round still exited non-zero on LeakSanitizer leaks (see the PR comments for details).Not verified
test_pulse_stream_restartfailed in the local RED run. It is in a different area and passes in hosted Tier 2.