Skip to content

Fix shared ptr circular reference leaks#40480

Open
chemwolf6922 wants to merge 1 commit intomasterfrom
user/chemwolf6922/fix-shared-ptr-circular-referece-leak
Open

Fix shared ptr circular reference leaks#40480
chemwolf6922 wants to merge 1 commit intomasterfrom
user/chemwolf6922/fix-shared-ptr-circular-referece-leak

Conversation

@chemwolf6922
Copy link
Copy Markdown
Contributor

@chemwolf6922 chemwolf6922 commented May 9, 2026

Summary of the Pull Request

Both loggers have their member thread holding a shared pointer of themselves. This renders the m_exitEvents logic useless. And could lead to resource leaks.
Since the thread's lifespan is always shorter than the logger's, changing the capture from shared_from_this to this will fix the leak.

PR Checklist

  • Closes: Wslservice.exe leaks memory and threads #40470
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Co-authored-by: Copilot <copilot@github.com>
Copilot AI review requested due to automatic review settings May 9, 2026 09:24
@chemwolf6922 chemwolf6922 requested a review from a team as a code owner May 9, 2026 09:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a thread/lifetime ownership cycle in the Windows-side telemetry loggers that could prevent destruction and lead to leaked threads/resources over long uptimes (as reported in #40470). It removes shared_from_this() captures from the worker thread lambdas so the logger objects can be torn down normally.

Changes:

  • Remove std::enable_shared_from_this inheritance from GuestTelemetryLogger and DmesgCollector.
  • Change worker thread lambdas to capture this instead of a shared_ptr to self, breaking the circular ownership.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/windows/service/exe/GuestTelemetryLogger.h Drops enable_shared_from_this inheritance to avoid self-owning patterns.
src/windows/service/exe/GuestTelemetryLogger.cpp Updates worker thread capture from shared_from_this() to this to break the ref cycle.
src/windows/common/Dmesg.h Drops enable_shared_from_this inheritance for the collector.
src/windows/common/Dmesg.cpp Updates dmesg worker thread capture from shared_from_this() to this to break the ref cycle.
Comments suppressed due to low confidence (1)

src/windows/service/exe/GuestTelemetryLogger.cpp:83

  • The worker thread treats shutdown as an exception path: helpers::ConnectPipe throws E_ABORT when any exit event is signaled (including m_threadExit from the destructor). With CATCH_LOG() this will be logged as an error even though it’s expected during teardown; consider catching explicitly and suppressing/logging-at-debug when wil::ResultFromCaughtException() == E_ABORT (similar to DmesgCollector’s handling).
            }
        }
        CATCH_LOG()
    });

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