Skip to content

Leak logger mutex #25134

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

decahedron1
Copy link
Contributor

Description

Allowing the destructor for DefaultLoggerMutex to run may cause a crash on macOS in certain usages of ONNX Runtime (most notably, with the ort Rust bindings). Since the mutex is a static local variable, it's fine to just leak the memory.

Motivation and Context

Resolves #24579, #25038.

This change was applied to the ONNX Runtime binaries that ship with ort and no further issues have been reported.

static std::mutex& DefaultLoggerMutex() noexcept {
static std::mutex mutex;
static std::mutex* DefaultLoggerMutex() noexcept {
static std::mutex* mutex = new std::mutex();
Copy link
Member

Choose a reason for hiding this comment

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

Define it as a global var would be fine.
Then there is no memory leak and no ordering issue.

Copy link
Member

Choose a reason for hiding this comment

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

Oh .. I was wrong

Copy link
Member

Choose a reason for hiding this comment

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

I just asked Gemini. Unfortunately, the destructor of std::mutex on macOS platform is not trivial. This is really sad!!!!!

Copy link
Member

Choose a reason for hiding this comment

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

Could someone please help me confirm that? If we define std::mutex as a global var with default parameters:

std::mutext m;

Will its destructor call pthread_mutex_destroy ?

Copy link
Member

Choose a reason for hiding this comment

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

It seems that Android has the same problem.

@snnn
Copy link
Member

snnn commented Jun 26, 2025

But, this PR will introduce memory leaks. We have a memory leak checker in the windows_x64_debug pipeline, and it failed because of that. Do we have to make this change?

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.

mutex issue on Mac only for release 1.21.X only
2 participants