Skip to content

Add subinterpreter support#279

Open
scopreon wants to merge 1 commit intobloomberg:mainfrom
scopreon:add_subinterpreter_support
Open

Add subinterpreter support#279
scopreon wants to merge 1 commit intobloomberg:mainfrom
scopreon:add_subinterpreter_support

Conversation

@scopreon
Copy link
Contributor

@scopreon scopreon commented Feb 22, 2026

Issue number of the reported bug or feature request: #59,#280

Describe your changes
This PR adds simple subinterpreter support to pystack. It preserves all original functionality but does it a few more times by traversing the interpreter linked list. It only supports pure python stacks currently.

Added a new component TracebackPrinter which should help with printing in future. Additional configuration for the printer can be added in future.

Few changes in version.h/cpp.

First contribution so maybe missed something.

Testing performed
Added tests for TracebackPrinter. Added additional end-to-end test for subinterpreters support using the new concurrent 3.14 functionality.

Additional context
Example output

root@875d1afe36dd:/workspaces/pystack# pystack remote 86996
Interpreter-3
  Traceback for thread 87005 (Thread-3 (exec)) [] (most recent call last):
      (Python) File "<script>", line 5, in <module>
  
Interpreter-2
  Traceback for thread 87004 (Thread-2 (exec)) [] (most recent call last):
      (Python) File "<script>", line 5, in <module>
  
Interpreter-1
  Traceback for thread 87003 (Thread-1 (exec)) [] (most recent call last):
      (Python) File "<script>", line 5, in <module>
  
Interpreter-0 (main)
  Traceback for thread 87005 (Thread-3 (exec)) [] (most recent call last):
      (Python) File "/usr/lib/python3.14/threading.py", line 1044, in _bootstrap
          self._bootstrap_inner()
      (Python) File "/usr/lib/python3.14/threading.py", line 1082, in _bootstrap_inner
          self._context.run(self.run)
      (Python) File "/usr/lib/python3.14/threading.py", line 1024, in run
          self._target(*self._args, **self._kwargs)
      (Python) File "/usr/lib/python3.14/concurrent/interpreters/__init__.py", line 215, in exec
          excinfo = _interpreters.exec(self._id, code, restrict=True)
  
  Traceback for thread 87004 (Thread-2 (exec)) [] (most recent call last):
      (Python) File "/usr/lib/python3.14/threading.py", line 1044, in _bootstrap
          self._bootstrap_inner()
      (Python) File "/usr/lib/python3.14/threading.py", line 1082, in _bootstrap_inner
          self._context.run(self.run)
      (Python) File "/usr/lib/python3.14/threading.py", line 1024, in run
          self._target(*self._args, **self._kwargs)
      (Python) File "/usr/lib/python3.14/concurrent/interpreters/__init__.py", line 215, in exec
          excinfo = _interpreters.exec(self._id, code, restrict=True)
  
  Traceback for thread 87003 (Thread-1 (exec)) [] (most recent call last):
      (Python) File "/usr/lib/python3.14/threading.py", line 1044, in _bootstrap
          self._bootstrap_inner()
      (Python) File "/usr/lib/python3.14/threading.py", line 1082, in _bootstrap_inner
          self._context.run(self.run)
      (Python) File "/usr/lib/python3.14/threading.py", line 1024, in run
          self._target(*self._args, **self._kwargs)
      (Python) File "/usr/lib/python3.14/concurrent/interpreters/__init__.py", line 215, in exec
          excinfo = _interpreters.exec(self._id, code, restrict=True)
  
  Traceback for thread 86996 (python3.14) [] (most recent call last):
      (Python) File "/workspaces/pystack/test_file_multiple_interps.py", line 28, in <module>
          print("Main interpreter sleeping forever...")

@codecov-commenter
Copy link

codecov-commenter commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 69.32907% with 96 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.60%. Comparing base (dd69426) to head (c46223d).

Files with missing lines Patch % Lines
tests/integration/test_subinterpreters.py 39.25% 65 Missing ⚠️
src/pystack/_pystack/pythread.cpp 32.55% 29 Missing ⚠️
src/pystack/_pystack/interpreter.cpp 88.88% 1 Missing ⚠️
src/pystack/_pystack/process.cpp 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #279      +/-   ##
==========================================
- Coverage   83.03%   82.60%   -0.44%     
==========================================
  Files          48       50       +2     
  Lines        6242     6484     +242     
  Branches      480      489       +9     
==========================================
+ Hits         5183     5356     +173     
- Misses       1059     1128      +69     
Flag Coverage Δ
cpp 82.60% <69.32%> (-0.44%) ⬇️
python_and_cython 82.60% <69.32%> (-0.44%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Iterate over all interpreters in the linked list when determining the PID
offset, fixing an issue where pystack only inspected the first interpreter,
which is not guaranteed to be the main interpreter. This ensures the correct TID
is located even when multiple subinterpreters are present.

Add support for subinterpreters in pure Python stack reporting. Threads running
in subinterpreters are now detected and grouped by interpreter ID. Native stack
reporting for subinterpreters is not yet supported.

Signed-off-by: Saul Cooperman <saulcoops@gmail.com>
@scopreon scopreon force-pushed the add_subinterpreter_support branch from 6e47658 to c46223d Compare March 8, 2026 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants