Skip to content

libobs: Preserve configuration files on flush failure - #35

Closed
Mquarmoc wants to merge 1 commit into
obs-rust:masterfrom
Mquarmoc:fix/config-save-flush
Closed

Mquarmoc wants to merge 1 commit into
obs-rust:masterfrom
Mquarmoc:fix/config-save-flush

Conversation

@Mquarmoc

@Mquarmoc Mquarmoc commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

Check the fclose result in config_save so config_save_safe does not replace the original after a failed buffered write. Also let an empty configuration save successfully by skipping the zero-length fwrite.

Motivation and Context

On a full disk, a small fwrite can succeed in the stdio buffer while fclose fails. The previous code reported success and allowed the temporary file to replace valid settings. Three new C tests cover direct flush errors, preservation of the original and backup on a safe-save error, and empty direct/safe saves.

How Has This Been Tested?

The new failure cases were run against the original implementation and failed before the fix. The complete repository ./lint and ./unittest commands pass independently on this branch (Ubuntu 24.04 WSL host, Ubuntu 26.04 validation container):

  • clang-format 22.1.3, gersemi 0.25.0, Rust formatting and clippy.
  • Rust workspace unit, layout and C differential tests.
  • All seven C test executables with ENABLE_RUST_LIBOBS=OFF and ON.
  • DeckLink build in both configurations; identical libobs exports (1,752 symbols).

The new filesystem failure tests are Linux-only because they use /dev/full. Windows/macOS and an interactive OBS session were not run locally. These fixes detect buffered write errors; they do not add power-loss durability.

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 (no public API or usage change).
  • I have used AI tooling in the creation of this PR.
  • I am an AI agent assisting with this pull request: Codex (GPT-6), responsible for the scoped audit, implementation, regression tests, code review and local validation.

A buffered fwrite can succeed while fclose fails on a full disk.
Report the close error before config_save_safe replaces the original.
Allow saving an empty configuration as well.

Add Linux /dev/full regressions for direct and safe saves.
@zackees

zackees commented Oct 9, 2026

Copy link
Copy Markdown
Member

Rebased onto current master as a RED → GREEN pair in #70 (your authorship kept via Co-Authored-By). The one addition: test_config_file's characterized empty-save CONFIG_ERROR is updated to the fixed behavior. This PR can close once #70 lands.

@zackees zackees closed this in eb60963 Oct 10, 2026
zackees added a commit to nathan-v/obs-studio that referenced this pull request Oct 10, 2026
Write configuration files to /dev/full, where small writes succeed in
the stdio buffer and fail at fclose: config_save must report the error,
config_save_safe must keep the original and not rotate a backup, and an
empty configuration must still save directly and safely. test_config_file
pinned the empty-save CONFIG_ERROR as characterized, not endorsed; it now
expects CONFIG_SUCCESS.

Rebased from obs-rust#35 onto current master.

Co-Authored-By: Mquarmoc <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit that referenced this pull request Oct 12, 2026
open() spawns a real audio thread on both sides, whose ~21 ms worker
ticks race the driver's explicit oracle_audio_tick/tick_for_test calls
and the shared TICK recorder. Review comment on fork PR #35:
- DRIVER mutex held for every test body so parallel #[test]s cannot
  interleave into TICK/ORDER.
- Every recorded action now gates on the timestamp: input callbacks
  already filtered start_ts==MAGIC, but output callbacks pushed into
  TICK whenever it was Some; record_output and output_full_* now also
  require data.timestamp in [MAGIC, MAGIC+1e9), which real worker
  ticks (os_gettime_ns ~1e18) never enter.
No production-code change; all 4 tests still pass.
zackees pushed a commit that referenced this pull request Oct 12, 2026
Deterministic C-vs-Rust regressions (fork PR #35 acceptance):
- self_disconnect_parity: oracle removes only itself, the port's
  iter_mut().rev() drops the whole tail — 1 vs 0 inputs left.
- earlier_input_removal_parity: [A,B,C] with B removing A => order
  [3,2,2] (shifted element re-invoked) — locks the index-read
  semantics in for the rewrite.
- vec_growth_parity: a callback connects a 5th input; the oracle's
  fixed bound yields [4,3,2,1], the port yields [4,3,2].
- concurrent_connect_disconnect_parity: churn from another thread
  across 10 driven ticks + quiesced final-tick order.
- audio_io_core.rs call sites move to the new safe API (connect takes
  Option<&audio_convert_info>, disconnect is &self) — fails to compile
  on the current unsafe-FFI-shaped core: RED.
zackees pushed a commit that referenced this pull request Oct 12, 2026
Fork PR #35 review: the worker held &mut AudioOutput for its whole life
while connect/disconnect shims minted another &mut — aliasing UB ahead of
the lock. And do_audio_output held mix.inputs.iter_mut().rev() across
the callback, so a re-entrant connect could realloc the Vec under a live
iterator (self-disconnect dropped the tail: oracle 1 vs port 0 inputs;
push mid-tick lost the last element: [4,3,2] vs oracle [4,3,2,1]).

Restructure, matching the C one-for-one:
- AudioOutput is the owner (info getters, JoinHandle, initialized); the
  state the thread and FFI callers share lives in a Box<Shared> created
  before the recursive pthread mutex is initialized at its final
  address (POSIX forbids moving an initialized mutex).
- The worker gets a &Shared only — no &mut AudioOutput or &mut Shared
  exists after open. connect/disconnect/active/tick_for_test are &self;
  mixes sit behind input_mutex + UnsafeCell with borrows scoped to a
  single statement.
- do_audio_output keeps C's reverse-index traversal: bound fixed at
  entry, inputs[i-1] re-read per iteration, callback/param/resampler/
  allow_clipping copied into locals, every borrow ended, callback
  invoked while still holding the recursive lock. Mid-dispatch removal
  past the loop index is skipped instead of calling freed tail memory
  (documented deviation; the C's stale-tail call has no defined
  equivalent).
- Drop: stop + join complete before the shared box is freed, and the
  join never runs under the input lock.
- NULL input_cb: tick treated as failed (C faults). NULL output cb:
  connect accepted, dead call skipped (C faults next tick). Both
  documented at the top of audio_io.rs.
- Test determinism: a shared DRIVER mutex serializes MAGIC-stamped
  manual ticks in both test files (the real worker never produces MAGIC
  timestamps); the parity TICK record is replaced per driven tick so a
  test that never takes its record cannot poison the next case.

unsafe stays confined to the UnsafeCell/FFI sites with SAFETY comments;
the exception is recorded for the tracking issue.
zackees pushed a commit that referenced this pull request Oct 12, 2026
Unix-first portability per fork PR #35 review: libc exposes no pthread
API on Windows, so the port, its shims, oracle and tests are
#[cfg(unix)] / #![cfg(unix)] and libobs keeps building
media-io/audio-io.c on Windows (same ENABLE_RUST_LIBOBS +
PLATFORM_ID:Windows guard shape as util/utf8.c). The 10 audio_output_*
exports get the ' unix' marker so rust.cmake does not /EXPORT them on
Windows — enforced by obs-util's export_list scan. A Windows backend
behind the same safe API is separate work (noted in audio_io.rs).

Also pins the audio_io_parity proptest-regressions file (repo
convention: bitstream/config_file ship theirs).
zackees pushed a commit that referenced this pull request Oct 12, 2026
obs_startup needs an X display; on headless Linux every obs-dependent
case silently skipped (!obs_up -> skip). The fork PR #35 review requires
a Tier 2 that cannot skip: the cases now assert obs_up loudly, and the
test is registered via xvfb-run -a where xvfb exists (validate image
gains the xvfb package) so they actually run — failures like an
always-false audio_output_connect can no longer hide behind a skip.

Local-Gate: v1 tree=c8db6ae56f567359dffd12caa5966158ab439936 secs=33 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791693456,"gate":"rust/all/fmt","host":"linux-x86_64","key":"cd423e03662fdf0d13cd05a924d8451ac1dcaa75ee035f6f256a7b48857f7b95","lane":"lint","parents":["f03b6ed91acbb9db863d37858469f1995b248ad9"],"secs":5,"stamp":"5d0ec5461ad708d9efedb55ff851aefd","tree":"c8db6ae56f567359dffd12caa5966158ab439936","v":1,"via":"run"}
Ci-Attestation: {"at":1791693456,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"cd423e03662fdf0d13cd05a924d8451ac1dcaa75ee035f6f256a7b48857f7b95","lane":"lint","parents":["f03b6ed91acbb9db863d37858469f1995b248ad9"],"secs":5,"stamp":"2127baeef10d9722e392e4e1d8f43ff9","tree":"c8db6ae56f567359dffd12caa5966158ab439936","v":1,"via":"run"}
Ci-Attestation: {"at":1791693456,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"e53b1970f5af9aa3341c8c5341de995d6153585e927c48db3af11d6a8ffd5f3d","lane":"rust-test","parents":["f03b6ed91acbb9db863d37858469f1995b248ad9"],"secs":20,"stamp":"3fcd1fe0ce7b781cbcaf03ea972473c9","tree":"c8db6ae56f567359dffd12caa5966158ab439936","v":1,"via":"run"}
Ci-Attestation: {"at":1791693456,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"2add412b2ec51597e865ca2744b1b89e7e1d3a898d52de9f57597c5ba43a02c1","lane":"tier2-linux","parents":["f03b6ed91acbb9db863d37858469f1995b248ad9"],"secs":8,"stamp":"af2df4e9aa76ea57216a87a6d914f2b6","tree":"c8db6ae56f567359dffd12caa5966158ab439936","v":1,"via":"run"}
zackees pushed a commit that referenced this pull request Oct 12, 2026
Fork PR #35 review items:
- fresh_clamp_snapshot_parity: input cb connects an allow_clipping input
  to a mix that was empty at the active_mixes snapshot. C re-checks
  !mix->inputs.num at clamp time, so the new mix is clamped and the
  unclamped row carries the marker (recorded 2.5f); the port skips it
  behind the stale mask (recorded 0.0).
- resample_parity: a rate-differing conversion must create a resampler
  and run the resample path; the NULL stub refuses the connect on both
  sides, so the assertion fails until a real resampler exists.
zackees pushed a commit that referenced this pull request Oct 12, 2026
Fork PR #35 items 3+4: explicit test ticks raced the real audio thread —
a worker tick could interleave between an oracle tick and the matching
port tick, mutating buffers and the registry under the same state the
test was reading. Every driven-tick test now runs on a no-worker pair:
oracle_audio_output_open_no_thread (same object minus stop_event and
pthread; close destroys the mutex then frees inputs like the C) and
AudioOutput::open_no_worker (open minus spawn). The concurrent test
still exercises cross-thread connect/disconnect through the hammer
threads; only the tick is exclusive. Shared TICK/ORDER statics are
drained after closes via close_pair so nothing leaks between tests.
audio_io.rs's input_thread_runs keeps the real worker — the thread is
its subject.
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.

2 participants