|
11 | 11 | #include <Babylon/Polyfills/Blob.h> |
12 | 12 | #include <Babylon/Polyfills/TextDecoder.h> |
13 | 13 | #include <gtest/gtest.h> |
| 14 | +#include <atomic> |
| 15 | +#include <chrono> |
14 | 16 | #include <future> |
15 | 17 | #include <iostream> |
| 18 | +#include <thread> |
16 | 19 |
|
17 | 20 | namespace |
18 | 21 | { |
@@ -125,6 +128,62 @@ TEST(Console, Log) |
125 | 128 | done.get_future().get(); |
126 | 129 | } |
127 | 130 |
|
| 131 | +TEST(AppRuntime, DestroyDoesNotDeadlock) |
| 132 | +{ |
| 133 | + // Deterministic test for the race condition in the AppRuntime destructor. |
| 134 | + // |
| 135 | + // A per-instance hook sleeps WHILE HOLDING the queue mutex, right before |
| 136 | + // condition_variable::wait(). We synchronize so the worker is definitely |
| 137 | + // in the hook before triggering destruction. |
| 138 | + // |
| 139 | + // Old (broken) code: cancel() + notify_all() fire without the mutex, |
| 140 | + // so the notification is lost while the worker sleeps → deadlock. |
| 141 | + // Fixed code: Append(cancel) calls push() which NEEDS the mutex, |
| 142 | + // so it blocks until the worker enters wait() → notification delivered. |
| 143 | + for (int i = 0; i < 3; i++) |
| 144 | + { |
| 145 | + // Shared state for hook synchronization |
| 146 | + std::atomic<bool> hookEnabled{false}; |
| 147 | + std::atomic<bool> hookSignaled{false}; |
| 148 | + std::promise<void> workerInHook; |
| 149 | + |
| 150 | + auto runtime = std::make_unique<Babylon::AppRuntime>(); |
| 151 | + |
| 152 | + // Set the callback once, before any dispatches. The callback checks |
| 153 | + // hookEnabled so we control when it actually sleeps. |
| 154 | + runtime->SetBeforeWaitCallback([&]() { |
| 155 | + if (hookEnabled.load() && !hookSignaled.exchange(true)) |
| 156 | + workerInHook.set_value(); |
| 157 | + if (hookEnabled.load()) |
| 158 | + std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 159 | + }); |
| 160 | + |
| 161 | + // Dispatch work and wait for completion |
| 162 | + std::promise<void> ready; |
| 163 | + runtime->Dispatch([&ready](Napi::Env) { |
| 164 | + ready.set_value(); |
| 165 | + }); |
| 166 | + ready.get_future().wait(); |
| 167 | + |
| 168 | + // Enable the hook and dispatch a no-op to wake the worker, |
| 169 | + // ensuring it cycles through the hook on its way back to idle |
| 170 | + hookEnabled.store(true); |
| 171 | + runtime->Dispatch([](Napi::Env) {}); |
| 172 | + |
| 173 | + // Wait for the worker to be in the hook (holding mutex, sleeping) |
| 174 | + workerInHook.get_future().wait(); |
| 175 | + |
| 176 | + // NOW destroy |
| 177 | + auto destroyFuture = std::async(std::launch::async, [&runtime]() { |
| 178 | + runtime.reset(); |
| 179 | + }); |
| 180 | + |
| 181 | + auto status = destroyFuture.wait_for(std::chrono::seconds(5)); |
| 182 | + ASSERT_NE(status, std::future_status::timeout) |
| 183 | + << "Deadlock detected on iteration " << i; |
| 184 | + } |
| 185 | +} |
| 186 | + |
128 | 187 | int RunTests() |
129 | 188 | { |
130 | 189 | testing::InitGoogleTest(); |
|
0 commit comments