X Tutup
Skip to content

thread_inherit_context, upgrade threading & contextvar #6727

Merged
youknowone merged 3 commits intoRustPython:mainfrom
youknowone:thread_inherit_context
Jan 17, 2026
Merged

thread_inherit_context, upgrade threading & contextvar #6727
youknowone merged 3 commits intoRustPython:mainfrom
youknowone:thread_inherit_context

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Jan 14, 2026

TODO: update tests

Summary by CodeRabbit

  • New Features
    • Added a new configuration flag to control whether new threads inherit parent context. Defaults to disabled.
    • Exposed the option via command-line X-option and an environment variable, with validation and consistent error messaging.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 14, 2026

📝 Walkthrough

Walkthrough

Adds a new boolean setting thread_inherit_context to runtime configuration, propagates it from Settings into FlagsData, and wires parsing/validation for the X-option and environment variable (PYTHON_THREAD_INHERIT_CONTEXT) to control the flag.

Changes

Cohort / File(s) Summary
Settings structure expansion
crates/vm/src/vm/setting.rs
Added public boolean field thread_inherit_context with doc comment and set default to false in Default implementation.
FlagsData structure expansion
crates/vm/src/stdlib/sys.rs
Added boolean field thread_inherit_context to FlagsData and wired from_settings to initialize it from settings.thread_inherit_context.
X-option / env parsing
src/settings.rs
Added parsing and validation for thread_inherit_context in X-options and PYTHON_THREAD_INHERIT_CONTEXT env var; accepts 1/0, errors on invalid values and sets settings.thread_inherit_context accordingly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A tiny flag hops into place,
From env and X it finds its space,
Parent context may or may not be shared,
Two structs now carry what was declared,
A rabbit nods—configuration prepared. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'thread_inherit_context, upgrade threading & contextvar' directly reflects the main changes: it adds thread_inherit_context configuration and upgrades threading and contextvar modules.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

@youknowone youknowone changed the title thread_inherit_context, upgrade threading & contextvar (3.14) thread_inherit_context, upgrade threading & contextvar Jan 17, 2026
@youknowone youknowone force-pushed the thread_inherit_context branch from 63912db to b85ba35 Compare January 17, 2026 14:28
@youknowone youknowone marked this pull request as ready for review January 17, 2026 14:28
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@crates/vm/src/vm/setting.rs`:
- Around line 91-92: The CLI parser never wires the
Settings.thread_inherit_context flag into the -X option match, so -X
thread_inherit_context has no effect; update the match in the -X parsing logic
(the code handling dev, faulthandler, warn_default_encoding, etc.) to handle the
"thread_inherit_context" token and set sys.flags.thread_inherit_context (or
Settings.thread_inherit_context) to true when seen (and handle disabling if
parser supports "no_thread_inherit_context" or "thread_inherit_context=0");
ensure you reference the same flag name (thread_inherit_context) and the
Settings/sys.flags field so the value flows from CLI into Settings.
🧹 Nitpick comments (1)
crates/vm/src/stdlib/sys.rs (1)

1329-1329: Consider adding sys.flags coverage for the new flag.

Given the PR note about pending tests, please add/extend tests to assert sys.flags.thread_inherit_context for both default and enabled configurations.

If you want, I can draft the test updates.

@youknowone youknowone force-pushed the thread_inherit_context branch from b85ba35 to a674563 Compare January 17, 2026 15:40
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/settings.rs`:
- Around line 314-327: The PYTHON_THREAD_INHERIT_CONTEXT env handling currently
runs after the -X thread_inherit_context option and thus overrides the X-option;
move the shown get_env("PYTHON_THREAD_INHERIT_CONTEXT") block so it executes
before the code that parses/handles the -X thread_inherit_context option (the
block that sets settings.thread_inherit_context from the -X flag), ensuring the
command-line -X thread_inherit_context takes precedence (consistent with
int_max_str_digits behavior).

Comment on lines +314 to +327
if let Some(val) = get_env("PYTHON_THREAD_INHERIT_CONTEXT") {
settings.thread_inherit_context = match val.to_str() {
Some("1") => true,
Some("0") => false,
_ => {
error!(
"Fatal Python error: config_init_thread_inherit_context: \
PYTHON_THREAD_INHERIT_CONTEXT=N: N is missing or invalid\n\
Python runtime state: preinitialized"
);
std::process::exit(1);
}
};
}
Copy link
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

🏁 Script executed:

#!/bin/bash
# Check PYTHONINTMAXSTRDIGITS and int_max_str_digits handling to compare precedence

# Look for PYTHONINTMAXSTRDIGITS handling
rg -n -B5 -A10 "PYTHONINTMAXSTRDIGITS" src/settings.rs

# Check int_max_str_digits processing order
echo "---"
rg -n "int_max_str_digits" src/settings.rs

Repository: RustPython/RustPython

Length of output: 1647


🏁 Script executed:

#!/bin/bash
# Get detailed view of lines 288-327 to see the exact order of processing

sed -n '280,330p' src/settings.rs | cat -n

Repository: RustPython/RustPython

Length of output: 2419


Fix precedence: X-option should override environment variable, not vice versa.

The environment variable PYTHON_THREAD_INHERIT_CONTEXT (lines 315-328) is processed after the -X thread_inherit_context option (lines 289-301), causing it to override the X-option. This is inconsistent with the handling of int_max_str_digits, where the X-option is processed last and takes precedence. Reorder to process the environment variable before the X-option block to ensure command-line options take precedence, or document this as intentional behavior if it differs from Python's design.

🤖 Prompt for AI Agents
In `@src/settings.rs` around lines 314 - 327, The PYTHON_THREAD_INHERIT_CONTEXT
env handling currently runs after the -X thread_inherit_context option and thus
overrides the X-option; move the shown get_env("PYTHON_THREAD_INHERIT_CONTEXT")
block so it executes before the code that parses/handles the -X
thread_inherit_context option (the block that sets
settings.thread_inherit_context from the -X flag), ensuring the command-line -X
thread_inherit_context takes precedence (consistent with int_max_str_digits
behavior).

@youknowone youknowone merged commit ca76cb7 into RustPython:main Jan 17, 2026
23 of 24 checks passed
@youknowone youknowone deleted the thread_inherit_context branch January 17, 2026 23:53
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.

1 participant

X Tutup