Skip to content
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

_PyEval_SetTrace() fails with RuntimeError: Cannot install a trace function while another trace function is being installed #98257

Closed
vstinner opened this issue Oct 14, 2022 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@vstinner
Copy link
Member

The test_concurrency() of test_import fails randomly on sys.settrace(). Example:

$ ./python -m test test_import -m test_concurrency -v -j30 -F --fail-env-changed 
(...)
0:00:13 load avg: 12.46 [ 97/1] test_import failed (env changed)
test_concurrency (test.test_import.ImportTests) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.487s

OK
Warning -- Uncaught thread exception: RuntimeError
Exception in thread Thread-13 (run):
Traceback (most recent call last):
  File "/home/vstinner/python/3.10/Lib/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/home/vstinner/python/3.10/Lib/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/vstinner/python/3.10/Lib/test/test_import/__init__.py", line 457, in run
    sys.settrace(None)
RuntimeError: Cannot install a trace function while another trace function is being installed
(...)

The problem is that the _PyEval_SetTrace() function fails with RuntimeError("Cannot install a trace function while another trace function is being installed") on Py_XDECREF(traceobj); indirectly calls sys.settrace().

IMO Py_XDECREF(traceobj); should be called after reentrant = 0;, at the end of the function.

@vstinner vstinner added the type-bug An unexpected behavior, bug, or error label Oct 14, 2022
vstinner added a commit that referenced this issue Oct 19, 2022
Make sys.setprofile() and sys.settrace() functions reentrant.  They
can no long fail with: RuntimeError("Cannot install a trace function
while another trace function is being installed").

Make _PyEval_SetTrace() and _PyEval_SetProfile() functions reentrant,
rather than detecting and rejecting reentrant calls. Only delete the
reference to function arguments once the new function is fully set,
when a reentrant call is safe. Call also _PySys_Audit() earlier.
@vstinner
Copy link
Member Author

Fixed by a8fe4bb

carljm added a commit to carljm/cpython that referenced this issue Oct 20, 2022
* main: (40 commits)
  pythongh-98461: Fix source location in comprehensions bytecode (pythonGH-98464)
  pythongh-98421: Clean Up PyObject_Print (pythonGH-98422)
  pythongh-98360: multiprocessing now spawns children on Windows with correct argv[0] in virtual environments (pythonGH-98462)
  CODEOWNERS: Become a typing code owner (python#98480)
  [doc] Improve logging cookbook example. (pythonGH-98481)
  Add more tkinter.Canvas tests (pythonGH-98475)
  pythongh-95023: Added os.setns and os.unshare functions (python#95046)
  pythonGH-98363: Presize the list for batched() (pythonGH-98419)
  pythongh-98374: Suppress ImportError for invalid query for help() command. (pythongh-98450)
  typing tests: `_overload_dummy` raises `NotImplementedError`, not `RuntimeError` (python#98351)
  pythongh-98354: Add unicode check for 'name' attribute in _imp_create_builtin (pythonGH-98412)
  pythongh-98257: Make _PyEval_SetTrace() reentrant (python#98258)
  pythongh-98414: py.exe launcher does not use defaults for -V:company/ option (pythonGH-98460)
  pythongh-98417: Store int_max_str_digits on the Interpreter State (pythonGH-98418)
  Doc: Remove title text from internal links (python#98409)
  [doc] Refresh the venv introduction documentation, and correct the statement about VIRTUAL_ENV (pythonGH-98350)
  Docs: Bump sphinx-lint and fix unbalanced inline literal markup (python#98441)
  pythongh-92886: Replace assertion statements in `handlers.BaseHandler` to support running with optimizations (`-O`) (pythonGH-93231)
  pythongh-92886: Fix tests that fail when running with optimizations (`-O`) in `_test_multiprocessing.py` (pythonGH-93233)
  pythongh-92886: Fix tests that fail when running with optimizations (`-O`) in `test_py_compile.py` (pythonGH-93235)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant