open-telemetry / open-telemetry/opentelemetry-cpp
GlobalLogHandler usage may lead to UB
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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