Skip to content

Simplify logManager configuration initialization#1403

Open
thomasameisel wants to merge 4 commits intomainfrom
thmeisel/Config-Crash-Fix
Open

Simplify logManager configuration initialization#1403
thomasameisel wants to merge 4 commits intomainfrom
thmeisel/Config-Crash-Fix

Conversation

@thomasameisel
Copy link
Copy Markdown
Contributor

@thomasameisel thomasameisel commented Jan 20, 2026

Refactor logManager configuration handling to use default configuration directly.

We are seeing a crash that originates in the 1DS SDK. See this issue and this issue for more details on the crash including crash logs. The call stack is crashing when it assigns a C++ object ILogConfiguration to a function-level static property in the ODWLogManager class during static log configuration. I don't think this is needed and by removing it we can simplify this code and avoid a crash.

The issue was that we are creating a new ILogConfiguration object from the config NSDictionary that is passed into this function. Since nothing was holding onto this new ILogConfiguration object it would get deallocated.

Instead we can merge the config NSDictionary into the static log configuration object. This way we don't need to hold onto the new ILogConfiguration object that is created because we are merging into the static log configuration. We also avoid copying the static log configuration object or needing to keep an extra static reference to it.

@thomasameisel thomasameisel requested a review from a team as a code owner January 20, 2026 18:58
Comment thread wrappers/obj-c/ODWLogManager.mm Outdated
auto& defaultConfig = LogManager::GetLogConfiguration();
logManagerConfig = defaultConfig;
// Get reference to the default configuration
ILogConfiguration& logManagerConfig = LogManager::GetLogConfiguration();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is the case, but I'd like to verify that we'll continue holding a strong reference to the default configuration object even if we don't assign it to a static variable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LogManagerBase::GetLogConfiguration implements this function using a function-level static variable

@thomasameisel
Copy link
Copy Markdown
Contributor Author

I dug into the history of this crash some more and found this issue. The issue was that we are creating a new ILogConfiguration object from the config NSDictionary that is passed into this function. Since nothing was holding onto this new ILogConfiguration object it would get deallocated.

I just pushed a new commit that merges the config NSDictionary into the static log configuration object. This way we don't need to hold onto the new ILogConfiguration object that is created because we are merging into the static log configuration. We also avoid copying the static log configuration object or needing to keep an extra static reference to it.

@lalitb
Copy link
Copy Markdown
Contributor

lalitb commented Jan 22, 2026

(Note: I'm reviewing this from a C++ perspective and don't have expertise in Obj-C/iOS specifics.)

I'm having trouble connecting this change to the reported crash. In the Obj-C path, the existing code already uses a static ILogConfiguration, and FromJSON deep-copies string values (Variant stores its own std::string), so there shouldn't be a dangling-string lifetime issue.

This PR changes behavior in a few ways :

  • It mutates the singleton config in place rather than copying defaults into a local static
  • A second call (or concurrent call) will now merge values into the live LogManager's config instead of starting fresh

If the crash was due to dangling pointers, can you point to which config entries are void* in this iOS path? That's the only Variant type that does shallow copy. Without a stack trace or repro case, it's hard to validate that this addresses the root cause. Could you clarify the exact crash and how this change fixes it?

@thomasameisel
Copy link
Copy Markdown
Contributor Author

thomasameisel commented Jan 22, 2026

(Note: I'm reviewing this from a C++ perspective and don't have expertise in Obj-C/iOS specifics.)

I'm having trouble connecting this change to the reported crash. In the Obj-C path, the existing code already uses a static ILogConfiguration, and FromJSON deep-copies string values (Variant stores its own std::string), so there shouldn't be a dangling-string lifetime issue.

This PR changes behavior in a few ways :

  • It mutates the singleton config in place rather than copying defaults into a local static
  • A second call (or concurrent call) will now merge values into the live LogManager's config instead of starting fresh

LogManagerBase::Initialize does something similar when passing in a configuration object that is a different reference from the default. So while the ODWLogManager behavior is different, I believe the overall static log manager behavior should remain the same.

If the crash was due to dangling pointers, can you point to which config entries are void* in this iOS path? That's the only Variant type that does shallow copy. Without a stack trace or repro case, it's hard to validate that this addresses the root cause. Could you clarify the exact crash and how this change fixes it?

@maxgolov elaborated on the issue with the copy in this comment:

I think the issue is somewhere in copying the const char* values, as in - pointers to values, from temporary. When temporary container is gone, the memory used by container (map) values is reclaimed. Thus, the shallow copy of configuration values in a different spot is now referencing memory that's already been freed / reused / allocated by something else. This is obviously not an issue for older code, where you operated on 'default' configuration object (globally initialized once by static initializer), that is never destroyed. Please keep your incoming configuration object permanent, or static, that should solve it. One way to handle it for your scenario is to use magic static getter for your configuration object - move its populate in a separate method, and return a reference to static. This is thread-safe in C++. Pattern described here: https://blog.mbedded.ninja/programming/languages/c-plus-plus/magic-statics/#singletons

I attached 4 crash logs at the bottom of this issue. From what I can tell the issue is happening in the assignment operator so avoiding the need to use the assignment operator would fix the crash. I'd be happy to iterate on this change if you have another suggestion @lalitb.

@lalitb
Copy link
Copy Markdown
Contributor

lalitb commented Jan 22, 2026

I attached 4 crash logs at the bottom https://github.com/microsoft/cpp_client_telemetry_modules/issues/315. From what I can tell the issue is happening in the assignment operator so avoiding the need to use the assignment operator would fix the crash. I'd be happy to iterate on this change if you have another suggestion @lalitb.

Looking at a couple of crash traces, they indeed show the same pattern: crash during iteration of ILogConfiguration’s internal VariantMap while copying from GetLogConfiguration(). By the time the copy happens, the map is already corrupted.
This PR removes that copy, which should avoid this immediate crash. It doesn’t address why the map was corrupted. The ObjC surface doesn’t enforce thread-safe access to the shared config:

  • ODWLogConfiguration setters write to GetLogConfiguration() without locking
  • initForTenant:withConfig: has no synchronization guard
  • _initialized isn’t atomic

If multiple queues configure telemetry during startup, they can race on the shared config; the offending thread may have finished by the time we see the crash. Based on code and stack traces (not as an ObjC/iOS expert), adding synchronization in the ObjC layer (e.g., @synchronized or a serial queue) around config access seems like the right way to address the underlying race - worth a sanity check from someone more familiar with this wrapper.

@lalitb
Copy link
Copy Markdown
Contributor

lalitb commented Jan 22, 2026

Thinking more, I believe instead of fixing the SDK or ObjC wrapper - For the underlying race, the application should ensure serialized access to the SDK during initialization - e.g., configure and initialize from a single thread/queue rather than calling setters and initForTenant: from multiple queues concurrently.

Do you see any such pattern in your application ?

@thomasameisel
Copy link
Copy Markdown
Contributor Author

thomasameisel commented Jan 22, 2026

Thinking more, I believe instead of fixing the SDK or ObjC wrapper - For the underlying race, the application should ensure serialized access to the SDK during initialization - e.g., configure and initialize from a single thread/queue rather than calling setters and initForTenant: from multiple queues concurrently.

Yes, I've verified through code inspection and setting breakpoints that in iOS Outlook we only configure the static log manager once from a single thread (either the main thread or a serial background queue). We also initialize new log managers and loggers from that same thread.

I'm wondering if the issue has to do with this comment in VariantType.hpp:

    union
    {
        int64_t     iV;
        double      dV;
        const char* sV;
        bool        bV;
        void*       pV;
    };

    // Unfortunately keeping object pointers inside the union above causes issues
    // with C++11 static initializer feature. The pointers get corrupted and calling
    // destructor via delete causes a crash with MSVC compiler (both 2015 and 2017).

Adding threading synchronization (or documentation that this API is not thread-safe) would make sense, but I'm not sure if it's the root cause of this crash.

@maxgolov
Copy link
Copy Markdown
Contributor

I think your changes look good. But I'd like to share that we hit some very odd bit-packing issues with different compilers in Microsoft Mesh product. Many of these issues were solved in my branch, that was unfortunately not merged in the main. I'd suggest to use Claude Sonnet/Opus 4.5 or GPT-5.2 to review the contents of the branch, and see what changes could be potentially backported to the main, and/or your PR. I apologize for never merging this in the main, as we had a separate repo with the product-specific fixes. I am no longer involved in Observability, you might find some fixes relevant to your cross-platform scenario. We did ran our spinoff of the SDK on Apple Vision Pro, for example.. It was working alright, but our usage patterns were potentially different than yours.

Sorry that I can't be of much help #1122 and #1099 might be relevant. There was also a good comment in one of these PRs that some things are really C++ compiler settings-specific, e.g. packing and alignment. You may want to look closely at Apple Silicon ARM64, if you are hitting some issues specifically on that newer platform.

Hope that is not a noise and hope that helps. Use GitHub Copilot to navigate the changes, as these are quite extensive. But you should be able to grok these now, as these were written before vibe-coding became so prominently available.

@lalitb
Copy link
Copy Markdown
Contributor

lalitb commented Jan 22, 2026

I'm wondering if the issue has to do with this comment in VariantType.hpp:

That comment is about an old MSVC quirk: putting object pointers in the union caused issues with MSVC’s handling of magic statics and would crash on destruction. On Clang/iOS it doesn’t apply, and the code moved std::string/maps out of the union to avoid it. The crash stacks we’re seeing are during map assignment/initialization, not destruction, so this MSVC note isn’t likely related.

@ThomsonTan ThomsonTan force-pushed the thmeisel/Config-Crash-Fix branch from ff973d8 to 49f9832 Compare January 28, 2026 21:49
@lalitb
Copy link
Copy Markdown
Contributor

lalitb commented Feb 3, 2026

@thomasameisel - wondering if you are still working on this PR to get it merged?

@microsoft-github-policy-service
Copy link
Copy Markdown

@thomasameisel please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"
Contributor License Agreement

Contribution License Agreement

This Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
and conveys certain license rights to Microsoft Corporation and its affiliates (“Microsoft”) for Your
contributions to Microsoft open source projects. This Agreement is effective as of the latest signature
date below.

  1. Definitions.
    “Code” means the computer software code, whether in human-readable or machine-executable form,
    that is delivered by You to Microsoft under this Agreement.
    “Project” means any of the projects owned or managed by Microsoft and offered under a license
    approved by the Open Source Initiative (www.opensource.org).
    “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any
    Project, including but not limited to communication on electronic mailing lists, source code control
    systems, and issue tracking systems that are managed by, or on behalf of, the Project for the purpose of
    discussing and improving that Project, but excluding communication that is conspicuously marked or
    otherwise designated in writing by You as “Not a Submission.”
    “Submission” means the Code and any other copyrightable material Submitted by You, including any
    associated comments and documentation.
  2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any
    Project. This Agreement covers any and all Submissions that You, now or in the future (except as
    described in Section 4 below), Submit to any Project.
  3. Originality of Work. You represent that each of Your Submissions is entirely Your original work.
    Should You wish to Submit materials that are not Your original work, You may Submit them separately
    to the Project if You (a) retain all copyright and license information that was in the materials as You
    received them, (b) in the description accompanying Your Submission, include the phrase “Submission
    containing materials of a third party:” followed by the names of the third party and any licenses or other
    restrictions of which You are aware, and (c) follow any other instructions in the Project’s written
    guidelines concerning Submissions.
  4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else
    for whom You are acting in making Your Submission, e.g. as a contractor, vendor, or agent. If Your
    Submission is made in the course of Your work for an employer or Your employer has intellectual
    property rights in Your Submission by contract or applicable law, You must secure permission from Your
    employer to make the Submission before signing this Agreement. In that case, the term “You” in this
    Agreement will refer to You and the employer collectively. If You change employers in the future and
    desire to Submit additional Submissions for the new employer, then You agree to sign a new Agreement
    and secure permission from the new employer before Submitting those Submissions.
  5. Licenses.
  • Copyright License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license in the
    Submission to reproduce, prepare derivative works of, publicly display, publicly perform, and distribute
    the Submission and such derivative works, and to sublicense any or all of the foregoing rights to third
    parties.
  • Patent License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under
    Your patent claims that are necessarily infringed by the Submission or the combination of the
    Submission with the Project to which it was Submitted to make, have made, use, offer to sell, sell and
    import or otherwise dispose of the Submission alone or with the Project.
  • Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement.
    No additional licenses or rights whatsoever (including, without limitation, any implied licenses) are
    granted by implication, exhaustion, estoppel or otherwise.
  1. Representations and Warranties. You represent that You are legally entitled to grant the above
    licenses. You represent that each of Your Submissions is entirely Your original work (except as You may
    have disclosed under Section 3). You represent that You have secured permission from Your employer to
    make the Submission in cases where Your Submission is made in the course of Your work for Your
    employer or Your employer has intellectual property rights in Your Submission by contract or applicable
    law. If You are signing this Agreement on behalf of Your employer, You represent and warrant that You
    have the necessary authority to bind the listed employer to the obligations contained in this Agreement.
    You are not expected to provide support for Your Submission, unless You choose to do so. UNLESS
    REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, AND EXCEPT FOR THE WARRANTIES
    EXPRESSLY STATED IN SECTIONS 3, 4, AND 6, THE SUBMISSION PROVIDED UNDER THIS AGREEMENT IS
    PROVIDED WITHOUT WARRANTY OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF
    NONINFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  2. Notice to Microsoft. You agree to notify Microsoft in writing of any facts or circumstances of which
    You later become aware that would make Your representations in this Agreement inaccurate in any
    respect.
  3. Information about Submissions. You agree that contributions to Projects and information about
    contributions may be maintained indefinitely and disclosed publicly, including Your name and other
    information that You submit with Your Submission.
  4. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and
    the parties consent to exclusive jurisdiction and venue in the federal courts sitting in King County,
    Washington, unless no federal subject matter jurisdiction exists, in which case the parties consent to
    exclusive jurisdiction and venue in the Superior Court of King County, Washington. The parties waive all
    defenses of lack of personal jurisdiction and forum non-conveniens.
  5. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and
    supersedes any and all prior agreements, understandings or communications, written or oral, between
    the parties relating to the subject matter hereof. This Agreement may be assigned by Microsoft.

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.

4 participants