Skip to content

fix(threadx): fix clock scaling math and TCB memory leak in system.c - #1278

Open
michael545 wants to merge 3 commits into
eclipse-zenoh:mainfrom
michael545:fix/threadx-stm32-system-pal
Open

fix(threadx): fix clock scaling math and TCB memory leak in system.c#1278
michael545 wants to merge 3 commits into
eclipse-zenoh:mainfrom
michael545:fix/threadx-stm32-system-pal

Conversation

@michael545

@michael545 michael545 commented Jul 28, 2026

Copy link
Copy Markdown

Description

What does this PR do?

This PR resolves two critical ThreadX PAL issues in src/system/threadx/stm32/system.c:

  1. Clock Math Integer Truncation: __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 by ticks / TX_TIMER_TICKS_PER_SECOND and nanoseconds via remainder modulus.
  2. Task Control Block Memory Leak: _z_task_join() waited for task completion (TX_COMPLETED), but omitted tx_thread_delete(). On session reconnection, _z_task_init() invoked tx_thread_create() on the existing TCB, failing with 0x0E (TX_THREAD_ERROR). This PR adds tx_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:

  • Root cause documented - Explain what caused the bug in the PR description
  • Reproduction test added - Test that fails on main branch without the fix
  • Test passes with fix - The reproduction test passes with your changes
  • Regression prevention - Test will catch if this bug reoccurs in the future
  • Fix is minimal - Changes are focused only on fixing the bug
  • Related bugs checked - Verified no similar bugs exist in related code

Why this matters: Bugs without tests often reoccur.

Instructions:

  1. Check off items as you complete them (change - [ ] to - [x])
  2. The PR checklist CI will verify these are completed

This checklist updates automatically when labels change, but preserves your checked boxes.

@DenisBiryukov91 DenisBiryukov91 added the bug Something isn't working label Aug 18, 2026
Comment thread src/system/threadx/stm32/system.c Outdated
- 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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);
}
Comment thread src/system/threadx/stm32/system.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

3 participants