Opened 5 years ago

Closed 5 years ago

#5604 closed bug (fixed)

Thread handle leak in runtime

Reported by: yurij Owned by:
Priority: normal Milestone: 7.4.1
Component: Runtime System Version: 7.2.1
Keywords: _endthreadex, shutdownThread Cc:
Operating System: Windows Architecture: x86
Type of failure: Other Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


win32\OSThreads.c shutdownThread uses _endthreadex(0), which does not call CloseHandle for thread handle.

It causes thread handle leak on each runtime initialization/deinitialization

Change History (8)

comment:1 Changed 5 years ago by simonmar

I'm too lazy to look it up. What's the right API to use here?

comment:2 Changed 5 years ago by simonmar

Milestone: 7.4.1

comment:3 Changed 5 years ago by yurij

_endthread calls CloseHandle, so it should be the right API.

In fact, I`m trying to workaround FFI bug with hs_init/hs_exit:

Ive found some global/thread-local variables (myTask, allocs, generations, mblock_allocated) which doesnt properly set to NULL/0 on hs_exit and set it to NULL in appropriate finalization functions but obviously it is not enough to fix segfaults after repeated runtime initialization/deinitialization.

So I try to use LoadLibrary/hs_init/use haskell functions/hs_exit/FreeLibrary call sequence to avoid this problem and notice this handle leak.

comment:4 Changed 5 years ago by simonmar

I'm looking at this. What do you use to see handle leaks?

comment:5 Changed 5 years ago by yurij

Windows Task Manager with switched on column "Handle count" or Sysinternals Process Explorer.

comment:6 Changed 5 years ago by marlowsd@…

commit dad0d225ec5117b715efc0c44c4fa0d606660381

Author: Simon Marlow <>
Date:   Tue Nov 8 16:28:57 2011 +0000

    Close some handle leaks (#5604)
    Also, use the Win32 API (CreateThread) instead of the CRT API
    (_beginthreadex) for thread creation.

 rts/win32/OSThreads.c |   30 +++++++++++++++++++++---------
 1 files changed, 21 insertions(+), 9 deletions(-)

comment:7 Changed 5 years ago by marlowsd@…

commit 86a26670b809b02209640dd136a29705d72a4966

Author: Simon Marlow <>
Date:   Thu Nov 10 16:36:39 2011 +0000

    Close the handle for the ticker thread (#5604)

 rts/win32/Ticker.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

comment:8 Changed 5 years ago by simonmar

Resolution: fixed
Status: newclosed

I think this is now fixed

  • In a test program with no hs_init/hs_exit I see 2 thread handles open at the end of main
  • After calling hs_init/hs_exit, with 7.2.1 I see 5 thread handles
  • with these patches, we're back to 2 handles open after hs_exit. So I think all the leaks are plugged.

I don't know how to make an automatic test case. If you can make a program to test this, please attach it to the ticket and we'll put it in the test suite.

Note: See TracTickets for help on using tickets.