open-telemetry / open-telemetry/opentelemetry-cpp

GlobalLogHandler usage may lead to UB

Open
#2,024 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug do-not-stale
Dominant language
C++
Stars
1.4k
Forks
632
Avg merge
1d 13h
Merged PRs (30d)
75

Description

OpenTelemetry-cpp library provides the only one way to log own errors: to use singletone that located under the hood of class GlobalLogHandler.
GlobalLogHandler::GetHandlerAndLevel() may provide a reference to destroyed object if called during app finalization. It will lead to undefined behaviour. "if (!log_handler)" in OTEL_INTERNAL_LOG_DISPATCH macro will not cover this case. I will provide a code snipped if you need it.

Also, there's one more problem with stateful singletone.
I have an integration test in my project that runs several modules in a single process. I can't guarantee that correct global handler will be set if such setup is a part of initialization of every module.

So, I would propose to avoid mandatory singletone usage. I think that correct way is to initialize classes with LogHandlerProvider.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing GlobalLogHandler::GetHandlerAndLevel() and the OTEL_INTERNAL_LOG_DISPATCH macro, then investigate the reported finalization and multi-module initialization cases. Done means the logging path no longer permits a reference to a destroyed object and the handler behavior is defined for modules initialized in one process.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
observability-sre
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.