[CDRIVER-3634] Windows thread handles are leaked Created: 25/Apr/20  Updated: 28/Oct/23  Resolved: 07/May/20

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.17.0-beta2, 1.17.0

Type: Bug Priority: Minor - P4
Reporter: Kevin Albertson Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: platform-problems
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File handle-leak.c     PNG File handle-leak.png    

 Description   

The thread functions defined in common-thread-private.h give a platform agnostic thread and mutex API.

On Windows, bson_thread_create wraps CreateThread. This creates a new thread handle. And bson_thread_join wraps WaitForSingleObject to wait on that thread handle, but it does not close the thread handle after waiting.

The documentation for CreateThread states:

> The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.

The leak can be observed this with a simple example program that just creates and destroys a mongoc_client_pool_t repeatedly (which creates one monitoring thread and joins it repeatedly). The example is attached as handle-leak.c. A screenshot of process explorer showing a large number of open handles is also attached.



 Comments   
Comment by Githook User [ 25/May/20 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@10gen.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3635 use _beginthreadex, not CreateThread

Use correct calling convention in thread functions.
CDRIVER-3634 close thread handles after joining.
Branch: r1.17
https://github.com/mongodb/mongo-c-driver/commit/d47399cb8a885c538f5446b71ace9655e215454f

Comment by Githook User [ 07/May/20 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@10gen.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3635 use _beginthreadex, not CreateThread

Use correct calling convention in thread functions.
CDRIVER-3634 close thread handles after joining.
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/bedfc0157f2d0a88c463086915426546b0f633b3

Generated at Wed Feb 07 21:18:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.