Skip to content

libobs: Stop resetting encoder packet times under a running encoder (#3788) - #3791

Merged
zackees merged 2 commits into
masterfrom
fix/3788-encoder-packet-times-stop
Nov 7, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/3788-encoder-packet-times-stop

Conversation

@zackees

@zackees zackees commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #3788.

Cause

obs_encoder_stop() set encoder_packet_times.num = 0 under init_mutex, before it disconnected the encoder. do_encode() appends on the delivering thread and does not take init_mutex or any other lock for that array, so nothing ordered the two. A reset between the increment and the element read in da_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, so pts is 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 to cur_pts = 0 and before add_connection. No thread can be delivering there: a raw stop waits for the frame in video_output_disconnect (the input list lock is held across delivery), a GPU stop waits on gpu_encode_inactive, and init_mutex serializes 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.

  • RED (local Docker Tier 2, before the fix): stopping_one_of_two_outputs_keeps_the_array failed, and stopping_the_last_output_waits_for_the_frame failed with "from 3 to 0 during a frame". restarting_starts_with_an_empty_array passed, as a guard for the new reset.
  • GREEN: hosted Tier 2 and asan pass on this head.
  • Local ASan container loop on the fix, 40 rounds of this test and 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

  • TSan was not run on this change.
  • The failing ASan overflow was not reproduced on the old code, so the loop shows absence after the fix, not a before and after of that report.

test_pulse_stream_restart failed in the local RED run. It is in a different area and passes in hosted Tier 2.

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
zackees marked this pull request as ready for review November 7, 2026 01:38
@zackees
zackees force-pushed the fix/3788-encoder-packet-times-stop branch from 2d5253d to 9f8e31e Compare November 7, 2026 01:38
@zackees
zackees added this pull request to the merge queue Nov 7, 2026
Merged via the queue into master with commit 52bf4c7 Nov 7, 2026
43 checks passed
@zackees
zackees deleted the fix/3788-encoder-packet-times-stop branch November 7, 2026 02:14
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: encoder: ASan heap-buffer-overflow in local test_encoding_lag_rescale

1 participant