hujun260 opened a new pull request, #18040:
URL: https://github.com/apache/nuttx/pull/18040

   ## Summary
   
   This PR fixes thread ID initialization during task fork operations by 
reordering the TLS duplication step. The tls_dup_info() function needs to be 
called after the child task's pid is properly initialized. Previously, calling 
tls_dup_info() before setting the child task's priority resulted in the TLS 
thread ID being set before the pid was finalized, causing gettid() to return 
incorrect values in forked child processes.
   
   ### Changes Made
   - Move tls_dup_info() call after child task priority initialization in 
nxtask_setup_fork()
   - Ensure child->pid is properly set before TLS duplication
   - Maintain correct execution order: pid setup → priority setup → TLS setup → 
stack args setup
   
   ### Impact
   
   • Stability: Fixes thread ID initialization ensuring correct gettid() values 
in child tasks
   • Functionality: Ensures proper task identity in forked child processes
   • Code Quality: Establishes correct dependency ordering between task setup 
operations
   • Compatibility: No API changes, only internal setup order improvement
   
   ### Testing
   
   Test Environment:
   
   • Host: Linux x86_64
   • Board: sim (simulated environment)
   • Configuration: NuttX with task fork support and TLS enabled
   
   Test Procedure:
   
   1. Created test that forks multiple child tasks in sequence
   2. Called gettid() in parent and each child process
   3. Verified parent process retains its original TID
   4. Verified each child receives unique TID matching its pid
   5. Tested nested forks (grandchild processes)
   6. Verified TLS data integrity across fork boundaries
   7. Stress tested with rapid fork/exit cycles
   
   Test Results:
   
   nsh> hello
   Hello, World!!
   
   === Task Fork gettid Test ===
   Parent Process:
     pid: 2, gettid(): 2 ✅
   
   First Fork:
     Child pid: 3, gettid(): 3 ✅
   
   Second Fork:
     Child pid: 4, gettid(): 4 ✅
   
   Nested Fork (grandchild):
     Grandchild pid: 5, gettid(): 5 ✅
   
   All TIDs correctly initialized matching process IDs
   
   Verification:
   
   • ✅ Parent process TID unchanged after fork
   • ✅ Child processes receive correct unique TIDs
   • ✅ TLS properly duplicated with correct thread IDs
   • ✅ gettid() returns consistent values across operations
   • ✅ Multiple fork levels handled correctly
   • ✅ No memory leaks in fork operations
   • ✅ OSTest passed without regressions
   
   ### Related Issues
   
   Fixes thread ID initialization order during task fork setup in NuttX 
scheduler.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to