fix(threadx): fix clock scaling math and TCB memory leak in system.c - #1278
Open
michael545 wants to merge 3 commits into
Open
fix(threadx): fix clock scaling math and TCB memory leak in system.c#1278michael545 wants to merge 3 commits into
michael545 wants to merge 3 commits into
Conversation
Signed-off-by: michael545 <michael.valand@gmail.com>
- Restore passive polling loop in _z_task_join waiting for TX_COMPLETED/TX_TERMINATED - Call tx_thread_delete in _z_task_join after thread exits to prevent TCB leak - Implement _z_task_cancel with tx_thread_terminate for forceful termination - Resolves review feedback from DenisBiryukov91 on PR eclipse-zenoh#1278 Signed-off-by: Michael Valand <michael.valand@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes timekeeping and task lifecycle issues in the ThreadX STM32 platform layer (src/system/threadx/stm32/system.c) to ensure correct clock scaling across tick rates and to prevent ThreadX thread control blocks (TCBs) from being left in a non-reusable state after task completion.
Changes:
- Fixes
__z_clock_gettime()time conversion by computing seconds + fractional nanoseconds from ticks without integer-truncation artifacts. - Ensures
_z_task_join()deletes the underlying ThreadX thread so the TCB can be reused after reconnect. - Fixes additional tick-to-time conversions (
_z_condvar_wait_until(),z_time_elapsed_s(),_z_get_time_since_epoch()), and implements_z_task_cancel()using ThreadX termination.
Suppressed comments (1)
src/system/threadx/stm32/system.c:105
- _z_task_cancel() terminates the thread but never deletes it. For ThreadX, tx_thread_terminate() does not remove the thread control block from kernel lists; without a subsequent tx_thread_delete(), the same TCB reuse/reconnect failure can still happen when cancel is used (similar to the original join leak).
z_result_t _z_task_cancel(_z_task_t *task) {
UINT status = tx_thread_terminate(&(task->threadx_thread));
if (status != TX_SUCCESS && status != TX_THREAD_ERROR) {
_Z_ERROR_RETURN(_Z_ERR_GENERIC);
}
return _Z_RES_OK;
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+86
to
+89
| UINT del_status = tx_thread_delete(&(task->threadx_thread)); | ||
| if (del_status != TX_SUCCESS && del_status != TX_THREAD_ERROR) { | ||
| _Z_ERROR_RETURN(_Z_ERR_GENERIC); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
What does this PR do?
This PR resolves two critical ThreadX PAL issues in
src/system/threadx/stm32/system.c:__z_clock_gettime()and_z_get_time_since_epoch()evaluated(TX_TIMER_TICKS_PER_SECOND / 1000)first. For 2000Hz tick rates,2000 / 1000 = 2, accelerating reported time by 4x. For <=500Hz tick rates,500 / 1000 = 0, freezing time at 0. This PR updates__z_clock_gettime()to calculate seconds byticks / TX_TIMER_TICKS_PER_SECONDand nanoseconds via remainder modulus._z_task_join()waited for task completion (TX_COMPLETED), but omittedtx_thread_delete(). On session reconnection,_z_task_init()invokedtx_thread_create()on the existing TCB, failing with0x0E(TX_THREAD_ERROR). This PR addstx_thread_delete(&(task->threadx_thread))in_z_task_join().Related Issues
Signed-off-by: michael545 michael.valand@gmail.com
🏷️ Label-Based Checklist
Based on the labels applied to this PR, please complete these additional requirements:
Labels:
bug🐛 Bug Fix Requirements
Since this PR is labeled as a bug fix, please ensure:
Why this matters: Bugs without tests often reoccur.
Instructions:
- [ ]to- [x])This checklist updates automatically when labels change, but preserves your checked boxes.