Title: Single-threaded re-entrancy via a recursive generator causes an access violation (segfault) in `pairwise_next` · Issue #149557 · python/cpython · GitHub
Open Graph Title: Single-threaded re-entrancy via a recursive generator causes an access violation (segfault) in `pairwise_next` · Issue #149557 · python/cpython
X Title: Single-threaded re-entrancy via a recursive generator causes an access violation (segfault) in `pairwise_next` · Issue #149557 · python/cpython
Description: Crash report What happened? The existing partial fix in main (the it = po->it; if (it == NULL) re-read guard after the first tp_iternext call) does not cover the second tp_iternext call, where it is still a borrowed reference. In the sin...
Open Graph Description: Crash report What happened? The existing partial fix in main (the it = po->it; if (it == NULL) re-read guard after the first tp_iternext call) does not cover the second tp_iternext call, where it i...
X Description: Crash report What happened? The existing partial fix in main (the it = po->it; if (it == NULL) re-read guard after the first tp_iternext call) does not cover the second tp_iternext call, where i...
Opengraph URL: https://github.com/python/cpython/issues/149557
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Single-threaded re-entrancy via a recursive generator causes an access violation (segfault) in `pairwise_next`","articleBody":"# Crash report\n\n### What happened?\n\nThe existing partial fix in `main` (the `it = po-\u003eit; if (it == NULL)` re-read guard after the first `tp_iternext` call) does not cover the second `tp_iternext` call, where `it` is still a borrowed reference. In the single-threaded case a re-entrant call can trigger `Py_CLEAR(po-\u003eit)` which drops the generator's refcount to zero and frees it, leaving the outer call's local `it` as a dangling pointer.\n\n**Minimal reproducer**:\n\n```python\nfrom itertools import pairwise\n\ndef g():\n yield next(it)\n\nfor _ in range(10000):\n try:\n it = pairwise(g())\n next(it)\n except ValueError:\n pass\n```\n\n```\nException ignored in: \u003cgenerator object g at 0x...\u003e\nValueError: generator already executing\nWindows fatal exception: access violation\n```\n\nThis is also the root cause of https://github.com/spyder-ide/qtconsole/issues/635.\n\nSome options to address:\n- `Py_INCREF(it)` before the second `tp_iternext(it)` call + `Py_DECREF` after, or\n- a re-entrancy guard flag (like `teedataobject.running`) as suggested by @rhettinger.\n\nNote: `Py_BEGIN_CRITICAL_SECTION` (added in PR #144489) does **not** protect against same-thread re-entrancy.\n\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-150589\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2026-05-08T12:25:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/149557/cpython/issues/149557"}
| 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:8b656177-5d7c-f4f6-a44b-a1e4ffbe4651 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C532:A6212:B95851:109461E:6A588E67 |
| html-safe-nonce | d5acf2d1f123db0551105c9d44e53baac79097367f7c5fdd44ad5c3044b5c607 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTMyOkE2MjEyOkI5NTg1MToxMDk0NjFFOjZBNTg4RTY3IiwidmlzaXRvcl9pZCI6IjQ3ODMxNzc2MjA3Njc3Mzk5MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2c5fd772620003100c0326010abf2147dd0094b69a8ba69edce963ec073ac7f1 |
| hovercard-subject-tag | issue:4406369825 |
| 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/149557/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1321a544d524de6d55164879eb04b0763397ae3abe183a79c774e486132a157b/python/cpython/issues/149557 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1321a544d524de6d55164879eb04b0763397ae3abe183a79c774e486132a157b/python/cpython/issues/149557 |
| og:image:alt | Crash report What happened? The existing partial fix in main (the it = po->it; if (it == NULL) re-read guard after the first tp_iternext call) does not cover the second tp_iternext call, where it i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eendebakpt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| 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 | 8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width