diff --git a/docs/fallback-warning.md b/docs/fallback-warning.md index f779a6d850..eba86b741b 100644 --- a/docs/fallback-warning.md +++ b/docs/fallback-warning.md @@ -68,10 +68,11 @@ obs_frontend_warn_fallback("browser.sandbox", obs_module_text("SandboxFallback.T - `id` is a stable name. Use `.`. - The caller translates `title` and `message`. -- `cb` is called once, on the UI thread, never inside the call. Keep `param` - valid until then. `cb` can be NULL. -- Without the OBS frontend (another host of the plugin), the call logs and - calls `cb` with `true` inside the call. +- `cb` is called once. While OBS runs, it is called on the UI thread and + never inside the call. Keep `param` valid until then. `cb` can be NULL. +- Without the OBS frontend (another host of the plugin), and after OBS has + begun to shut down, the call logs and calls `cb` with `true` inside the + call. The API reference is in [reference-frontend-api.rst](sphinx/reference-frontend-api.rst). diff --git a/docs/sphinx/reference-frontend-api.rst b/docs/sphinx/reference-frontend-api.rst index 69fa562334..f071eeaf04 100644 --- a/docs/sphinx/reference-frontend-api.rst +++ b/docs/sphinx/reference-frontend-api.rst @@ -1023,9 +1023,9 @@ Functions once (after the log line). At exit, an open warning closes and queued warnings accept. - *cb* is never called inside this call when OBS runs. In a host without - the OBS frontend, this call logs, then calls *cb* with *true* inside the - call. + *cb* is never called inside this call while OBS runs. In a host without + the OBS frontend, and after OBS has begun to shut down, this call logs, + then calls *cb* with *true* inside the call. See ``docs/fallback-warning.md`` in OBS-Studio-Rust (obs-rust/obs-studio#2218). diff --git a/frontend/build/frontend-fallback-warning.pch.hpp b/frontend/build/frontend-fallback-warning.pch.hpp index fab80fef4a..89d583aa38 100644 --- a/frontend/build/frontend-fallback-warning.pch.hpp +++ b/frontend/build/frontend-fallback-warning.pch.hpp @@ -22,4 +22,5 @@ #include #include +#include #include diff --git a/frontend/utility/FallbackWarning.cpp.hpp b/frontend/utility/FallbackWarning.cpp.hpp index 65a3b87dff..cfb5972299 100644 --- a/frontend/utility/FallbackWarning.cpp.hpp +++ b/frontend/utility/FallbackWarning.cpp.hpp @@ -21,6 +21,13 @@ static struct { QPointer open; } fw_state; +/* Any thread. Set by SetMainWindow(nullptr): no event loop will run the + * queue, so Warn() answers at once (#2369). Warn() queues under the same lock, + * so a warning is either queued before shutdown (and answered by its flush) + * or answered inside the call. */ +static std::mutex fw_shutdown_mutex; +static bool fw_shut_down = false; + static const char *fw_text(const char *s) { return s ? s : ""; @@ -135,6 +142,10 @@ static void fw_pump() void SetMainWindow(QWidget *window, const Texts &texts) { + { + std::lock_guard lock(fw_shutdown_mutex); + fw_shut_down = !window; + } fw_state.window = window; fw_state.texts = texts; fw_state.ready = true; @@ -151,6 +162,12 @@ void Warn(const char *id, const char *title, const char *message, bool can_cance fw_log(id, message); FwRequest r{id, fw_text(title), message, can_cancel, cb, param}; + std::unique_lock lock(fw_shutdown_mutex); + if (fw_shut_down) { + lock.unlock(); + fw_answer(r, true); + return; + } QMetaObject::invokeMethod( qApp, [r]() { diff --git a/frontend/utility/FallbackWarning.hpp b/frontend/utility/FallbackWarning.hpp index 7a34db2a04..bf1c1a057d 100644 --- a/frontend/utility/FallbackWarning.hpp +++ b/frontend/utility/FallbackWarning.hpp @@ -33,8 +33,9 @@ struct Texts { void SetMainWindow(QWidget *window, const Texts &texts); /* Any thread. Logs now. Then, on the UI thread and never inside this call, - * shows the dialog or accepts, and calls `cb` (if not NULL) once. NULL - * strings are empty. */ + * shows the dialog or accepts, and calls `cb` (if not NULL) once. After + * SetMainWindow(nullptr) (shutdown) it accepts at once, inside this call. + * NULL strings are empty. */ void Warn(const char *id, const char *title, const char *message, bool can_cancel, Callback cb, void *param); } // namespace FallbackWarning diff --git a/test/cmocka/test_fallback_warning_queue.cpp b/test/cmocka/test_fallback_warning_queue.cpp index d532c13041..79ca0650bc 100644 --- a/test/cmocka/test_fallback_warning_queue.cpp +++ b/test/cmocka/test_fallback_warning_queue.cpp @@ -333,6 +333,38 @@ static void shutdown_answers_everyone(void **) assert_true(queued.answers[0].accepted); } +/* After shutdown (SetMainWindow(nullptr)) no event loop runs the queue: a + * later warning logs and is answered at once, inside the call, accepted + * (#2369). A new main window starts the queue again. */ +static void after_shutdown_answers_inside_warn(void **) +{ + FallbackWarning::SetMainWindow(main_window, texts()); + FallbackWarning::SetMainWindow(nullptr, texts()); + settle(); + logged.clear(); + + Caller &caller = new_caller(); + FallbackWarning::Warn("test.late", "Late", "After shutdown.", true, answer, &caller); + assert_int_equal(caller.answers.size(), 1); + assert_true(caller.answers[0].accepted); + assert_int_equal(logged.size(), 1); + assert_int_equal(logged[0].level, LOG_ERROR); + settle(); + assert_int_equal(caller.answers.size(), 1); + + FallbackWarning::SetMainWindow(main_window, texts()); + Caller &again = new_caller(); + FallbackWarning::Warn("test.again", "Again", "Running.", false, answer, &again); + assert_true(again.answers.empty()); + settle(); +#ifdef OBS_FRONTEND_RUST + for (OBSFallbackWarning *d : dialogs()) { + click(d, "acceptButton"); + } +#endif + assert_int_equal(again.answers.size(), 1); +} + /* No callback and NULL strings are allowed. */ static void null_callback_and_strings(void **) { @@ -364,6 +396,7 @@ int main(int argc, char **argv) cmocka_unit_test_setup_teardown(waits_while_another_dialog_is_modal, setup, teardown), cmocka_unit_test_setup_teardown(shutdown_answers_everyone, setup, teardown), cmocka_unit_test_setup_teardown(null_callback_and_strings, setup, teardown), + cmocka_unit_test_setup_teardown(after_shutdown_answers_inside_warn, setup, teardown), }; if (argc > 1) { cmocka_set_test_filter(argv[1]);