Skip to content

[Prior 1] dlt_user: fix deadlock by stopping threads before locking dlt_mutex - #915

Open
minminlittleshrimp wants to merge 2 commits into
masterfrom
fix-dlt-user-deadlock
Open

minminlittleshrimp wants to merge 2 commits into
masterfrom
fix-dlt-user-deadlock

Conversation

@minminlittleshrimp

@minminlittleshrimp minminlittleshrimp commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

dlt_user: fix deadlock by stopping threads before locking dlt_mutex
dlt_free() held dlt_mutex while calling dlt_stop_threads(), but the
housekeeper thread needs dlt_mutex to proceed (e.g. inside
dlt_user_log_check_user_message). Since pthread_mutex_lock is not a
cancellation point, pthread_cancel could not interrupt the blocked
housekeeper, causing pthread_join to deadlock.

  • Move dlt_stop_threads() before dlt_mutex_lock() in dlt_free()
  • Add cooperative exit check at top of housekeeper loop (~500ms latency)
  • On MSYS2/MinGW: cooperative wait (1s) + pthread_cancel fallback
  • On Linux/other: pthread_cancel directly (no added latency)
  • Reset dlt_user_housekeeper_exit_requested in dlt_start_threads()
    to prevent stale flag from previous dlt_free() cycle
  • Guard CLOCK_MONOTONIC for MSYS2/MinGW in condattr and clock_gettime
  • Add pthread_condattr_destroy() after pthread_cond_init()

Signed-off-by: LUU QUANG MINH Minh.LuuQuang@vn.bosch.com

@minminlittleshrimp

minminlittleshrimp commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator Author

Hello @santhoshsivanhere
here we go again, leaks show up everywhere :)) https://github.com/COVESA/dlt-daemon/actions/runs/34745962782/job/103693843991?pr=915
Please review this PR, and as it shows leaks, I propose to fix them in this PR also, what do ya think?

@minminlittleshrimp minminlittleshrimp changed the title [Prior 1] Fix MSYS2 deadlock in dlt_user.c init/free and add TSAN CI job [Prior 1] Fix MSYS2 deadlock in dlt_user.c init/free and add TSAN CI job - propose to fix memleak also??? Sep 14, 2026
@minminlittleshrimp

Copy link
Copy Markdown
Collaborator Author

Discussion has been made, split this into 3 PRs.

  • Fix deadlock libdlt
  • Fix memleak for also protocol v2 APIs
  • Introduce sanitizer pipe

@minminlittleshrimp
minminlittleshrimp force-pushed the fix-dlt-user-deadlock branch 3 times, most recently from 463f644 to d9f4aa7 Compare September 21, 2026 17:13
@minminlittleshrimp minminlittleshrimp changed the title [Prior 1] Fix MSYS2 deadlock in dlt_user.c init/free and add TSAN CI job - propose to fix memleak also??? [Prior 1] dlt_user: fix deadlock by stopping threads before locking dlt_mutex Sep 21, 2026
dlt_free() held dlt_mutex while calling dlt_stop_threads(), but the
housekeeper thread needs dlt_mutex to proceed (e.g. inside
dlt_user_log_check_user_message). Since pthread_mutex_lock is not a
cancellation point, pthread_cancel could not interrupt the blocked
housekeeper, causing pthread_join to deadlock.

- Move dlt_stop_threads() before dlt_mutex_lock() in dlt_free()
- Add cooperative exit check at top of housekeeper loop (~500ms latency)
- On MSYS2/MinGW: cooperative wait (1s) + pthread_cancel fallback
- On Linux/other: pthread_cancel directly (no added latency)
- Reset dlt_user_housekeeper_exit_requested in dlt_start_threads()
  to prevent stale flag from previous dlt_free() cycle
- Guard CLOCK_MONOTONIC for MSYS2/MinGW in condattr and clock_gettime
- Add pthread_condattr_destroy() after pthread_cond_init()

Signed-off-by: LUU QUANG MINH <Minh.LuuQuang@vn.bosch.com>
Replace 'SpacesInParens: Never' (clang-format 16+) with
'SpacesInParentheses: false' (clang-format 14 compatible).

Signed-off-by: LUU QUANG MINH <Minh.LuuQuang@vn.bosch.com>

This branch has not been deployed

No deployments
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.

1 participant