Title: profiling.sampling CPU mode may attribute on-CPU samples to detached tstates when sharing native_thread_id · Issue #159312 · python/cpython · GitHub
Open Graph Title: profiling.sampling CPU mode may attribute on-CPU samples to detached tstates when sharing native_thread_id · Issue #159312 · python/cpython
X Title: profiling.sampling CPU mode may attribute on-CPU samples to detached tstates when sharing native_thread_id · Issue #159312 · python/cpython
Description: Bug report My reproducer uses one worker OS thread. The worker enters funA -> funB using tstate0, detaches tstate0, creates and attaches tstate1 in the same interpreter, then runs funC -> funD. Only funD performs CPU work. Despite that, ...
Open Graph Description: Bug report My reproducer uses one worker OS thread. The worker enters funA -> funB using tstate0, detaches tstate0, creates and attaches tstate1 in the same interpreter, then runs funC -> funD. Onl...
X Description: Bug report My reproducer uses one worker OS thread. The worker enters funA -> funB using tstate0, detaches tstate0, creates and attaches tstate1 in the same interpreter, then runs funC -> fun...
Opengraph URL: https://github.com/python/cpython/issues/159312
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"profiling.sampling CPU mode may attribute on-CPU samples to detached tstates when sharing native_thread_id","articleBody":"### Bug report\n\nMy reproducer uses one worker OS thread. The worker enters funA -> funB using tstate0, detaches tstate0, creates and attaches tstate1 in the same interpreter, then runs funC -> funD. Only funD performs CPU work. Despite that, CPU-mode output attributes about half of the direct samples to the suspended funB/tstate0 stack and half to the executing funD/tstate1 stack.\n\n```python\nimport os\nimport sys\nimport threading\n\nimport _native\n\n\ndef funD():\n print(\n f\"HOT LOOP funD pid={os.getpid()} tid={threading.get_native_id()}\",\n file=sys.stderr,\n flush=True,\n )\n value = 1\n while True:\n value = (value * 1_103_515_245 + 12_345) & 0x7FFF_FFFF\n value ^= value >> 7\n\n\ndef funC():\n _native.print_tstate(\"tstate1 in funC\")\n funD()\n\n\ndef funB():\n _native.switch_tstate_and_call(funC)\n\n\ndef funA():\n funB()\n\n\ndef work0():\n print(\n f\"WORKER tid={threading.get_native_id()}\",\n file=sys.stderr,\n flush=True,\n )\n _native.print_tstate(\"initial tstate0\")\n funA()\n\n\ndef main():\n print(\n f\"MAIN tid={threading.get_native_id()}\",\n file=sys.stderr,\n flush=True,\n )\n worker = threading.Thread(target=work0, name=\"work0\")\n worker.start()\n worker.join()\n\n\nif __name__ == \"__main__\":\n main()\n\n```\n\n```c\n#define _GNU_SOURCE\n#include \u003cPython.h>\n#include \u003cstdio.h>\n#include \u003csys/syscall.h>\n#include \u003cunistd.h>\n\nstatic long\ngettid_linux(void)\n{\n return (long)syscall(SYS_gettid);\n}\n\nstatic void\nprint_tstate_info(const char *where, PyThreadState *tstate)\n{\n PyThreadState *current = PyThreadState_GetUnchecked();\n unsigned long native_tid = tstate ? tstate->native_thread_id : 0;\n long tid = gettid_linux();\n\n fprintf(stderr,\n \"TSTATE %-24s tstate=%p current=%p native_thread_id=%lu \"\n \"gettid=%ld match=%s\\n\",\n where, (void *)tstate, (void *)current, native_tid, tid,\n tstate && native_tid == (unsigned long)tid ? \"yes\" : \"no\");\n fflush(stderr);\n}\n\nstatic PyObject *\nprint_tstate(PyObject *self, PyObject *args)\n{\n const char *where;\n if (!PyArg_ParseTuple(args, \"s\", &where)) {\n return NULL;\n }\n print_tstate_info(where, PyThreadState_Get());\n Py_RETURN_NONE;\n}\n\nstatic PyObject *\nswitch_tstate_and_call(PyObject *self, PyObject *callback)\n{\n if (!PyCallable_Check(callback)) {\n PyErr_SetString(PyExc_TypeError, \"callback must be callable\");\n return NULL;\n }\n\n PyThreadState *tstate0 = PyThreadState_Get();\n PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate0);\n print_tstate_info(\"tstate0 in funB\", tstate0);\n\n PyThreadState *saved0 = PyEval_SaveThread();\n print_tstate_info(\"tstate0 detached\", saved0);\n\n PyThreadState *tstate1 = PyThreadState_New(interp);\n if (tstate1 == NULL) {\n PyEval_RestoreThread(saved0);\n return PyErr_NoMemory();\n }\n\n print_tstate_info(\"tstate1 created\", tstate1);\n PyEval_RestoreThread(tstate1);\n print_tstate_info(\"tstate1 attached\", tstate1);\n\n /* The callback enters funC -> funD, and funD intentionally does not return. */\n PyObject *result = PyObject_CallNoArgs(callback);\n\n /* Cleanup remains correct if funC is changed to return. */\n PyObject *raised = result ? NULL : PyErr_GetRaisedException();\n Py_XDECREF(result);\n PyThreadState *saved1 = PyEval_SaveThread();\n PyEval_RestoreThread(saved0);\n PyThreadState_Clear(saved1);\n PyThreadState_Delete(saved1);\n\n if (raised) {\n PyErr_SetRaisedException(raised);\n return NULL;\n }\n Py_RETURN_NONE;\n}\n\nstatic PyMethodDef methods[] = {\n {\"print_tstate\", print_tstate, METH_VARARGS, NULL},\n {\"switch_tstate_and_call\", switch_tstate_and_call, METH_O, NULL},\n {NULL, NULL, 0, NULL}\n};\n\nstatic PyModuleDef_Slot slots[] = {\n {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},\n {Py_mod_gil, Py_MOD_GIL_NOT_USED},\n {0, NULL}\n};\n\nstatic struct PyModuleDef module = {\n PyModuleDef_HEAD_INIT, \"_native\", NULL, 0, methods, slots\n};\n\nPyMODINIT_FUNC\nPyInit__native(void)\n{\n return PyModuleDef_Init(&module);\n}\n```\nand the sample result is:\n\n\u003cimg width=\"2715\" height=\"165\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/af08416d-c7c5-482e-aea1-d555a4329714\" />\n\n[cpython315-tstate-tachyon-demo.zip](https://github.com/user-attachments/files/33383416/cpython315-tstate-tachyon-demo.zip)\n\n[tachyon-flamegraph.html](https://github.com/user-attachments/files/33383426/tachyon-flamegraph.html)\n\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159328\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/superajun-wsj","@type":"Person","name":"superajun-wsj"},"datePublished":"2026-10-13T11:52:27Z","url":"https://github.com/python/cpython/issues/159312"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bd25ae5d-adb5-7250-9403-d209d996f4e7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B578:AA9CB:42F444:583216:6B023C51 |
| html-safe-nonce | 68fd3e8a59297ab00a5b2bf84b4b70fc42fb767f85d9a4557dc08916c17e0f4b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTc4OkFBOUNCOjQyRjQ0NDo1ODMyMTY6NkIwMjNDNTEiLCJ2aXNpdG9yX2lkIjoiMTA1Mzc0MDA3NzE5ODQ5MDcwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a4fecac8f98cfa99d2fb64f88a6414c0e836c844f2f74bc25ec7cfcfb6e0dfde |
| hovercard-subject-tag | issue:5829796021 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/159312/issue_layout |
| twitter:image | https://opengraph.githubassets.com/982ddc03d75b0e02cc051d390a80fdccdcfcf460841e8dada547d1b8eaa70183/python/cpython/issues/159312 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/982ddc03d75b0e02cc051d390a80fdccdcfcf460841e8dada547d1b8eaa70183/python/cpython/issues/159312 |
| og:image:alt | Bug report My reproducer uses one worker OS thread. The worker enters funA -> funB using tstate0, detaches tstate0, creates and attaches tstate1 in the same interpreter, then runs funC -> funD. Onl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | superajun-wsj |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width