Commit 6f9e51fe authored by Jason Madden's avatar Jason Madden

Killing the monitoring thread should restore the greenlet trace function.

parent fcfc9304
...@@ -468,7 +468,7 @@ class PeriodicMonitoringThread(object): ...@@ -468,7 +468,7 @@ class PeriodicMonitoringThread(object):
# The trace function that was previously installed, # The trace function that was previously installed,
# if any. # if any.
previous_trace_function = staticmethod(lambda _event, _args: None) previous_trace_function = None
# The absolute minimum we will sleep, regardless of # The absolute minimum we will sleep, regardless of
# what particular monitoring functions want to say. # what particular monitoring functions want to say.
...@@ -485,7 +485,7 @@ class PeriodicMonitoringThread(object): ...@@ -485,7 +485,7 @@ class PeriodicMonitoringThread(object):
# the trace function is threadlocal # the trace function is threadlocal
assert get_thread_ident() == hub.thread_ident assert get_thread_ident() == hub.thread_ident
prev_trace = settrace(self.greenlet_trace) prev_trace = settrace(self.greenlet_trace)
self.previous_trace_function = prev_trace or self.previous_trace_function self.previous_trace_function = prev_trace
# Create the actual monitoring thread. This is effectively a "daemon" # Create the actual monitoring thread. This is effectively a "daemon"
# thread. # thread.
...@@ -500,7 +500,8 @@ class PeriodicMonitoringThread(object): ...@@ -500,7 +500,8 @@ class PeriodicMonitoringThread(object):
self._active_greenlet = args[1] self._active_greenlet = args[1]
else: else:
self._active_greenlet = None self._active_greenlet = None
self.previous_trace_function(event, args) if self.previous_trace_function is not None:
self.previous_trace_function(event, args)
def monitoring_functions(self): def monitoring_functions(self):
# Return a list of tuples: [(function, period)] # Return a list of tuples: [(function, period)]
...@@ -521,6 +522,8 @@ class PeriodicMonitoringThread(object): ...@@ -521,6 +522,8 @@ class PeriodicMonitoringThread(object):
def kill(self): def kill(self):
# Stop this monitoring thread from running. # Stop this monitoring thread from running.
self.should_run = False self.should_run = False
# Uninstall our tracing hook
settrace(self.previous_trace_function)
def __call__(self): def __call__(self):
# The function that runs in the monitoring thread. # The function that runs in the monitoring thread.
...@@ -539,9 +542,13 @@ class PeriodicMonitoringThread(object): ...@@ -539,9 +542,13 @@ class PeriodicMonitoringThread(object):
# Make sure the hub is still around, and still active, # Make sure the hub is still around, and still active,
# and keep it around while we are here. # and keep it around while we are here.
hub = self._hub_wref() hub = self._hub_wref()
if hub and self.should_run: if not hub:
for f, _ in functions: self.kill()
f(hub)
if self.should_run:
for f, period in functions:
if period:
f(hub)
except SystemExit: except SystemExit:
pass pass
except: # pylint:disable=bare-except except: # pylint:disable=bare-except
......
...@@ -176,12 +176,20 @@ class TestPeriodicMonitoringThread(greentest.TestCase): ...@@ -176,12 +176,20 @@ class TestPeriodicMonitoringThread(greentest.TestCase):
before_funs = monitor._additional_monitoring_functions before_funs = monitor._additional_monitoring_functions
monitor.add_monitoring_function( monitor.add_monitoring_function(
monitor_cond, monitor_cond,
0) 0.01)
cond.wait() cond.wait()
cond.release() cond.release()
monitor._additional_monitoring_functions = before_funs monitor._additional_monitoring_functions = before_funs
@greentest.ignores_leakcheck
def test_kill_removes_trace(self):
from greenlet import gettrace
hub = get_hub()
hub.start_periodic_monitoring_thread()
self.assertIsNotNone(gettrace())
hub.periodic_monitoring_thread.kill()
self.assertIsNone(gettrace())
@greentest.ignores_leakcheck @greentest.ignores_leakcheck
def test_blocking_this_thread(self): def test_blocking_this_thread(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment