Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mobile: Don't activate previous logging context on engine destruction #37833

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mobile/library/common/engine_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ EngineCommon::EngineCommon(std::shared_ptr<Envoy::OptionsImplBase> options) : op
};
// `set_new_handler` is false because the application using Envoy Mobile should decide how to
// handle `new` memory allocation failures.
// `activate_saved_logging_context_on_destruction` controls whether the logging Context activates
// a previously stored static context on destruction. This sometimes causes memory crashes in
// Envoy Mobile on StrippedMainBase destruction, so avoid cleaning up the logging context to
// prevent crashes on engine destruction.
base_ = std::make_unique<StrippedMainBase>(
*options_, real_time_system_, default_listener_hooks_, prod_component_factory_,
std::make_unique<PlatformImpl>(), std::make_unique<Random::RandomGeneratorImpl>(), nullptr,
create_instance, /*set_new_handler=*/false);
create_instance, /*set_new_handler=*/false,
/*activate_saved_logging_context_on_destruction=*/false);
// Disabling signal handling in the options makes it so that the server's event dispatcher _does
// not_ listen for termination signals such as SIGTERM, SIGINT, etc
// (https://github.com/envoyproxy/envoy/blob/048f4231310fbbead0cbe03d43ffb4307fff0517/source/server/server.cc#L519).
Expand Down
9 changes: 6 additions & 3 deletions source/common/common/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,19 @@ static std::atomic<Context*> current_context = nullptr;
static_assert(std::atomic<Context*>::is_always_lock_free);

Context::Context(spdlog::level::level_enum log_level, const std::string& log_format,
Thread::BasicLockable& lock, bool should_escape, bool enable_fine_grain_logging)
Thread::BasicLockable& lock, bool should_escape, bool enable_fine_grain_logging,
bool activate_saved_on_destruction)
: log_level_(log_level), log_format_(log_format), lock_(lock), should_escape_(should_escape),
enable_fine_grain_logging_(enable_fine_grain_logging), save_context_(current_context) {
enable_fine_grain_logging_(enable_fine_grain_logging),
activate_saved_on_destruction_(activate_saved_on_destruction),
save_context_(current_context) {
current_context = this;
activate();
}

Context::~Context() {
current_context = save_context_;
if (current_context != nullptr) {
if (current_context != nullptr && activate_saved_on_destruction_) {
current_context.load()->activate();
} else {
Registry::getSink()->clearLock();
Expand Down
4 changes: 3 additions & 1 deletion source/common/common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ enum class LoggerMode { Envoy, FineGrainLog };
class Context {
public:
Context(spdlog::level::level_enum log_level, const std::string& log_format,
Thread::BasicLockable& lock, bool should_escape, bool enable_fine_grain_logging = false);
Thread::BasicLockable& lock, bool should_escape, bool enable_fine_grain_logging = false,
bool activate_saved_on_destruction = true);
~Context();

/**
Expand All @@ -316,6 +317,7 @@ class Context {
Thread::BasicLockable& lock_;
bool should_escape_;
bool enable_fine_grain_logging_;
bool activate_saved_on_destruction_;
Context* const save_context_;

std::string fine_grain_log_format_;
Expand Down
9 changes: 5 additions & 4 deletions source/exe/stripped_main_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ StrippedMainBase::StrippedMainBase(const Server::Options& options, Event::TimeSy
std::unique_ptr<Server::Platform> platform_impl,
std::unique_ptr<Random::RandomGenerator>&& random_generator,
std::unique_ptr<ProcessContext> process_context,
CreateInstanceFunction create_instance, bool set_new_handler)
CreateInstanceFunction create_instance, bool set_new_handler,
bool activate_saved_logging_context_on_destruction)
: platform_impl_(std::move(platform_impl)), options_(options),
component_factory_(component_factory), stats_allocator_(symbol_table_) {
// Process the option to disable extensions as early as possible,
Expand All @@ -72,9 +73,9 @@ StrippedMainBase::StrippedMainBase(const Server::Options& options, Event::TimeSy
tls_ = std::make_unique<ThreadLocal::InstanceImpl>();
Thread::BasicLockable& log_lock = restarter_->logLock();
Thread::BasicLockable& access_log_lock = restarter_->accessLogLock();
logging_context_ = std::make_unique<Logger::Context>(options_.logLevel(), options_.logFormat(),
log_lock, options_.logFormatEscaped(),
options_.enableFineGrainLogging());
logging_context_ = std::make_unique<Logger::Context>(
options_.logLevel(), options_.logFormat(), log_lock, options_.logFormatEscaped(),
options_.enableFineGrainLogging(), activate_saved_logging_context_on_destruction);

configureComponentLogLevels();

Expand Down
3 changes: 2 additions & 1 deletion source/exe/stripped_main_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class StrippedMainBase {
std::unique_ptr<Server::Platform> platform_impl,
std::unique_ptr<Random::RandomGenerator>&& random_generator,
std::unique_ptr<ProcessContext> process_context,
CreateInstanceFunction create_instance, bool set_new_handler = true);
CreateInstanceFunction create_instance, bool set_new_handler = true,
bool activate_saved_logging_context_on_destruction = true);

void runServer() {
ASSERT(options_.mode() == Server::Mode::Serve);
Expand Down
Loading