From d505a477a423f8764069893763a0fcd76f183c42 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Thu, 26 Mar 2026 17:21:10 +0100 Subject: [PATCH] libstore: asyncify RemoteStore::openConnection Change-Id: Icc7e705314b9a099687c37859700959139840631 --- lix/libstore/remote-store.cc | 10 ++++++---- lix/libstore/remote-store.hh | 4 ++-- lix/libstore/ssh-store.cc | 10 ++++++---- lix/libstore/uds-remote-store.cc | 8 +++++--- lix/libstore/uds-remote-store.hh | 2 +- lix/nix/daemon.cc | 2 +- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lix/libstore/remote-store.cc b/lix/libstore/remote-store.cc index b783d1627..b52d469d0 100644 --- a/lix/libstore/remote-store.cc +++ b/lix/libstore/remote-store.cc @@ -55,21 +55,23 @@ RemoteStore::RemoteStore(const RemoteStoreConfig & config) } -ref RemoteStore::openConnectionWrapper() -{ +kj::Promise>> RemoteStore::openConnectionWrapper() +try { if (failed) throw Error("opening a connection to remote store '%s' previously failed", getUri()); try { - return openConnection(); + co_return TRY_AWAIT(openConnection()); } catch (...) { failed = true; throw; } +} catch (...) { + co_return result::current_exception(); } kj::Promise>> RemoteStore::openAndInitConnection() try { - auto conn = openConnection(); + auto conn = TRY_AWAIT(openConnection()); try { TRY_AWAIT(initConnection(*conn)); co_return conn; diff --git a/lix/libstore/remote-store.hh b/lix/libstore/remote-store.hh index afcc4dc90..8353efb08 100644 --- a/lix/libstore/remote-store.hh +++ b/lix/libstore/remote-store.hh @@ -178,11 +178,11 @@ public: struct Connection; - ref openConnectionWrapper(); + kj::Promise>> openConnectionWrapper(); protected: - virtual ref openConnection() = 0; + virtual kj::Promise>> openConnection() = 0; kj::Promise>> openAndInitConnection(); diff --git a/lix/libstore/ssh-store.cc b/lix/libstore/ssh-store.cc index e38f18144..147da1e7b 100644 --- a/lix/libstore/ssh-store.cc +++ b/lix/libstore/ssh-store.cc @@ -101,7 +101,7 @@ protected: } }; - ref openConnection() override; + kj::Promise>> openConnection() override; std::string host; @@ -119,8 +119,8 @@ protected: }; }; -ref SSHStore::openConnection() -{ +kj::Promise>> SSHStore::openConnection() +try { auto conn = make_ref(); std::string command = config_.remoteProgram + " --stdio"; @@ -128,7 +128,9 @@ ref SSHStore::openConnection() command += " --store " + shellEscape(config_.remoteStore.get()); conn->sshConn = ssh.startCommand(command); - return conn; + return {conn}; +} catch (...) { + return {result::current_exception()}; } void registerSSHStore() { diff --git a/lix/libstore/uds-remote-store.cc b/lix/libstore/uds-remote-store.cc index 1929aa50f..9093448f0 100644 --- a/lix/libstore/uds-remote-store.cc +++ b/lix/libstore/uds-remote-store.cc @@ -78,8 +78,8 @@ static void connectToFirstAvailableSocket(AutoCloseFD & sockFD, const std::list< throw Error("could not connect to any lix socket (tried %s)", concatStringsSep(", ", paths)); } -ref UDSRemoteStore::openConnection() -{ +kj::Promise>> UDSRemoteStore::openConnection() +try { auto conn = make_ref(); /* Connect to a daemon that does the privileged work for us. */ @@ -109,7 +109,9 @@ ref UDSRemoteStore::openConnection() conn->startTime = std::chrono::steady_clock::now(); - return conn; + co_return conn; +} catch (...) { + co_return result::current_exception(); } diff --git a/lix/libstore/uds-remote-store.hh b/lix/libstore/uds-remote-store.hh index dc3bfb612..6f66c25f7 100644 --- a/lix/libstore/uds-remote-store.hh +++ b/lix/libstore/uds-remote-store.hh @@ -101,7 +101,7 @@ private: } }; - ref openConnection() override; + kj::Promise>> openConnection() override; std::optional path; }; diff --git a/lix/nix/daemon.cc b/lix/nix/daemon.cc index 6245cbfa0..08b5d312a 100644 --- a/lix/nix/daemon.cc +++ b/lix/nix/daemon.cc @@ -531,7 +531,7 @@ daemonInstance(AsyncIoRoot & aio, std::optional forceTrustClientOpt */ static void forwardStdioConnection(AsyncIoRoot & aio, RemoteStore & store) { - auto conn = store.openConnectionWrapper(); + auto conn = aio.blockOn(store.openConnectionWrapper()); auto connSocket = AIO().lowLevelProvider.wrapSocketFd(conn->getFD()); auto asyncStdin = AIO().lowLevelProvider.wrapInputFd(STDIN_FILENO); auto asyncStdout = AIO().lowLevelProvider.wrapOutputFd(STDOUT_FILENO);