[PR #19673] fixed thread sync and fixed loop condition , also the pipelistener loop is fixed #31918

Open
opened 2026-01-31 09:50:23 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/19673

State: closed
Merged: No


Thread Handle Check---
cppHANDLE hPipeListenerThread{ reinterpret_cast(_beginthread(PipeListener, 0, hPipeIn)) };
_beginthread() returns (uintptr_t)-1 on error, but there's no error checking before proceeding.
Process Handle Cleanup---
The code closes piClient.hThread and piClient.hProcess even if CreateProcess failed, which could close invalid handles.
Thread Synchronization---
Using Sleep(500) is unreliable for thread synchronization. The listener thread may still be running when cleanup begins.
PipeListener Loop Condition---

dwBytesRead is DWORD (unsigned), so it's always >= 0. This should check for > 0 to detect EOF.
Missing Error Handling---
Several places lack proper error checking and cleanup on failure paths.

I have made the following changes to errors and issues ,
--Added thread creation error checking - Verifies _beginthread() didn't return -1
---Fixed process handle cleanup - Only closes handles if CreateProcess succeeded
--Improved thread synchronization - Closes output pipe to signal EOF, then waits for listener thread to complete
--Fixed loop condition - Changed dwBytesRead >= 0 to dwBytesRead > 0 to properly detect EOF
--Added null pointer check - Verifies lpAttributeList before cleanup

**Original Pull Request:** https://github.com/microsoft/terminal/pull/19673 **State:** closed **Merged:** No --- Thread Handle Check--- cppHANDLE hPipeListenerThread{ reinterpret_cast<HANDLE>(_beginthread(PipeListener, 0, hPipeIn)) }; _beginthread() returns (uintptr_t)-1 on error, but there's no error checking before proceeding. Process Handle Cleanup--- The code closes piClient.hThread and piClient.hProcess even if CreateProcess failed, which could close invalid handles. Thread Synchronization--- Using Sleep(500) is unreliable for thread synchronization. The listener thread may still be running when cleanup begins. PipeListener Loop Condition--- dwBytesRead is DWORD (unsigned), so it's always >= 0. This should check for > 0 to detect EOF. Missing Error Handling--- Several places lack proper error checking and cleanup on failure paths. I have made the following changes to errors and issues , --Added thread creation error checking - Verifies _beginthread() didn't return -1 ---Fixed process handle cleanup - Only closes handles if CreateProcess succeeded --Improved thread synchronization - Closes output pipe to signal EOF, then waits for listener thread to complete --Fixed loop condition - Changed dwBytesRead >= 0 to dwBytesRead > 0 to properly detect EOF --Added null pointer check - Verifies lpAttributeList before cleanup
claunia added the pull-request label 2026-01-31 09:50:23 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#31918