Skip to content

Intermittent failure in test_sys.SysModuleTest.test_current_exceptions #110796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FFY00 opened this issue Oct 13, 2023 · 1 comment
Closed

Intermittent failure in test_sys.SysModuleTest.test_current_exceptions #110796

FFY00 opened this issue Oct 13, 2023 · 1 comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@FFY00
Copy link
Member

FFY00 commented Oct 13, 2023

Bug report

Bug description:

Example: https://github.com/python/cpython/actions/runs/6502915514/job/17662666558?pr=110794

The code in question:

def test_current_exceptions(self):
import threading
import traceback
# Spawn a thread that blocks at a known place. Then the main
# thread does sys._current_frames(), and verifies that the frames
# returned make sense.
entered_g = threading.Event()
leave_g = threading.Event()
thread_info = [] # the thread's id
def f123():
g456()
def g456():
thread_info.append(threading.get_ident())
entered_g.set()
while True:
try:
raise ValueError("oops")
except ValueError:
if leave_g.wait(timeout=support.LONG_TIMEOUT):
break
t = threading.Thread(target=f123)
t.start()
entered_g.wait()
try:
# At this point, t has finished its entered_g.set(), although it's
# impossible to guess whether it's still on that line or has moved on
# to its leave_g.wait().
self.assertEqual(len(thread_info), 1)
thread_id = thread_info[0]
d = sys._current_exceptions()
for tid in d:
self.assertIsInstance(tid, int)
self.assertGreater(tid, 0)
main_id = threading.get_ident()
self.assertIn(main_id, d)
self.assertIn(thread_id, d)
self.assertEqual(None, d.pop(main_id))
# Verify that the captured thread frame is blocked in g456, called
# from f123. This is a little tricky, since various bits of
# threading.py are also in the thread's call stack.
exc_value = d.pop(thread_id)
stack = traceback.extract_stack(exc_value.__traceback__.tb_frame)
for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
if funcname == "f123":
break
else:
self.fail("didn't find f123() on thread's call stack")
self.assertEqual(sourceline, "g456()")
# And the next record must be for g456().
filename, lineno, funcname, sourceline = stack[i+1]
self.assertEqual(funcname, "g456")
self.assertTrue(sourceline.startswith("if leave_g.wait("))
finally:
# Reap the spawned thread.
leave_g.set()
t.join()

Originally added in 64366fa

I believe the failure is due to sys._current_exceptions being run before the exception is raised.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@FFY00 FFY00 added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir labels Oct 13, 2023
@FFY00 FFY00 changed the title Intermittent failure in ERROR: test_current_exceptions (test.test_sys.SysModuleTest.test_current_exceptions Intermittent failure in test_sys.SysModuleTest.test_current_exceptions Oct 13, 2023
FFY00 added a commit to FFY00/cpython that referenced this issue Oct 13, 2023
@hugovk
Copy link
Member

hugovk commented Nov 9, 2023

Thanks for the fix!

@hugovk hugovk closed this as completed Nov 9, 2023
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants