diff --git a/core/impl/cluster.cxx b/core/impl/cluster.cxx index c5d149091..6be6e1618 100644 --- a/core/impl/cluster.cxx +++ b/core/impl/cluster.cxx @@ -371,18 +371,21 @@ class cluster_impl : public std::enable_shared_from_this if (event == fork_event::prepare) { io_.stop(); io_thread_.join(); + io_.notify_fork(fork_event_to_asio(event)); + if (transactions_) { + transactions_->notify_fork(event); + } } else { // TODO(SA): close all sockets in fork_event::child io_.restart(); + io_.notify_fork(fork_event_to_asio(event)); + if (event == fork_event::parent && transactions_) { + transactions_->notify_fork(event); + } io_thread_ = std::thread{ [&io = io_] { io.run(); } }; } - io_.notify_fork(fork_event_to_asio(event)); - - if (event != fork_event::child && transactions_) { - transactions_->notify_fork(event); - } } void close(core::utils::movable_function handler) @@ -599,9 +602,9 @@ cluster::notify_fork(fork_event event) -> void if (err.ec()) { // TODO(SA): we should fall to background reconnect loop similar to Columnar build CB_LOG_ERROR("Unable to reconnect instance after fork: {}", err.ec().message()); - return; + } else { + impl_ = new_impl; } - impl_ = new_impl; barrier->set_value(); }); @@ -612,10 +615,11 @@ cluster::notify_fork(fork_event event) -> void void cluster::close(std::function&& handler) { - if (!impl_) { - return handler(); + if (auto impl = std::move(impl_); impl) { + impl->close(std::move(handler)); + } else { + handler(); } - impl_->close(std::move(handler)); } auto diff --git a/test/test_integration_examples.cxx b/test/test_integration_examples.cxx index 6efc5924b..19882635f 100644 --- a/test/test_integration_examples.cxx +++ b/test/test_integration_examples.cxx @@ -79,8 +79,8 @@ namespace start_using #include #include -int -main(int argc, const char* argv[]) +auto +main(int argc, const char* argv[]) -> int { if (argc != 4) { fmt::print("USAGE: ./start_using couchbase://127.0.0.1 Administrator password\n"); @@ -284,8 +284,8 @@ class github_actions_configuration_profile : public couchbase::configuration_pro } }; -int -main(int argc, const char* argv[]) +auto +main(int argc, const char* argv[]) -> int { if (argc != 4) { fmt::print("USAGE: ./example_search couchbase://127.0.0.1 Administrator password\n"); @@ -594,8 +594,8 @@ namespace example_buckets //! [example-buckets] #include -int -main(int argc, const char* argv[]) +auto +main(int argc, const char* argv[]) -> int { if (argc != 4) { fmt::print("USAGE: ./example_buckets couchbase://127.0.0.1 Administrator password\n"); @@ -725,8 +725,8 @@ namespace example_fork #include -int -main(int argc, const char* argv[]) +auto +main(int argc, const char* argv[]) -> int { if (argc != 4) { fmt::print("USAGE: ./example_fork couchbase://127.0.0.1 Administrator password\n"); @@ -747,14 +747,13 @@ main(int argc, const char* argv[]) return 1; } - auto bucket = cluster.bucket(bucket_name); - cluster.notify_fork(couchbase::fork_event::prepare); auto child_pid = fork(); if (child_pid == 0) { cluster.notify_fork(couchbase::fork_event::child); fmt::print("CHILD(pid={}): continue after fork()\n", getpid()); + auto bucket = cluster.bucket(bucket_name); auto collection = bucket.scope("tenant_agent_00").collection("users"); { @@ -790,6 +789,7 @@ main(int argc, const char* argv[]) cluster.notify_fork(couchbase::fork_event::parent); fmt::print("PARENT(pid={}): continue after fork() child_pid={}\n", getpid(), child_pid); + auto bucket = cluster.bucket(bucket_name); { auto collection = bucket.scope("tenant_agent_00").collection("users"); std::string doc_id = "tenant_agent_00";