Skip to content

Update thread related tests to 3.14.5#8018

Merged
youknowone merged 5 commits into
RustPython:mainfrom
ShaharNaveh:update-test-threads
Jun 3, 2026
Merged

Update thread related tests to 3.14.5#8018
youknowone merged 5 commits into
RustPython:mainfrom
ShaharNaveh:update-test-threads

Conversation

@ShaharNaveh

@ShaharNaveh ShaharNaveh commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Enhanced thread module stack size validation to enforce minimum size requirements. The stack size function now validates that configured values meet system compatibility thresholds and raises clear errors when they are too small.
    • Improved timeout handling in lock operations with stricter validation and consistent error reporting.

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • Lib/test/test_thread.py is excluded by !Lib/**

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 83fca2a9-dade-4fe1-a7bf-ac701d8a0b22

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The _thread module gains CPython-aligned stack size validation and cleaner timeout error handling. Stack margin constants enable minimum stack size checks, the stack_size function now accepts and validates typed integer arguments, and lock timeout validation uses consistent error construction without string conversions.

Changes

Thread Stack Size and Timeout Improvements

Layer / File(s) Summary
Stack margin constants and typed imports
crates/vm/src/stdlib/_thread.rs
Imports PyIntRef and introduces CPython-derived constants (SYSTEM_PAGE_SIZE, PY_OS_LOG2_STACK_MARGIN, PY_OS_STACK_MARGIN, PY_OS_STACK_MARGIN_BYTES, PY_OS_MIN_STACK_SIZE) for computing minimum stack size bounds.
Timeout validation cleanup in lock macro
crates/vm/src/stdlib/_thread.rs
Refactors error handling in acquire_lock_impl! to use vm.new_value_error(...) and vm.new_overflow_error(...) directly without owned-string conversions, and reorganizes overflow checks to return early.
Stack size function with typed argument and validation
crates/vm/src/stdlib/_thread.rs
Changes stack_size signature from OptionalArg<usize> to OptionalArg<PyIntRef> and return type to PyResult<usize>; validates non-zero sizes against the computed minimum (PY_OS_MIN_STACK_SIZE + SYSTEM_PAGE_SIZE), raising ValueError when too small, before swapping and returning the VM's stacksize.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • youknowone

Poem

🐰 A stack grows tall with careful size,
With Python margins, stack-wise,
Now typed with care, no more surprises,
The threading code revision rises! 🧵✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title claims to update thread tests to 3.14.5, but the actual changes involve refactoring the _thread.stack_size function signature, adding validation logic, and modifying error handling—not updating tests. Revise the title to accurately reflect the main change, such as 'Validate thread stack_size and improve timeout handling' or 'Update _thread.stack_size with validation and CPython-referenced constants'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/threading.py
[x] lib: cpython/Lib/_threading_local.py
[ ] test: cpython/Lib/test/test_threading.py (TODO: 17)
[x] test: cpython/Lib/test/test_threadedtempfile.py
[ ] test: cpython/Lib/test/test_threading_local.py (TODO: 3)

dependencies:

  • threading

dependent tests: (157 tests)

  • threading: test_android test_asyncio test_bytes test_bz2 test_code test_concurrent_futures test_context test_contextlib test_ctypes test_decimal test_docxmlrpc test_email test_enum test_fork1 test_frame test_ftplib test_functools test_gc test_hashlib test_httplib test_httpservers test_imaplib test_importlib test_inspect test_io test_itertools test_largefile test_linecache test_logging test_memoryview test_opcache test_pathlib test_poll test_poplib test_queue test_robotparser test_sched test_signal test_smtplib test_socket test_socketserver test_sqlite3 test_ssl test_subprocess test_super test_sys test_syslog test_termios test_threadedtempfile test_threading test_threading_local test_time test_urllib2_localnet test_weakref test_winreg test_wsgiref test_xmlrpc test_zstd
    • asyncio: test_asyncio test_os test_sys_settrace test_unittest
    • bdb: test_bdb
    • concurrent.futures._base: test_concurrent_futures
    • concurrent.futures.process: test_compileall test_concurrent_futures
    • concurrent.futures.thread: test_genericalias
    • dummy_threading: test_dummy_threading
    • http.cookiejar: test_http_cookiejar test_urllib2
      • urllib.request: test_pathlib test_pydoc test_sax test_site test_urllib test_urllib2net test_urllibnet
    • importlib.util: test_asdl_parser test_ctypes test_doctest test_importlib test_pkgutil test_py_compile test_reprlib test_runpy test_zipfile test_zipimport
      • py_compile: test_argparse test_cmd_line_script test_importlib test_modulefinder test_multiprocessing_main_handling
      • pyclbr: test_pyclbr
      • sysconfig: test_c_locale_coercion test_cmd_line test_dtrace test_embed test_launcher test_osx_env test_peg_generator test_posix test_pyexpat test_regrtest test_support test_sysconfig test_tools test_venv
      • zipfile: test_shutil test_zipapp test_zipfile test_zipfile64 test_zipimport_support
    • logging: test_unittest
      • hashlib: test_hmac test_tarfile test_unicodedata
    • multiprocessing: test_fcntl test_re
    • queue: test_dummy_thread
    • subprocess: test_atexit test_audit test_ctypes test_faulthandler test_file_eintr test_gzip test_json test_msvcrt test_ntpath test_platform test_plistlib test_quopri test_repl test_script_helper test_select test_tempfile test_traceback test_unittest test_utf8_mode test_wait3 test_webbrowser test_xpickle
      • ctypes.util: test_ctypes
      • ensurepip: test_ensurepip
      • platform: test__locale test__osx_support test_baseexception test_builtin test_cmath test_math test_mimetypes test_strptime
    • sysconfig:
      • trace: test_trace
    • zipfile:
      • shutil: test_filecmp test_glob test_string_literals test_unicode_file test_zoneinfo

[x] test: cpython/Lib/test/test_thread.py (TODO: 3)
[x] test: cpython/Lib/test/test_thread_local_bytecode.py
[x] test: cpython/Lib/test/test_threadsignals.py

dependencies:

dependent tests: (13 tests)
- [ ] concurrent.futures: test_asyncio test_compileall test_concurrent_futures test_context test_genericalias test_inspect test_struct test_wmi
- [x] asyncio: test_asyncio test_logging test_os test_sys_settrace test_unittest

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/vm/src/stdlib/_thread.rs`:
- Around line 56-61: The constant PY_OS_LOG2_STACK_MARGIN currently uses
debug_assertions to pick 12 vs 11 which mismatches CPython; update the selection
for PY_OS_LOG2_STACK_MARGIN to detect sanitizers instead (ASan/TSan) rather than
debug builds — e.g. change the cfg_select! condition used in the const
PY_OS_LOG2_STACK_MARGIN to check for sanitizer cfgs (Address/Thread sanitizer)
or drive it from a build-time flag so it returns 12 only when a sanitizer is
enabled and 11 otherwise; ensure any related MIN_SIZE/_thread.stack_size tests
rely on that corrected constant or are adjusted to accept the
sanitizer-dependent minimum.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: da590a8a-2db9-4db3-965f-f017e95cb8e3

📥 Commits

Reviewing files that changed from the base of the PR and between 8e35cc9 and 1a6d0f9.

⛔ Files ignored due to path filters (5)
  • Lib/test/test_thread.py is excluded by !Lib/**
  • Lib/test/test_thread_local_bytecode.py is excluded by !Lib/**
  • Lib/test/test_threadedtempfile.py is excluded by !Lib/**
  • Lib/test/test_threading.py is excluded by !Lib/**
  • Lib/test/test_threading_local.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/stdlib/_thread.rs

Comment on lines +56 to +61
// TODO: Check for sanitize once https://github.com/rust-lang/rust/issues/39699 is closed
/// [CPython `_PyOS_LOG2_STACK_MARGIN`](https://github.com/python/cpython/blob/v3.14.5/Include/internal/pycore_pythonrun.h#L41-L47)
const PY_OS_LOG2_STACK_MARGIN: u32 = cfg_select! {
debug_assertions => 12,
_ => 11,
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

CPython 3.14.5 _PyOS_LOG2_STACK_MARGIN definition pycore_pythonrun.h sanitizer

💡 Result:

In CPython 3.14, the macro _PyOS_LOG2_STACK_MARGIN is an internal configuration macro defined in the internal header file pycore_pythonrun.h [1][2]. It is used to define the C stack margin—the amount of stack space reserved to prevent stack overflows [2][3]. As of the current CPython 3.14 codebase, _PyOS_LOG2_STACK_MARGIN is set based on the environment and build configuration [2]: 1. If AddressSanitizer (_Py_ADDRESS_SANITIZER) or ThreadSanitizer (_Py_THREAD_SANITIZER) are enabled, _PyOS_LOG2_STACK_MARGIN is set to 12 (corresponding to 4096 pointers) [2][4]. 2. In other cases, it defaults to 11 (corresponding to 2048 pointers) [2]. This macro is a private, internal-only implementation detail [2][5]. It was moved from public headers to pycore_pythonrun.h to hide it from the public API, as it is intended solely for CPython's internal stack overflow protection mechanisms [2][5]. The value is used to calculate the actual stack margin in bytes (_PyOS_STACK_MARGIN_BYTES), which is used by CPython to check for stack exhaustion, particularly on platforms like Windows or when using specific sanitizers [2][3][4]. It is not intended for use by third-party C extensions [5].

Citations:


Align _PyOS_LOG2_STACK_MARGIN logic with CPython’s sanitizer-based values.

CPython sets _PyOS_LOG2_STACK_MARGIN to 12 only when ASan/TSan is enabled; otherwise it uses 11. Using Rust’s debug_assertions to select 12 makes debug (non-sanitized) runs compute a larger MIN_SIZE than CPython, so any _thread.stack_size boundary tests can diverge between debug and release—either select based on actual sanitizer cfgs or ensure the updated tests don’t assume a single fixed minimum across profiles.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/vm/src/stdlib/_thread.rs` around lines 56 - 61, The constant
PY_OS_LOG2_STACK_MARGIN currently uses debug_assertions to pick 12 vs 11 which
mismatches CPython; update the selection for PY_OS_LOG2_STACK_MARGIN to detect
sanitizers instead (ASan/TSan) rather than debug builds — e.g. change the
cfg_select! condition used in the const PY_OS_LOG2_STACK_MARGIN to check for
sanitizer cfgs (Address/Thread sanitizer) or drive it from a build-time flag so
it returns 12 only when a sanitizer is enabled and 11 otherwise; ensure any
related MIN_SIZE/_thread.stack_size tests rely on that corrected constant or are
adjusted to accept the sanitizer-dependent minimum.

@ShaharNaveh ShaharNaveh force-pushed the update-test-threads branch from cf903e5 to 52acc28 Compare June 3, 2026 08:50
@youknowone youknowone enabled auto-merge (squash) June 3, 2026 09:41
@youknowone youknowone merged commit 376c4f1 into RustPython:main Jun 3, 2026
27 checks passed
JamesClarke7283 added a commit to JamesClarke7283/RustPython that referenced this pull request Jun 3, 2026
Brings the branch up to date with main so CI job names match the
required status checks (the windows snippets/cpython job was renamed
by RustPython#8004 when its matrix skips were cleared). Also picks up RustPython#8018's
_thread.rs stack-margin updates, which are orthogonal to the
apply_thread_stack_size fix in this PR.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants