diff --git a/src/anbox/cmds/run.cpp b/src/anbox/cmds/run.cpp index 6e32f094d80ee260acfb9694d61d5bc0805b6401..b508f2da736a72cec8ca434a5c2ac108447f69c4 100644 --- a/src/anbox/cmds/run.cpp +++ b/src/anbox/cmds/run.cpp @@ -144,6 +144,10 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) })); container::Client container(rt); + container.register_terminate_handler([&]() { + WARNING("Lost connection to container manager, terminating."); + trap->stop(); + }); container::Configuration container_configuration; container_configuration.bind_mounts = { {qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"}, diff --git a/src/anbox/container/client.cpp b/src/anbox/container/client.cpp index fe0e9d5fe74ac08cfcfe13e816266e2058bc304d..43748120ed43f7fb45544cfa83bf993dd631b4f0 100644 --- a/src/anbox/container/client.cpp +++ b/src/anbox/container/client.cpp @@ -18,11 +18,11 @@ #include "anbox/container/client.h" #include "anbox/config.h" #include "anbox/container/management_api_stub.h" -#include "anbox/logger.h" #include "anbox/network/local_socket_messenger.h" #include "anbox/rpc/channel.h" #include "anbox/rpc/message_processor.h" #include "anbox/rpc/pending_call_cache.h" +#include "anbox/logger.h" namespace ba = boost::asio; namespace bs = boost::system; @@ -43,7 +43,17 @@ Client::Client(const std::shared_ptr &rt) Client::~Client() {} void Client::start_container(const Configuration &configuration) { - management_api_->start_container(configuration); + try { + management_api_->start_container(configuration); + } catch (const std::exception &e) { + ERROR("Failed to start container: %s", e.what()); + if (terminate_callback_) + terminate_callback_(); + } +} + +void Client::register_terminate_handler(const TerminateCallback &callback) { + terminate_callback_ = callback; } void Client::read_next_message() { @@ -54,7 +64,11 @@ void Client::read_next_message() { void Client::on_read_size(const boost::system::error_code &error, std::size_t bytes_read) { - if (error) BOOST_THROW_EXCEPTION(std::runtime_error(error.message())); + if (error) { + if (terminate_callback_) + terminate_callback_(); + return; + } std::vector data(bytes_read); std::copy(buffer_.data(), buffer_.data() + bytes_read, data.data()); diff --git a/src/anbox/container/client.h b/src/anbox/container/client.h index 63aabdfe2a966ed40255ee3a8d80e839f76accb1..bccf4d1d4ef07d991a4c18d7194e3443f8c40388 100644 --- a/src/anbox/container/client.h +++ b/src/anbox/container/client.h @@ -34,11 +34,15 @@ namespace container { class ManagementApiStub; class Client { public: + typedef std::function TerminateCallback; + Client(const std::shared_ptr &rt); ~Client(); void start_container(const Configuration &configuration); + void register_terminate_handler(const TerminateCallback &callback); + private: void read_next_message(); void on_read_size(const boost::system::error_code &ec, @@ -50,6 +54,7 @@ class Client { std::shared_ptr management_api_; std::shared_ptr processor_; std::array buffer_; + TerminateCallback terminate_callback_; }; } // namespace container } // namespace anbox