Skip to content

modules: release kernel stack when deleting module threads - #692

Open
prashit-vora wants to merge 1 commit into
eclipse-threadx:devfrom
prashit-vora:fix/module-thread-kernel-stack-leak
Open

modules: release kernel stack when deleting module threads#692
prashit-vora wants to merge 1 commit into
eclipse-threadx:devfrom
prashit-vora:fix/module-thread-kernel-stack-leak

Conversation

@prashit-vora

@prashit-vora prashit-vora commented Sep 3, 2026

Copy link
Copy Markdown

Fixes #400.

User-mode module threads allocate a separate kernel stack from the module object pool. The thread-delete dispatcher freed the TX_THREAD object but left that kernel stack allocated.

This change releases the kernel stack after _txe_thread_delete() succeeds and before releasing the thread object.

Testing:

  • Built the Cortex-M3 GNU module and module-manager images.
  • Ran the reproduction in QEMU using mps2-an385.
  • Without the fix, tx_thread_create() returned TX_NO_MEMORY after 18 create/delete cycles.
  • With the fix, all 64 cycles completed.
Temporary QEMU reproduction
#define TXM_MODULE

#include "txm_module.h"

#define LEAK_TEST_ITERATION_COUNT  64
#define LEAK_TEST_STACK_SIZE       512

#define LEAK_TEST_PASS             0x600D600DUL
#define LEAK_TEST_ALLOCATE_FAILED  0xA1000000UL
#define LEAK_TEST_CREATE_FAILED    0xA2000000UL
#define LEAK_TEST_TERMINATE_FAILED 0xA3000000UL
#define LEAK_TEST_DELETE_FAILED    0xA4000000UL

TX_THREAD     *leak_test_thread_ptr;
ULONG          leak_test_stack[LEAK_TEST_STACK_SIZE / sizeof(ULONG)];
volatile ULONG leak_test_iterations;
volatile ULONG leak_test_result;

static void leak_test_thread_entry(ULONG input)
{
    (void) input;
}

void demo_module_start(ULONG id)
{
UINT iteration;
UINT status;

    (void) id;
    leak_test_iterations = 0;
    leak_test_result = 0;

    for (iteration = 0; iteration < LEAK_TEST_ITERATION_COUNT; iteration++)
    {
        status = txm_module_object_allocate((VOID **) &leak_test_thread_ptr,
                                            sizeof(TX_THREAD));
        if (status != TX_SUCCESS)
        {
            leak_test_result = LEAK_TEST_ALLOCATE_FAILED | status;
            break;
        }

        status = tx_thread_create(leak_test_thread_ptr, "leak test thread",
                                  leak_test_thread_entry, 0,
                                  leak_test_stack, sizeof(leak_test_stack),
                                  16, 16, TX_NO_TIME_SLICE, TX_DONT_START);
        if (status != TX_SUCCESS)
        {
            leak_test_result = LEAK_TEST_CREATE_FAILED | status;
            break;
        }

        status = tx_thread_terminate(leak_test_thread_ptr);
        if (status != TX_SUCCESS)
        {
            leak_test_result = LEAK_TEST_TERMINATE_FAILED | status;
            break;
        }

        status = tx_thread_delete(leak_test_thread_ptr);
        if (status != TX_SUCCESS)
        {
            leak_test_result = LEAK_TEST_DELETE_FAILED | status;
            break;
        }

        leak_test_iterations = iteration + 1;
    }

    if (leak_test_iterations == LEAK_TEST_ITERATION_COUNT)
    {
        leak_test_result = LEAK_TEST_PASS;
    }

}
QEMU results
Parent revision without the fix:
leak_test_iterations = 18
leak_test_result = 0xA2000010
                       ^ create failed with TX_NO_MEMORY

Revision with the fix:
leak_test_iterations = 64
leak_test_result = 0x600D600D
                       ^ pass

NOTE: This was not tested on physical hardware.

Signed-off-by: Prashit Vora <prashitvora2006@gmail.com>
@prashit-vora
prashit-vora force-pushed the fix/module-thread-kernel-stack-leak branch from b9f7bbb to 7f24633 Compare September 3, 2026 10:50
@prashit-vora

Copy link
Copy Markdown
Author

Hi @fdesbiens, Could you approve the workflow runs for this PR?

@prashit-vora

Copy link
Copy Markdown
Author

@fdesbiens could you review it ? i think its ready to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant