Skip to content

system/settings: Fix non-cached save deadlock#3500

Open
nightt5879 wants to merge 2 commits into
apache:masterfrom
nightt5879:nightt5879/fix-settings-sync-deadlock-3105
Open

system/settings: Fix non-cached save deadlock#3500
nightt5879 wants to merge 2 commits into
apache:masterfrom
nightt5879:nightt5879/fix-settings-sync-deadlock-3105

Conversation

@nightt5879
Copy link
Copy Markdown
Contributor

Summary

Fixes #3105.

This PR fixes the settings deadlock that can happen when CONFIG_SYSTEM_SETTINGS_CACHED_SAVES is disabled.

Commit structure:

  • Commit 1 (system/settings: Avoid recursive save locking) is the strict [BUG] Deadlock issue in system/settings when CONFIG_SYSTEM_SETTINGS_CACHED_SAVES is not set #3105 fix. The immediate save path now writes pending settings through a helper that assumes g_settings.mtx is already held, instead of calling the timer callback that locks the same mutex again. The cached-save timer callback still takes the mutex before using the same helper.
  • Commit 2 (system/settings: Drop unused recursive mutex type) is a small related cleanup. Since the settings save path no longer re-enters g_settings.mtx, the mutex no longer needs PTHREAD_MUTEX_RECURSIVE; this also keeps the fix independent of CONFIG_PTHREAD_MUTEX_TYPES. It also destroys the temporary mutex attribute object after initialization.

The second commit is logically separable and I am happy to drop it if maintainers consider it out of scope for #3105.

Out of scope:

Impact

With cached saves disabled, settings operations that save immediately no longer try to lock g_settings.mtx recursively via dump_cache().

  • New feature: NO
  • User adaptation required: NO
  • Build process change: NO
  • Hardware/architecture/board change: NO
  • Documentation update required: NO
  • Security impact: NO intended security impact
  • Compatibility impact: NO intended compatibility impact

Testing

Host:

  • Windows with WSL Ubuntu 24.04
  • CPU: x86_64
  • Compiler: GCC 13.3.0

Checks:

  • git diff --check upstream/master..HEAD: pass
  • checkpatch.sh -c -u -m -g HEAD~2..HEAD: pass
  • sim:nsh build: pass
    • CONFIG_SYSTEM_SETTINGS=y
    • # CONFIG_SYSTEM_SETTINGS_CACHED_SAVES is not set
    • # CONFIG_PTHREAD_MUTEX_TYPES is not set
    • confirmed CC: settings.c
    • result: SIM elf with dynamic libs archive in nuttx.tgz

Note: the temporary test build appended config overrides after configuring sim:nsh, so .config printed expected override warnings for the settings and pthread mutex options.

Fix apache#3105 by making the immediate save path write pending settings while the caller still owns g_settings.mtx instead of calling the timer callback, which locks the same mutex again.

The cached-save timer callback still takes the mutex before using the same locked helper, so asynchronous saves keep their existing synchronization while non-cached saves no longer depend on a recursive mutex.

Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
The settings save path no longer re-enters g_settings.mtx, so the mutex does not need to be initialized as PTHREAD_MUTEX_RECURSIVE.

This keeps the apache#3105 fix independent of CONFIG_PTHREAD_MUTEX_TYPES and destroys the temporary mutex attribute object after initialization.

Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
@nightt5879 nightt5879 marked this pull request as ready for review May 25, 2026 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Deadlock issue in system/settings when CONFIG_SYSTEM_SETTINGS_CACHED_SAVES is not set

2 participants