IronLanguages / IronLanguages/ironpython3

threads are not GCed on Mono

Open
#1,005 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Mono testing
Dominant language
C#
Stars
2.8k
Forks
316
Avg merge
1d 9h
Merged PRs (30d)
1

Description

Noticed this because some tests (test_hashlib) are running slow and sometimes timing out. Here's a simplified example:

import _thread
import functools
import gc
import threading
import time

def threading_setup():
    threading.current_thread() # ironpython: register the current thread if not running on a known thread
    return _thread._count(), threading._dangling.copy()

def threading_cleanup(*original_values):
    _MAX_COUNT = 100
    for count in range(_MAX_COUNT):
        values = _thread._count(), threading._dangling
        if values == original_values:
            return
        time.sleep(0.01)
        gc.collect()

    print("failed to GC the threads")

def reap_threads(func):
    @functools.wraps(func)
    def decorator(*args):
        key = threading_setup()
        try:
            return func(*args)
        finally:
            threading_cleanup(*key)
    return decorator

@reap_threads
def test_threaded_hashing():
    num_threads = 1

    def hash_in_chunks(event):
        event.set()

    events = []
    for threadnum in range(num_threads):
        event = threading.Event()
        events.append(event)
        threading.Thread(target=hash_in_chunks, args=[event]).start()

    for event in events:
        event.wait()

test_threaded_hashing()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the simplified Python reproduction on Mono and inspect the values returned by _thread._count() and threading._dangling after gc.collect(). Compare the behavior with the same test on another runtime; done means worker threads are reclaimed and the hashing test no longer reports leaked threads or times out.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.