mimalloc: prevent crashes in the post-command hook handling
#1185
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In 3e1d800 (potential fix for windows static linking with thread creation in dll's, 2022-11-07), mimalloc was changed to ensure that certain code paths use "weak" random seeds when initializing mimalloc.
This was a crucial fix for issues that were also discovered (and fixed independently) in Microsoft Git, where initializing with "strong" random seeds could cause Access Violations in
bcryptPrimitves.dll. The symptom would look like this:This symptom resurfaced in Microsoft Git, and the root cause for this regression is that 710d613 (refactor thread meta-data initilazation, upstream of python/cpython#113263, 2024-05-18) renamed the original
_mi_heap_init()function (which did contain that weak random seed special-case)to_mi_thread_heap_init(), and implemented a new_mi_heap_init()that does not special-case the weak random seed initialization.The good news is that we can now characterize much better under what circumstances this crash occurs: The code path in which this Access Violation occurs is in an
atexit()handler. That is important because mimalloc's ownatexit()handler has deinitialized mimalloc just before that handler wants to allocate something using mimalloc, which now faithfully tries to reinitialize mimalloc. And that's apparently the crux: something inbcryptPrimitives.dllis totally unhappy to initialize things when already running in theatexit()phase.This patch lets
_mi_heap_init()once again use the week random seed when linking statically.An alternative approach to address this might be to figure out what exactly is bothering
bcryptPrimitives.dllwhen callingBCryptGenRandom()in anatexit()handler, and then trying to accommodate BCrypt's needs, but that looks like a tough nugget to crack.