From 9cc72e8ea9f59f9a9627d05528d54a559ebd412c Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 23 Mar 2025 18:27:52 +0530 Subject: Fix segfault with luaruntime destructor --- src/AsyncEventLoop.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/AsyncEventLoop.cpp') diff --git a/src/AsyncEventLoop.cpp b/src/AsyncEventLoop.cpp index 6c6333f..230d891 100644 --- a/src/AsyncEventLoop.cpp +++ b/src/AsyncEventLoop.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "AsyncEventLoop.hpp" @@ -53,32 +54,36 @@ AsyncEventLoop::~AsyncEventLoop() { return; isLoopRunning = false; - // Wake it up. Stab it to death. (clear async queue) - wake(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - { - std::lock_guard lock(tasksQueueMutex); - std::queue>().swap(tasksQueue); - } + flushTasks(); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); uv_stop(loop); // Close all handles AsyncEventLoop::closeHandle(reinterpret_cast(&asyncHandle)); uv_walk(loop, AsyncEventLoop::closeHandle, nullptr); - while (uv_run(loop, UV_RUN_NOWAIT) != 0) - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - // TODO: Fix pending handler case (setTimeout(100) wait(20) close() -> error) if (loopThread.joinable()) loopThread.join(); - while (uv_loop_close(loop)) - uv_run(loop, UV_RUN_DEFAULT); + // Close loop + while (uv_loop_close(loop) == EBUSY) + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + uv_run(loop, UV_RUN_DEFAULT); + + if (uv_loop_alive(loop)) + qDebug() << "WARNING: Loop still has active handles!"; free(loop); loop = nullptr; } +void AsyncEventLoop::flushTasks() { + std::lock_guard lock(tasksQueueMutex); + std::queue>().swap(tasksQueue); + wake(); +} + void AsyncEventLoop::asyncHandleCallback(uv_async_t *handle) { auto *runtime = static_cast(handle->data); runtime->processTasks(); -- cgit v1.3.1