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

logger: Create only one Context per process for mobile #38149

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

abeyad
Copy link
Contributor

@abeyad abeyad commented Jan 23, 2025

The Logger::Context operates as a stack. When a new one is created, it becomes the active Context. When it gets destroyed, the destructor pops the previous Context as the currently active one.

This can lead to lifetime bugs in Envoy Mobile, if multiple engines are created and the destruction of those engines lead to indeterminate destruction of the Logger::Context objects.

In this commit, the behavior is changed so that the Logger::Context initialization is done by the external caller, not inside StrippedMainBase as previously done. This allows the caller, in this case, Envoy Mobile, to ensure only one Logger::Context is ever created, circumventing any potential issues on Context destruction with popping the previously saved context.

The Logger::Context operates as a stack. When a new one is created, it
becomes the active Context. When it gets destroyed, the destructor pops
the previous Context as the currently active one.

This can lead to lifetime bugs in Envoy Mobile, if multiple engines are
created and the destruction of those engines lead to indeterminate
destruction of the Logger::Context objects.

In this commit, the behavior is changed so that the Logger::Context
initialization is done by the external caller, not inside
StrippedMainBase as previously done. This allows the caller, in this
case, Envoy Mobile, to ensure only one Logger::Context is ever created,
circumventing any potential issues on Context destruction with popping
the previously saved context.

Signed-off-by: Ali Beyad <abeyad@google.com>
Copy link

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #38149 was opened by abeyad.

see: more, trace.

Signed-off-by: Ali Beyad <abeyad@google.com>
Signed-off-by: Ali Beyad <abeyad@google.com>
Signed-off-by: Ali Beyad <abeyad@google.com>
@abeyad
Copy link
Contributor Author

abeyad commented Jan 25, 2025

/retest

@abeyad abeyad marked this pull request as ready for review January 25, 2025 01:14
@abeyad
Copy link
Contributor Author

abeyad commented Jan 25, 2025

/assign @jmarantz

@abeyad
Copy link
Contributor Author

abeyad commented Jan 25, 2025

@jmarantz I also drafted #38188, which passes the tests too. I feel the code in this PR is cleaner, but it does have a bit more of a singleton paradigm in that StrippedMainBase still creates a Logger::Context but only if there isn't a currently active Context.

*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);
std::unique_ptr<Random::RandomGeneratorImpl> random_generator =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is a good use-case for auto as the type is obvious from context.

// This helps avoid issues on Logger::Context destruction when the previous saved context
// could be activated in a thread-unsafe manner.
void initOnceLoggerContext(const OptionsImplBase& options) {
static Thread::MutexBasicLockable* log_lock = new Thread::MutexBasicLockable();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: MUTABLE_CONSTRUCT_ON_FIRST_USE if we can make that work, just for consistency.

@@ -19,6 +21,17 @@ constexpr absl::Duration ENGINE_RUNNING_TIMEOUT = absl::Seconds(30);
// Google DNS address used for IPv6 probes.
constexpr absl::string_view IPV6_PROBE_ADDRESS = "2001:4860:4860::8888";
constexpr uint32_t IPV6_PROBE_PORT = 53;

// There is only one shared static Logger::Context instance for all Envoy Mobile engines.
// This helps avoid issues on Logger::Context destruction when the previous saved context
Copy link
Contributor

@jmarantz jmarantz Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the essence of this problem that Envoy-Mobile does not have access to make changes in main()?

How does it handle things like ProcessWIde?

Note that ProcessWide was invented after this whole thing I did long ago with MainCommon and tbe logger context. Maybe we can lean on that more and clean some of this stuff up, so there's just one way to initalize all the things that need to be done once per process, rather than having Logger::Context be special.

What I'd like to see is most of this stuff is normally owned in main() or similar, without singletons for the most part. But if E-M is special cause it doesn't get any access to main, then there can be one ProcessWide singleton specific to E-M.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants