Title: android (bionic): interpreter genesis aborts with "out of TLS keys" — StaticCell thread_local! statics exhaust PTHREAD_KEYS_MAX · Issue #9084 · RustPython/RustPython · GitHub
Open Graph Title: android (bionic): interpreter genesis aborts with "out of TLS keys" — StaticCell thread_local! statics exhaust PTHREAD_KEYS_MAX · Issue #9084 · RustPython/RustPython
X Title: android (bionic): interpreter genesis aborts with "out of TLS keys" — StaticCell thread_local! statics exhaust PTHREAD_KEYS_MAX · Issue #9084 · RustPython/RustPython
Description: A Python Interpreter written in Rust. Contribute to RustPython/RustPython development by creating an account on GitHub.
Open Graph Description: A Python Interpreter written in Rust. Contribute to RustPython/RustPython development by creating an account on GitHub.
X Description: A Python Interpreter written in Rust. Contribute to RustPython/RustPython development by creating an account on GitHub.
Opengraph URL: https://github.com/RustPython/RustPython
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"android (bionic): interpreter genesis aborts with \"out of TLS keys\" — StaticCell thread_local! statics exhaust PTHREAD_KEYS_MAX","articleBody":"---\nname: Report bugs\nabout: Report a bug not related to CPython compatibility\ntitle: 'android (bionic): interpreter genesis aborts with \"out of TLS keys\" — StaticCell thread_local! statics exhaust PTHREAD_KEYS_MAX'\nlabels: C-bug\nassignees: ''\n---\n\n## Summary\n\nEmbedding `rustpython-vm` on **android (aarch64, bionic libc)** without the `threading` feature aborts during interpreter genesis with:\n\n```text\nfatal runtime error: out of TLS keys, aborting\n```\n\nRoot cause: `StaticCell`'s non-threading variant is backed by `thread_local!` statics (`LocalKey\u003cOnceCell\u003c...>>`). On bionic, **each `thread_local!` static with a destructor consumes one process-wide `pthread_key_t`**, and the pool is capped at `PTHREAD_KEYS_MAX = 1024` (shared with every other TLS user in the process, including std and third-party crates).\n\n`rustpython-vm` instantiates a `StaticCell` slot per builtin type / exception slot / codec slot via the `static_cell!` macro — on the order of **hundreds of `thread_local!` statics** across `crates/vm/src` (11 files contain `static_cell!` invocations; `vm/context.rs`, `exception.rs` and the stdlib modules are the big ones). Genesis initializes all of them at once, blowing through the bionic key pool before startup completes. In a real app (game engine embedding), other TLS consumers push the pool close to the limit even before genesis.\n\n## Expected\n\nInterpreter genesis completes on android (no-threading configuration), as it does on desktop platforms.\n\n## Actual\n\nGenesis aborts during builtin-type initialization:\n\n```text\nfatal runtime error: out of TLS keys, aborting\n```\n\n## Environment\n\n- `aarch64-linux-android` (bionic), NDK r27+, `default-features = false`, features: `compiler, wasmbind, gc, stdio, importlib, encodings, freeze-stdlib` (no `threading`, no `host_env`)\n- Verified against current `main`: `crates/common/src/static_cell.rs` non-threading variant is still `LocalKey\u003cOnceCell\u003c...>>`-based (no android-specific branch)\n- Observed on: RustPython main snapshot 2026-09-22 (`0.6.0-dev`), and re-verified the unfixed state on current `main`\n\n## Analysis\n\n- Bionic (unlike glibc/musl desktop libs, and unlike macOS/Windows TLS implementations rust uses elsewhere) allocates a `pthread_key_t` **per `thread_local!` static with a destructor**, from a **process-wide pool of 1024** (`PTHREAD_KEYS_MAX`).\n- Every `static_cell!` slot in `rustpython-vm` is exactly such a static. Genesis (`Context::init_genesis` + stdlib init) touches all of them in one go.\n- A real application adds its own TLS consumers (std, codecs, GL/math crates), so the effective headroom is smaller than 1024.\n\n## Workaround we shipped (for context — not proposed as a direct patch)\n\nWe added an android-cfg variant of `StaticCell` in our vendored copy: a process-global `UnsafeCell\u003cOnceCell\u003cT>>` + `unsafe impl Sync`, selected via\n`#[cfg(all(not(feature = \"threading\"), feature = \"std\", target_os = \"android\"))]`.\n\nThis is safe under our embedding contract (GIL single-thread, one interpreter per process, always driven on the same thread), and genesis + full gameplay now run cleanly on android (aarch64 real device, HarmonyOS NEXT + 卓易通 container).\n\n**Caveat**: the global-cell approach relies on that single-thread contract and is **not thread-compatible** — we don't propose it as a direct upstream patch. Upstream would need a thread-compatible design (e.g., one TLS slot holding a registry of all `StaticCell`s, instead of one slot per cell).\n\n## Suggested directions\n\n1. A key-frugal `StaticCell` for platforms with small pthread key pools (single TLS slot holding a registry/table of cells), selected by cfg on `target_os = \"android\"`;\n2. Or documenting the android TLS constraint for embedders (no-threading builds abort at genesis once the type count is large enough).\n\nHappy to provide more details, test results, or validate a fix on our android test devices.\n\n## Python Documentation\n\nNot applicable — this is a runtime/embedding issue, not a CPython compatibility issue.\n","author":{"url":"https://github.com/GuYeying","@type":"Person","name":"GuYeying"},"datePublished":"2026-10-11T14:27:46Z","url":"https://github.com/RustPython/RustPython/issues/9084"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:e28999e2-8ba4-022e-fa8a-fc7c196cdc4f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D5CA:172B92:2D44156:3C55E99:6B01D413 |
| html-safe-nonce | f55e8f06418ce49bf4b868aea450f72e28cabc3a7bd3eb15d51a8ad7618039db |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENUNBOjE3MkI5MjoyRDQ0MTU2OjNDNTVFOTk6NkIwMUQ0MTMiLCJ2aXNpdG9yX2lkIjoiNjQ4ODUzMzMzNzgzNjIxMzI2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 577920eaa6d02d12f3ab506142630ba8c6fe3794735a3c8fe25540e49525cc42 |
| hovercard-subject-tag | repository:135201145 |
| 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/RustPython/RustPython/issues/9084 |
| twitter:image | https://opengraph.githubassets.com/8b6bc91d3baaf62a969a0721494e51d7c08f420d61d44b393656cebdf755eb84/RustPython/RustPython |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8b6bc91d3baaf62a969a0721494e51d7c08f420d61d44b393656cebdf755eb84/RustPython/RustPython |
| og:image:alt | A Python Interpreter written in Rust. Contribute to RustPython/RustPython development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-cache |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width