Skip to content

Commit

Permalink
Workaround potential CPython bug.
Browse files Browse the repository at this point in the history
This works around a potential bug in CPython where the state
associated with a monitoring tool can "leak" in an unexpected
manner.

This patch also removes the use of the functools cache on the
generator for test functions, this to eliminate any potential
state being stored on the code objects associated with the
test functions.
  • Loading branch information
stuartarchibald committed Apr 26, 2024
1 parent 3a17c60 commit 5d652e7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions numba/tests/test_sys_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import dis
import functools
import queue
import sys
import threading
Expand All @@ -11,7 +10,6 @@
from numba.core.serialize import _numba_unpickle


@functools.cache
def generate_usecase():
@jit('int64(int64)',)
def foo(x):
Expand Down Expand Up @@ -285,6 +283,14 @@ def test_disable_from_callback(self):
self.assertEqual(result, self.call_foo_result)
callback.assert_called()
finally:
# It is necessary to restart events that have been disabled. The
# "disabled" state of the `PY_START` event for the tool
# `self.tool_id` "leaks" into subsequent tests. These tests then end
# up failing as events that should trigger do not! It's not really
# clear why this happens, if it is part of the design or a side
# effect of the design, or if this behaviour is simply a bug in
# CPython itself.
sys.monitoring.restart_events()
sys.monitoring.register_callback(tool_id, event, None)
sys.monitoring.set_events(tool_id, NO_EVENTS)
sys.monitoring.free_tool_id(tool_id)
Expand Down

0 comments on commit 5d652e7

Please sign in to comment.