Skip to content

Conversation

snnn
Copy link
Member

@snnn snnn commented Sep 29, 2025

Move function-local static mutexes to file-scope statics.
This is to delay the destruction of mutexes, as in libc++ the destructor is non-trivial.

When a function-local static variable with a non-trivial destructor is defined, the compiler generates code to register its destruction at program exit. This can lead to order of destruction issues. By moving the mutex to a file-scope static, we ensure that it is initialized at program startup and its destructor is called at program exit, but in a more controlled manner. This avoids potential race conditions and other issues related to the order of destruction of static variables. The construction and destruction of std::mutex do not have any dependency other than the standard C++ runtime, therefore this change is safe.

Previously we made the mutexes function local because before VS 17.10 std::mutex's constructor was not constexpr(they cannot be initialized at compile time). So, it would cause constructor order problems if we didn't make them function local.

This PR replaces #25770 .

@snnn snnn linked an issue Sep 29, 2025 that may be closed by this pull request
Move function-local static mutexes to file-scope statics.
This is to delay the destruction of mutexes, as in libc++ the destructor is non-trivial.

When a function-local static variable with a non-trivial destructor is defined, the compiler generates code to register its destruction at program exit. This can lead to order of destruction issues, especially in a multi-threaded environment. By moving the mutex to a file-scope static, we ensure that it is initialized at program startup and its destructor is called at program exit, but in a more controlled manner. This avoids potential race conditions and other issues related to the order of destruction of static variables.
@snnn
Copy link
Member Author

snnn commented Oct 1, 2025

Will get a macOS machine and test this change.

@snnn
Copy link
Member Author

snnn commented Oct 7, 2025

I don't know what problem to fix ...

@snnn snnn changed the title Fix: Delay destruction of mutexes Delay destruction of mutexes Oct 8, 2025
#endif

#ifndef ORT_CONSTINIT
#define ORT_CONSTINIT
Copy link
Member

Choose a reason for hiding this comment

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

Overall, this might work, but we need to resolve this non C++20 case because w/o constinit static mutexes are worse than local function statics.

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently only macOS platform has this problem, and macOS build is using C++20.
I will continue to work on upgrading all pipelines to use C++20. I would prefer to get this PR merged before that work is done, since a lot of users are waiting for it. The constinit keyword is a sanitize check, which should not impact functionality.

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of an empty definition could we instead use #ifdef's in the files? If constinit is available, use that with an #ifdef around the file scope declaration. Otherwise have an #ifdef around the existing function scope declaration.

Copy link
Member Author

Choose a reason for hiding this comment

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

Then it will have very different behavior on different platforms, since function local statics are deallocated earlier than global vars. Then it will increase the complexity further.

Copy link
Member

@yuslepukhin yuslepukhin Oct 10, 2025

Choose a reason for hiding this comment

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

The ideal way to deal with it is to move the mutex into the structure it is trying to protect and not to have it static. I realize it may not be possible in every case, but I can see it is possible in some cases.

@snnn snnn requested a review from skottmckay October 8, 2025 20:42
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

3 participants