twisted / twisted/twisted

ThreadPool.threads leak

Open
#8,114 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug core new priority-normal
Dominant language
Python
Stars
6k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
10

Description

allenap's avatar allenap reported
Trac ID trac#8114
Type defect
Created 2015-11-19 16:01:41Z

Threads that are stopped are not removed from the TheadPool.threads list. This prevents garbage collection of the thread instances themselves and (possibly; not confirmed) thread-local storage. Programs that make frequent use of adjustPoolSize may suffer.

I've observed this in 15.4.0 (on Ubuntu 15.10 Wily) but not in 13.2.0 (on Ubuntu 14.04 Trusty).

The following can be used to reproduce the issue:

from pprint import pprint
from time import sleep

from twisted import __version__
from twisted.python import threadpool


print("Twisted " + __version__)


def count_running_threads(tp):
    return sum(1 for thread in tp.threads if thread.is_alive())


def wait_for_pool_to_adjust(tp, num_running):
    while count_running_threads(tp) != num_running:
        sleep(0.05)  # Allow threads to start or stop.
    assert len(tp.threads) == num_running, (
        "ThreadPool has %d threads, but %d were "
        "expected." % (len(tp.threads), num_running))


tp = threadpool.ThreadPool(name="foobar")
tp.start()
try:
    wait_for_pool_to_adjust(tp, 5)
    tp.adjustPoolsize(minthreads=10)
    wait_for_pool_to_adjust(tp, 10)
    tp.adjustPoolsize(minthreads=5, maxthreads=5)
    wait_for_pool_to_adjust(tp, 5)
except AssertionError:
    pprint(tp.threads)
    raise
finally:
    tp.stop()

Example from 15.4.0:

Twisted 15.4.0
[<Thread(PoolThread-foobar-0, started 140157835495168)>,
 <Thread(PoolThread-foobar-1, started 140157827102464)>,
 <Thread(PoolThread-foobar-2, stopped 140157818709760)>,
 <Thread(PoolThread-foobar-3, stopped 140157600659200)>,
 <Thread(PoolThread-foobar-4, stopped 140157592266496)>,
 <Thread(PoolThread-foobar-5, started 140157583873792)>,
 <Thread(PoolThread-foobar-6, started 140157575481088)>,
 <Thread(PoolThread-foobar-7, stopped 140157567088384)>,
 <Thread(PoolThread-foobar-8, started 140157558695680)>,
 <Thread(PoolThread-foobar-9, stopped 140157550302976)>]
Traceback (most recent call last):
  File "tpleak.py", line 29, in <module>
    wait_for_pool_to_adjust(tp, 5)
  File "tpleak.py", line 19, in wait_for_pool_to_adjust
    "expected." % (len(tp.threads), num_running))
AssertionError: ThreadPool has 10 threads, but 5 were expected.

Note that 5 threads are stopped.

Searchable metadata
trac-id__8114 8114
type__defect defect
reporter__allenap allenap
priority__normal normal
milestone__None None
branch__ 
branch_author__ 
status__new new
resolution__None None
component__core core
keywords__None None
time__1447948901637437 1447948901637437
changetime__1447948901637437 1447948901637437
version__None None
owner__None None

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 in twisted.python.threadpool and run the provided ThreadPool reproduction with adjustPoolsize. Trace how stopped threads are handled; done means the pool's threads list contains only the expected running threads after shrinking, and the reproduction assertion passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.