Skip to content

SystemError using Yappi for Python3.13 Free Threaded #179

@dan-glass

Description

@dan-glass

I am having trouble getting Yappi to work with Python 3.13t Free Threading. Attempting to isolate the issue, the smallest example was using unittest.patch. However I am able to see it with other libraries, notably a c binary accessed via ctypes. The issue occurs whether the GIL is disabled or not.

Code

#!/usr/bin/env python3.13t
import yappi
import unittest
from unittest.mock import patch
import sys
print(sys.version)

class TestReadFile(unittest.TestCase):

    def setUp(self):
        yappi.start()

    @patch('builtins.open')
    def test_read_file(self, mock_file):
        pass


if __name__ == '__main__':
    unittest.main()

Command

./test_yappi.py
PYTHON_GIL=1 ./test_yappi.py

Output

# ./test_yappi.py
3.13.0 experimental free-threading build (main, Oct  8 2024, 08:51:27) [GCC 9.4.0]
E
======================================================================
ERROR: test_read_file (__main__.TestReadFile.test_read_file)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.13/unittest/mock.py", line 686, in __getattr__
    elif self._mock_methods is not None:
         ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/unittest/mock.py", line 685, in __getattr__
    raise AttributeError(name)
AttributeError: _mock_methods

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.13/unittest/mock.py", line 1420, in patched
    with self.decoration_helper(patched,
         ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
                                args,
                                ^^^^^
                                keywargs) as (newargs, newkeywargs):
                                ^^^^^^^^^
  File "/usr/lib/python3.13/contextlib.py", line 141, in __enter__
    return next(self.gen)
  File "/usr/lib/python3.13/unittest/mock.py", line 1402, in decoration_helper
    arg = exit_stack.enter_context(patching)
  File "/usr/lib/python3.13/contextlib.py", line 530, in enter_context
    result = _enter(cm)
  File "/usr/lib/python3.13/unittest/mock.py", line 1549, in __enter__
    new = Klass(**_kwargs)
  File "/usr/lib/python3.13/unittest/mock.py", line 2156, in __init__
    def __init__(self, /, *args, **kw):

SystemError: <sys.legacy_event_handler object at 0x48614a4a950> returned a result with an exception set

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (errors=1)

Dockerfile

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
RUN add-apt-repository -y ppa:deadsnakes/ppa
RUN apt-get install -y \
    	python3.13-nogil \
    	libpython3.13-nogil \
    	libpython3.13-dev

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions