diff --git a/include/condy/runtime.hpp b/include/condy/runtime.hpp index aaf0de31..e2bc84d1 100644 --- a/include/condy/runtime.hpp +++ b/include/condy/runtime.hpp @@ -158,10 +158,6 @@ class Runtime { flush_global_queue_(); ring_enabled_.store(true, std::memory_order_relaxed); } - - if (!disable_register_ring_fd_) { - io_uring_register_ring_fd(ring_.ring()); - } } else if (run_thread_id_ == std::this_thread::get_id()) { flush_ring_(); } else { @@ -169,6 +165,10 @@ class Runtime { "Runtime::run() can only be called from the same thread"); } + if (!disable_register_ring_fd_) { + io_uring_register_ring_fd(ring_.ring()); + } + while (true) { tick_count_++; @@ -189,6 +189,10 @@ class Runtime { flush_ring_wait_(); } + if (!disable_register_ring_fd_) { + io_uring_unregister_ring_fd(ring_.ring()); + } + tick_count_ = 0; exit_allowed_.store(false, std::memory_order_release); } diff --git a/tests/test_runtime.cpp b/tests/test_runtime.cpp index 8c61d55a..6aba0a09 100644 --- a/tests/test_runtime.cpp +++ b/tests/test_runtime.cpp @@ -1,3 +1,4 @@ +#include "condy/async_operations.hpp" #include "condy/coro.hpp" #include "condy/detail/async_operations.hpp" #include "condy/detail/invoker.hpp" @@ -6,6 +7,7 @@ #include "condy/task.hpp" #include #include +#include #include namespace { @@ -301,3 +303,24 @@ TEST_CASE("test runtime - cross-thread schedule between runs") { REQUIRE(invoker.finished); } + +TEST_CASE("test runtime - runtime destroyed on another runtime") { + condy::Runtime runtime_a(options); + + auto func = [&]() -> condy::Coro { + { + auto runtime_b = std::make_unique(options); + std::thread thread_b([&]() { + runtime_b->allow_exit(); + runtime_b->run(); + }); + thread_b.join(); + } + + co_await condy::async_nop(); + }; + + condy::co_spawn(runtime_a, func()).detach(); + runtime_a.allow_exit(); + runtime_a.run(); +}