Skip to content
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

Fix for OpenMP thread affinity crashes on Android devices #1521

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

looechao
Copy link

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.

11 libomp.so __kmp_abort_process + 52
12 libomp.so __kmp_fatal + 128
13 libomp.so KMPNativeAffinity::Mask::set_system_affinity(bool) const + 208
14 libomp.so __kmp_affinity_set_init_mask + 864
15 libomp.so __kmp_parallel_initialize + 320
16 libomp.so __kmp_fork_call + 160
17 libomp.so __kmpc_fork_call + 236
18 libfluidsynth.so fluid_defsfont.c - line 398  fluid_defsfont_load_all_sampledata + 398
19 libfluidsynth.so fluid_defsfont.c - line 540  fluid_defsfont_load + 540
20 libfluidsynth.so fluid_defsfont.c - line 108  fluid_defsfloader_load + 108
21 libfluidsynth.so fluid_synth.c - line 5365  fluid_synth_sfload + 5365

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

  • Xiaomi Redmi Note 13 Pro | Redmi Note12 Turbo | Xiaomi 12Pro | Xiaomi 12 ultra
  • Confirmed on:
    • Xiaomi Redmi Note 13 Pro (Android 15)
    • Xiaomi 12 Pro

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.

@derselbst derselbst changed the title WIP: Fix for OpenMP thread affinity crashes on Android devices Fix for OpenMP thread affinity crashes on Android devices Mar 29, 2025
Copy link
Member

@derselbst derselbst left a comment

Choose a reason for hiding this comment

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

Excellent finding, thanks! I moved your code to fluid_synth_init(), which will be executed before the first synth instance is created. This will ensure the workaround getting applied, no matter how people are using fluidsynth (i.e. executable vs. via API only).
Feel free to test it. Android binaries will shortly become available here: https://dev.azure.com/tommbrt/tommbrt/_build/results?buildId=12127&view=artifacts&pathAsName=false&type=publishedArtifacts

@derselbst derselbst added the bug label Apr 6, 2025
@derselbst derselbst added this to the 2.4 milestone Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants