python / python/cpython

Segfault at exit when calling `sys.activate_stack_trampoline("perf")` after loading a native library

Open
#146,522 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-crash
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Crash report

What happened?
import sys, ctypes
ctypes.CDLL(None)
sys.activate_stack_trampoline("perf")
sys.activate_stack_trampoline("perf")
sys.activate_stack_trampoline("perf")

100% reproducible on 3.13.12, exits cleanly on 3.13.11.
Same between 3.14.3 and 3.14.2

Things of note:

  • The crash occurs during interpreter finalization, not at the point of the call
  • No exception is raised; is_stack_trampoline_active() returns True after each call
  • Without ctypes.CDLL, 3 calls don't crash (but 10 calls do crash with "no more code watcher IDs available" + segfault)
  • 2 calls + ctypes.CDLL doesn't crash; 3 calls is the threshold

Repeated calls to activate_stack_trampoline("perf") when the trampoline is already active should either be a no-op or raise an error.

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.13.12 (main, Mar 10 2026, 18:17:38) [Clang 21.1.4 ]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproducer on CPython 3.13.12 and 3.14.3, then trace the implementation of sys.activate_stack_trampoline("perf") through interpreter finalization. Check how repeated activation interacts with ctypes.CDLL(None), and verify that repeated calls either become no-ops or raise an error without a shutdown segfault.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.