Fix for OpenMP thread affinity crashes on Android devices #1521
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.
Issue
FluidSynth crashes on multiple Android devices (predominantly Xiaomi models) when loading SoundFonts.
These crashes occur when OpenMP attempts to set thread affinity during parallel sample loading in
fluid_defsfont_load_all_sampledata().
Crash stack trace shows failure in KMPNativeAffinity::Mask::set_system_affinity() with EINVAL error.
Root Cause
This is a documented issue in Android NDK's OpenMP implementation. When thread affinity setting fails on certain devices, libomp.so terminates the process instead of handling the error gracefully. The issue (Android NDK #1180) remains unresolved as it's difficult to reproduce. It still affects Xiaomi/Redmi devices.
Solution
Disable KMP_AFFINITY on Android by setting the environment variable early in initialization. This approach is successfully used in other libraries (e.g., Tencent's ncnn).
Affected Devices
References
Note: While setting KMP_AFFINITY=disabled in JNI_OnLoad successfully resolves the crashes in our application, I have not yet verified if placing this setting within FluidSynth's initialization will have the same effect. This issue is raised to bring attention to the problem and propose a potential solution that worked at the application level.