diff --git a/docs/features.md b/docs/features.md index 3111b1ee48..ad86bb5401 100644 --- a/docs/features.md +++ b/docs/features.md @@ -1201,6 +1201,11 @@ area. sleeping on the UI thread. *All*, 2026-10-17. Issue [#1283](https://github.com/obs-rust/obs-studio/issues/1283), PR [#1342](https://github.com/obs-rust/obs-studio/pull/1342). +- Recording shutdown waits until the output actually reports inactive, + including the interval after its deactivate signal; a signal alone no + longer permits premature shutdown. The UI keeps processing events within + the existing timeout. *All*, 2026-10-18. Issue + [#1391](https://github.com/obs-rust/obs-studio/issues/1391). ### Scripting diff --git a/frontend/widgets/RecordingShutdownWait.hpp b/frontend/widgets/RecordingShutdownWait.hpp index cbecccc4e4..787a76f099 100644 --- a/frontend/widgets/RecordingShutdownWait.hpp +++ b/frontend/widgets/RecordingShutdownWait.hpp @@ -29,9 +29,10 @@ * A graceful stop of an active output signals "stop" while the output is * still flushing its encoders, then "deactivate" when it is done, just * before it reports inactive; an output that never began capturing signals - * only "stop", and is already inactive. So the wait ends on "deactivate", or - * on "stop" once active() is false. libobs signals on its own threads: the - * callback only records the signal and queues the check to the UI thread. */ + * only "stop", and is already inactive. Signals trigger an active-state + * check. After "deactivate", queued timer checks bridge the interval before + * the output reports inactive, bounded by the same timeout. libobs signals + * on its own threads; callbacks queue checks to the UI thread. */ inline bool WaitForOutputToStop(signal_handler_t *output_signals, uint64_t timeout_ms, const std::function &active) { @@ -40,12 +41,20 @@ inline bool WaitForOutputToStop(signal_handler_t *output_signals, uint64_t timeo QObject receiver; /* lives on this (the UI) thread */ std::atomic deactivated{false}; const std::function *active = nullptr; + bool retry_pending = false; /* UI thread only */ - bool Stopped() const { return deactivated.load() || !(*active)(); } + bool Stopped() const { return !(*active)(); } void Check() { if (Stopped()) { loop.quit(); + } else if (deactivated.load() && !retry_pending) { + /* deactivate precedes the inactive store on the output thread. */ + retry_pending = true; + QTimer::singleShot(1, &receiver, [this]() { + retry_pending = false; + Check(); + }); } } void Queue() diff --git a/test/cmocka/test_recording_shutdown_inactive.cpp b/test/cmocka/test_recording_shutdown_inactive.cpp new file mode 100644 index 0000000000..aa47b579bc --- /dev/null +++ b/test/cmocka/test_recording_shutdown_inactive.cpp @@ -0,0 +1,108 @@ +/* #1391: deactivate is emitted before the output's inactive store. The + * signal must wake the wait, but cannot itself certify completion. */ +#include +#include +#include +extern "C" { +#include +} + +#include +#include +#include +#include + +#include +#include +#include + +#include "RecordingShutdownWait.hpp" + +struct Result { + bool stopped; + bool active; + bool ui_processed; + bool checked_after_deactivate; + qint64 elapsed_ms; +}; + +/* No sleeps or scheduler-dependent gap: the inactive store is a later UI + * event, scheduled only after the active predicate observes deactivate. + * The real libobs signal handler still invokes callbacks on a worker. */ +static Result run_wait(bool become_inactive) +{ + auto *handler = signal_handler_create(); + signal_handler_add(handler, "void stop(ptr output, int code)"); + signal_handler_add(handler, "void deactivate(ptr output)"); + std::atomic deactivated{false}; + bool active = true; + bool ui_processed = false; + bool checked_after_deactivate = false; + QObject receiver; + std::mutex mutex; + std::condition_variable condition; + bool start = false; + std::thread worker([&]() { + { + std::unique_lock lock(mutex); + condition.wait(lock, [&]() { return start; }); + } + uint8_t stack[128]; + calldata_t data; + calldata_init_fixed(&data, stack, sizeof(stack)); + calldata_set_ptr(&data, "output", nullptr); + calldata_set_int(&data, "code", 0); + deactivated = true; + signal_handler_signal(handler, "deactivate", &data); + }); + QTimer::singleShot(0, &receiver, [&]() { + ui_processed = true; + { + std::lock_guard lock(mutex); + start = true; + } + condition.notify_one(); + }); + QElapsedTimer elapsed; + elapsed.start(); + bool stopped = WaitForOutputToStop(handler, 100, [&]() { + if (deactivated.load() && !checked_after_deactivate) { + checked_after_deactivate = true; + if (become_inactive) { + QTimer::singleShot(0, &receiver, [&]() { active = false; }); + } + } + return active; + }); + Result result{stopped, active, ui_processed, checked_after_deactivate, elapsed.elapsed()}; + worker.join(); + signal_handler_destroy(handler); + return result; +} + +static void test_deactivate_before_inactive(void **) +{ + const auto result = run_wait(true); + assert_true(result.stopped); + assert_false(result.active); + assert_true(result.ui_processed); + assert_true(result.checked_after_deactivate); +} + +static void test_deactivate_without_inactive_times_out(void **) +{ + const auto result = run_wait(false); + assert_false(result.stopped); + assert_true(result.active); + assert_true(result.ui_processed); + assert_true(result.checked_after_deactivate); + assert_true(result.elapsed_ms >= 80); +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + const CMUnitTest tests[] = {cmocka_unit_test(test_deactivate_before_inactive), + cmocka_unit_test(test_deactivate_without_inactive_times_out)}; + return cmocka_run_group_tests(tests, nullptr, nullptr); +} diff --git a/test/cmocka/tests.d/test_recording_shutdown_inactive.cmake b/test/cmocka/tests.d/test_recording_shutdown_inactive.cmake new file mode 100644 index 0000000000..89929eb130 --- /dev/null +++ b/test/cmocka/tests.d/test_recording_shutdown_inactive.cmake @@ -0,0 +1,15 @@ +# Post-deactivate inactivity regression (#1391); real Qt event loop, no display. +if(OS_LINUX) + find_package(Qt6 QUIET COMPONENTS Core) + if(TARGET Qt6::Core) + add_executable(test_recording_shutdown_inactive test_recording_shutdown_inactive.cpp) + target_include_directories( + test_recording_shutdown_inactive + PRIVATE ${CMOCKA_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}/frontend/widgets" + ) + target_link_libraries(test_recording_shutdown_inactive PRIVATE OBS::libobs Qt6::Core ${CMOCKA_LIBRARIES}) + add_dependencies(cmocka-tests test_recording_shutdown_inactive) + add_test(test_recording_shutdown_inactive ${CMAKE_CURRENT_BINARY_DIR}/test_recording_shutdown_inactive) + set_tests_properties(test_recording_shutdown_inactive PROPERTIES TIMEOUT 15) + endif() +endif()