This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients eryksun, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Date 2021-06-16.16:01:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623859281.05.0.668102747164.issue44436@roundup.psfhosted.org>
In-reply-to
Content
> [Windows] _thread.start_new_thread() should close the thread handle

So far, I'm not convinced that Python must be changed.

I modified my Python locally so thread_run() writes GetCurrentThread() to stderr.

Example:
---
SLEEP = 5

import ctypes
import _thread
import time
import os

HANDLE = ctypes.c_voidp
DWORD = ctypes.c_ulong

ResumeThread = ctypes.windll.kernel32.ResumeThread
ResumeThread.argtypes = (HANDLE,)
ResumeThread.restype = DWORD

SuspendThread = ctypes.windll.kernel32.SuspendThread
SuspendThread.argtypes = (HANDLE,)
SuspendThread.restype = DWORD

def func():
    time.sleep(SLEEP)
    print("--thread exit--")

def check_handle(handle):
    x = SuspendThread(handle)
    print(f"SuspendThread({handle}) -> {x}")
    x = ResumeThread(handle)
    print(f"ResumeThread({handle}) -> {x}")

handle = _thread.start_new_thread(func, ())
print(f"start_new_thread() -> {handle}")
check_handle(handle)
time.sleep(SLEEP+1)
check_handle(handle)
---


Output:
---
start_new_thread() -> 2436
SuspendThread(2436) -> 4294967295
ResumeThread(2436) -> 4294967295
thread_run: GetCurrentThread() -> -2
--thread exit--
SuspendThread(2436) -> 4294967295
ResumeThread(2436) -> 4294967295
---

It seems like the handle returned by _beginthreadex() cannot be used with SuspendThread() or ResumeThread(): both fail. GetHandleInformation() also fails on this handle (I tried os.get_handle_inheritable()).
History
Date User Action Args
2021-06-16 16:01:21vstinnersetrecipients: + vstinner, paul.moore, tim.golden, zach.ware, eryksun, steve.dower
2021-06-16 16:01:21vstinnersetmessageid: <1623859281.05.0.668102747164.issue44436@roundup.psfhosted.org>
2021-06-16 16:01:21vstinnerlinkissue44436 messages
2021-06-16 16:01:20vstinnercreate