From 011f7d785a9e9ef240de0a9eea23531a247a3477 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Sun, 28 Feb 2021 19:03:16 +0000 Subject: [PATCH] logging: Rework for shutdown Make changes to logger thread to allow the logger to be replaced with a NOP logger (required for agent shutdown). Signed-off-by: James O. D. Hunt --- src/agent/src/main.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/agent/src/main.rs b/src/agent/src/main.rs index 595f4968a4..f785c94d9a 100644 --- a/src/agent/src/main.rs +++ b/src/agent/src/main.rs @@ -217,25 +217,39 @@ async fn real_main() -> std::result::Result<(), Box> { let writer = unsafe { File::from_raw_fd(wfd) }; // Recreate a logger with the log level get from "/proc/cmdline". - let (logger, _logger_async_guard) = + let (logger, logger_async_guard) = logging::create_logger(NAME, "agent", config.log_level, writer); announce(&logger, &config); - // This "unused" variable is required as it enables the global (and crucially static) logger, + // This variable is required as it enables the global (and crucially static) logger, // which is required to satisfy the the lifetime constraints of the auto-generated gRPC code. - let _guard = slog_scope::set_global_logger(logger.new(o!("subsystem" => "rpc"))); + let global_logger = slog_scope::set_global_logger(logger.new(o!("subsystem" => "rpc"))); - let mut _log_guard: Result<(), log::SetLoggerError> = Ok(()); + // Allow the global logger to be modified later (for shutdown) + global_logger.cancel_reset(); + + let mut ttrpc_log_guard: Result<(), log::SetLoggerError> = Ok(()); if config.log_level == slog::Level::Trace { // Redirect ttrpc log calls to slog iff full debug requested - _log_guard = Ok(slog_stdlog::init().map_err(|e| e)?); + ttrpc_log_guard = Ok(slog_stdlog::init().map_err(|e| e)?); } // Start the sandbox and wait for its ttRPC server to end start_sandbox(&logger, &config, init_mode).await?; + // Install a NOP logger for the remainder of the shutdown sequence + // to ensure any log calls made by local crates using the scope logger + // don't fail. + let global_logger_guard2 = + slog_scope::set_global_logger(slog::Logger::root(slog::Discard, o!())); + global_logger_guard2.cancel_reset(); + + drop(logger_async_guard); + + drop(ttrpc_log_guard); + // Trigger a controlled shutdown shutdown_tx .send(true)