Skip to content

Commit

Permalink
Try to fix spurious thread test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
snaury committed May 16, 2015
1 parent 641794f commit cbfee0c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tests/test_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import gc

import time
import weakref
import greenlet
import threading
Expand All @@ -28,6 +29,20 @@ def test_kwarg_refs(self):
if greenlet.GREENLET_USE_GC:
# These only work with greenlet gc support

def recycle_threads(self):
# By introducing a thread that does sleep we allow other threads,
# that have triggered their __block condition, but did not have a
# chance to deallocate their thread state yet, to finally do so.
# The way it works is by requring a GIL switch (different thread),
# which does a GIL release (sleep), which might do a GIL switch
# to finished threads and allow them to clean up.
def worker():
time.sleep(0.001)
t = threading.Thread(target=worker)
t.start()
time.sleep(0.001)
t.join()

def test_threaded_leak(self):
gg = []
def worker():
Expand All @@ -38,8 +53,7 @@ def worker():
t.start()
t.join()
del t
greenlet.getcurrent() # update ts_current
gc.collect()
self.recycle_threads()
greenlet.getcurrent() # update ts_current
gc.collect()
greenlet.getcurrent() # update ts_current
Expand All @@ -61,8 +75,7 @@ def additional():
t.start()
t.join()
del t
greenlet.getcurrent() # update ts_current
gc.collect()
self.recycle_threads()
greenlet.getcurrent() # update ts_current
gc.collect()
greenlet.getcurrent() # update ts_current
Expand Down

0 comments on commit cbfee0c

Please sign in to comment.