Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions frontend/widgets/RecordingShutdownWait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool()> &active)
{
Expand All @@ -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<bool> deactivated{false};
const std::function<bool()> *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()
Expand Down
108 changes: 108 additions & 0 deletions test/cmocka/test_recording_shutdown_inactive.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
extern "C" {
#include <cmocka.h>
}

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>

#include <QCoreApplication>
#include <QElapsedTimer>
#include <QTimer>

#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<bool> 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<std::mutex> 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<std::mutex> 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);
}
15 changes: 15 additions & 0 deletions test/cmocka/tests.d/test_recording_shutdown_inactive.cmake
Original file line number Diff line number Diff line change
@@ -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()
Loading