Title: Stop importing `threading` at interpreter shutdown by jseop-lim · Pull Request #9925 · RustPython/RustPython · GitHub
Open Graph Title: Stop importing `threading` at interpreter shutdown by jseop-lim · Pull Request #9925 · RustPython/RustPython
X Title: Stop importing `threading` at interpreter shutdown by jseop-lim · Pull Request #9925 · RustPython/RustPython
Description: Part of #9913 (item 17) This PR follows our AI policy Summary finalize_ex imports threading to call _shutdown(), so a program that never imported it runs the import machinery and threading._shutdown() on exit. A trace function still installed sees those frames, and sys.modules gains threading before the atexit callbacks run. CPython's wait_for_thread_shutdown looks the module up with PyImport_GetModule and returns when it was never imported. AS-IS $ rustpython -c "import atexit, sys; atexit.register(lambda: print('threading' in sys.modules))" True $ rustpython -c "import sys; sys.settrace(print)" ', line 1302, code _find_and_load> call None ', line 1283, code _resolve_parents> call None ... TO-BE $ rustpython -c "import atexit, sys; atexit.register(lambda: print('threading' in sys.modules))" False $ rustpython -c "import sys; sys.settrace(print)" (matching CPython 3.15.2) Changes In crates/vm/src/vm/interpreter.rs, finalize_ex takes threading from sys.modules and skips _shutdown() when it is absent. Extend extra_tests/snippets/stdlib_sys.py with a subprocess whose atexit callback prints whether threading is in sys.modules. Test plan Built and run on macOS aarch64 against upstream/main 7abccf0, using the CI feature set --no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plus threading, compared with CPython 3.15.2. The extended extra_tests/snippets/stdlib_sys.py fails on an unpatched build of that commit (AssertionError: True) and passes with the patch. CPython 3.15.2 prints False for the added subprocess; the snippet as a whole stops there earlier, at its sys.version_info[:3] == (3, 15, 0) assertion. A script that only calls sys.settrace(print) prints 408 traced frames at exit on the unpatched build and none with the patch. A non-daemon threading.Thread that outlives the main module is still joined at exit, and a thread started with _thread.start_new_thread is still not, as on the unpatched build and CPython. rustpython -m test test_threading test_atexit test_sys test_sys_settrace test_thread test_sys_setprofile: 874 run, 153 skipped, 1 failure, the same on the unpatched build. The failure is test_sys.test_stdlib_dir, which compares sys._stdlib_dir with the Lib directory I pointed RUSTPYTHONPATH at. cargo fmt --check clean, cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targets adds no warning, and the pre-commit hooks other than cspell and prettier pass on both changed files. I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this description; Claude Code ran the commands above; I reviewed the diff and their output. Summary by CodeRabbit Bug Fixes Interpreter shutdown no longer loads the threading module if it was not already loaded. When available, its shutdown hook still runs, and any errors continue to be reported as unraisable.
Open Graph Description: Part of #9913 (item 17) This PR follows our AI policy Summary finalize_ex imports threading to call _shutdown(), so a program that never imported it runs the import machinery and threading._shutd...
X Description: Part of #9913 (item 17) This PR follows our AI policy Summary finalize_ex imports threading to call _shutdown(), so a program that never imported it runs the import machinery and threading._shutd...
Opengraph URL: https://github.com/RustPython/RustPython/pull/9925
X: @github
Domain: github.com
| 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/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:0d8be625-2f43-7f09-184c-2729c3cbaca5 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9C3C:842EC:2D855A2:3CBBEE0:6B01D7FC |
| html-safe-nonce | b30b11ab39451fb76d8c5f233007b3ad3db259ce3457805a292036f8b4356e58 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzNDOjg0MkVDOjJEODU1QTI6M0NCQkVFMDo2QjAxRDdGQyIsInZpc2l0b3JfaWQiOiIzMjQyNzM3NjAzNDc2MzE0MTA4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5fce37795cd4b25a72935f1a5fa2942eaebaabaa9005d1d039aa1d507f1e6679 |
| hovercard-subject-tag | pull_request:5361510935 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/pull/9925/files |
| twitter:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| og:image:alt | Part of #9913 (item 17) This PR follows our AI policy Summary finalize_ex imports threading to call _shutdown(), so a program that never imported it runs the import machinery and threading._shutd... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
Viewport: width=device-width