Skip to content

Commit 5954ffa

Browse files
committed
feat(run): support simulation for free-threaded python
Remove PYTHONMALLOC=malloc which is unsupported for free-threaded builds
1 parent c84a798 commit 5954ffa

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod ignored_objects_path;
22
pub mod perf_maps;
3+
pub mod python;
34
pub mod venv_compat;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::process::Command;
2+
3+
/// Checks if the Python interpreter supports free-threaded mode.
4+
/// Returns true if Python is free-threaded (GIL disabled), false otherwise.
5+
pub fn is_free_threaded_python() -> bool {
6+
// Use sysconfig.get_config_var("Py_GIL_DISABLED") as recommended by Python docs at https://docs.python.org/3/howto/free-threading-python.html#identifying-free-threaded-python
7+
let output = Command::new("python")
8+
.args([
9+
"-c",
10+
"import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED') or 0)",
11+
])
12+
.output();
13+
14+
match output {
15+
Ok(output) if output.status.success() => {
16+
let stdout = String::from_utf8_lossy(&output.stdout);
17+
stdout.trim() == "1"
18+
}
19+
_ => false, // If Python is not available or command fails, assume not free-threaded
20+
}
21+
}

src/run/runner/valgrind/measure.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::run::runner::helpers::introspected_golang;
66
use crate::run::runner::helpers::introspected_nodejs;
77
use crate::run::runner::helpers::run_command_with_log_pipe::run_command_with_log_pipe;
88
use crate::run::runner::valgrind::helpers::ignored_objects_path::get_objects_path_to_ignore;
9+
use crate::run::runner::valgrind::helpers::python::is_free_threaded_python;
910
use crate::run::{config::Config, instruments::mongo_tracer::MongoTracer};
1011
use lazy_static::lazy_static;
1112
use std::env;
@@ -87,9 +88,16 @@ pub async fn measure(
8788
cmd.envs(get_base_injected_env(
8889
RunnerMode::Simulation,
8990
profile_folder,
90-
))
91-
.env("PYTHONMALLOC", "malloc")
92-
.env(
91+
));
92+
93+
// Only set PYTHONMALLOC=malloc for non-free-threaded Python builds.
94+
// Free-threaded Python (with GIL disabled) manages memory differently and
95+
// should not have PYTHONMALLOC overridden.
96+
if !is_free_threaded_python() {
97+
cmd.env("PYTHONMALLOC", "malloc");
98+
}
99+
100+
cmd.env(
93101
"PATH",
94102
format!(
95103
"{}:{}:{}",

0 commit comments

Comments
 (0)