Message 395939 - Python tracker

Message395939

Author vstinner
Recipients eryksun, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Date 2021-06-16.15:43:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623858203.56.0.613282344713.issue44436@roundup.psfhosted.org>
In-reply-to
Content
_thread.start_new_thread() is implemented by calling _beginthreadex(). 

Currently, when the thread completes: PyThread_exit_thread() is called which calls "_endthreadex(0)" on Windows. I proposed to no longer call it explicitly in bpo-44434.

_endthreadex() documentation says that the thread handle must be closed explicitly (with CloseHandle()), same applies for ExitThread().

"Unlike _endthread, the _endthreadex function does not close the thread handle, thereby allowing other threads to block on this one without fear that the handle will be freed out from under the system."

_endthread() closes the thread handle, but Python uses _endthreadex().

Should Python be modified to close explicitly the thread handle once the thread terminated?

_endthread() and _endthreadex() documentation:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/endthread-endthreadex?view=msvc-160

Example closing the thread handle in the thread which created the thread:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex?view=msvc-160

Simplified code:
---
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
    WaitForSingleObject( hThread, INFINITE );
    CloseHandle( hThread );
---


Would it be safe to close the handle just after PyThread_start_new_thread() success?

Or should it be done in the C function thread_run(), just before existing, CloseHandle(GetCurrentThread())?


To debug this issue, GetHandleInformation() can be used to check if the handle is still open or not. For example, call os.get_handle_inheritable(handle) in Python.
History
Date User Action Args
2021-06-16 15:43:23vstinnersetrecipients: + vstinner, paul.moore, tim.golden, zach.ware, eryksun, steve.dower
2021-06-16 15:43:23vstinnersetmessageid: <1623858203.56.0.613282344713.issue44436@roundup.psfhosted.org>
2021-06-16 15:43:23vstinnerlinkissue44436 messages
2021-06-16 15:43:23vstinnercreate