Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32: use _endthreadex to terminate threads, not ExitThread #1414

Closed
wants to merge 1 commit into from

Conversation

AtariDreams
Copy link

@AtariDreams AtariDreams commented Dec 22, 2022

Because we use the C runtime and
use _beginthread to create pthreads,
pthread_exit MUST use _endthread.

Otherwise, according to Microsoft:
"Failure to do so results in small
memory leaks when the thread
calls ExitThread."

Simply put, this is not the same as ExitThread.

Signed-off-by: Seija Kijin doremylover123@gmail.com
cc: Johannes Sixt j6t@kdbg.org

@AtariDreams AtariDreams force-pushed the severe-bug branch 2 times, most recently from 7153eb8 to d65a60f Compare December 22, 2022 20:49
@AtariDreams AtariDreams changed the title win32: use _endthread to terminate threads, not ExitThread win32: use _endthreadex to terminate threads, not ExitThread Dec 22, 2022
@AtariDreams AtariDreams force-pushed the severe-bug branch 2 times, most recently from d1a1c1b to 78f9d54 Compare December 22, 2022 20:55
@AtariDreams
Copy link
Author

/submit

@gitgitgadget-git
Copy link

Submitted as pull.1414.git.git.1671742750504.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1414/AtariDreams/severe-bug-v1

To fetch this version to local tag pr-git-1414/AtariDreams/severe-bug-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1414/AtariDreams/severe-bug-v1

@gitgitgadget-git
Copy link

On the Git mailing list, Johannes Sixt wrote (reply to this):

Am 22.12.22 um 21:59 schrieb Rose via GitGitGadget:
> From: Seija Kijin <doremylover123@gmail.com>
> 
> This is a pretty serious bug actually:
> Because we use the C runtime and
> use _beginthreadex to create pthreads,
> pthread_exit MUST use _endthreadex.
> 
> Otherwise, according to Microsoft:
> "Failure to do so results in small
> memory leaks when the thread
> calls ExitThread."
> 
> Simply put, this is not the same as ExitThread.
> 
> Signed-off-by: Seija Kijin <doremylover123@gmail.com>
> ---

>  compat/win32/pthread.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h
> index 737983d00ba..cc3221cb2c8 100644
> --- a/compat/win32/pthread.h
> +++ b/compat/win32/pthread.h
> @@ -66,7 +66,7 @@ pthread_t pthread_self(void);
>  
>  static inline void NORETURN pthread_exit(void *ret)
>  {
> -	ExitThread((DWORD)(intptr_t)ret);
> +	_endthreadex((unsigned)(uintptr_t)ret);
>  }
>  
>  typedef DWORD pthread_key_t;

Nice find! FWIW, this passes the test suite on Windows.

The patch text is:

Acked-by: Johannes Sixt <j6t@kdbg.org>

The commit message is highly exaggerated, though. The bug is by no means
serious. After all, we've been living with it for a decade. Notice that
pthread_exit() is only called when a thread runs into die(). Even though
we have a small memory leak, it does not occur in a loop because, after
one thread dies, we do not tend to keep starting many, many more that
all die.

-- Hannes

@gitgitgadget-git
Copy link

User Johannes Sixt <j6t@kdbg.org> has been added to the cc: list.

Because we use the C runtime and
use _beginthreadex to create pthreads,
pthread_exit MUST use _endthreadex.

Otherwise, according to Microsoft:
"Failure to do so results in small
memory leaks when the thread
calls ExitThread."

Simply put, this is not the same as ExitThread.

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
@AtariDreams
Copy link
Author

/submit

@gitgitgadget-git
Copy link

Submitted as pull.1414.v2.git.git.1671932510529.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1414/AtariDreams/severe-bug-v2

To fetch this version to local tag pr-git-1414/AtariDreams/severe-bug-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1414/AtariDreams/severe-bug-v2

@gitgitgadget-git
Copy link

On the Git mailing list, Johannes Sixt wrote (reply to this):

Am 25.12.22 um 02:41 schrieb Rose via GitGitGadget:
> From: Seija Kijin <doremylover123@gmail.com>
> 
> Because we use the C runtime and
> use _beginthreadex to create pthreads,
> pthread_exit MUST use _endthreadex.
> 
> Otherwise, according to Microsoft:
> "Failure to do so results in small
> memory leaks when the thread
> calls ExitThread."
> 
> Simply put, this is not the same as ExitThread.
> 
> Signed-off-by: Seija Kijin <doremylover123@gmail.com>
> ---
>     win32: use _endthreadex to terminate threads, not ExitThread
>     
>     Because we use the C runtime and use _beginthread to create pthreads,
>     pthread_exit MUST use _endthread.
>     
>     Otherwise, according to Microsoft: "Failure to do so results in small
>     memory leaks when the thread calls ExitThread."
>     
>     Simply put, this is not the same as ExitThread.
>     
>     Signed-off-by: Seija Kijin doremylover123@gmail.com
> 
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1414%2FAtariDreams%2Fsevere-bug-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1414/AtariDreams/severe-bug-v2
> Pull-Request: https://github.com/git/git/pull/1414
> 
> Range-diff vs v1:
> 
>  1:  78f9d54c304 ! 1:  3e8212fb9a7 win32: use _endthreadex to terminate threads, not ExitThread
>      @@ Metadata
>        ## Commit message ##
>           win32: use _endthreadex to terminate threads, not ExitThread
>       
>      -    This is a pretty serious bug actually:
>           Because we use the C runtime and
>           use _beginthreadex to create pthreads,
>           pthread_exit MUST use _endthreadex.
> 
> 
>  compat/win32/pthread.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h
> index 737983d00ba..cc3221cb2c8 100644
> --- a/compat/win32/pthread.h
> +++ b/compat/win32/pthread.h
> @@ -66,7 +66,7 @@ pthread_t pthread_self(void);
>  
>  static inline void NORETURN pthread_exit(void *ret)
>  {
> -	ExitThread((DWORD)(intptr_t)ret);
> +	_endthreadex((unsigned)(uintptr_t)ret);
>  }
>  
>  typedef DWORD pthread_key_t;
> 
> base-commit: 7c2ef319c52c4997256f5807564523dfd4acdfc7

Thank you! This patch is now

Acked-by: Johannes Sixt <j6t@kdbg.org>

-- Hannes

@gitgitgadget-git
Copy link

On the Git mailing list, Junio C Hamano wrote (reply to this):

Johannes Sixt <j6t@kdbg.org> writes:

> Am 25.12.22 um 02:41 schrieb Rose via GitGitGadget:
>> From: Seija Kijin <doremylover123@gmail.com>
>>  ...
> Thank you! This patch is now
>
> Acked-by: Johannes Sixt <j6t@kdbg.org>

Thanks.  Will queue.

@gitgitgadget-git
Copy link

This patch series was integrated into seen via 7ea35b7.

@gitgitgadget-git
Copy link

This branch is now known as sk/win32-pthread-exit-fix.

@gitgitgadget-git
Copy link

This patch series was integrated into seen via b16751d.

@gitgitgadget-git
Copy link

This patch series was integrated into next via ebcb1fe.

@gitgitgadget-git
Copy link

There was a status update in the "New Topics" section about the branch sk/win32-pthread-exit-fix on the Git mailing list:

An API emulation fix.

Will merge to 'master'.
source: <pull.1414.v2.git.git.1671932510529.gitgitgadget@gmail.com>

@gitgitgadget-git
Copy link

This patch series was integrated into seen via 7d5e440.

@gitgitgadget-git
Copy link

This patch series was integrated into seen via 039e5a0.

@gitgitgadget-git
Copy link

This patch series was integrated into master via 039e5a0.

@gitgitgadget-git
Copy link

This patch series was integrated into next via 039e5a0.

@gitgitgadget-git
Copy link

Closed via 039e5a0.

@AtariDreams AtariDreams deleted the severe-bug branch January 24, 2023 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants