From d5bec39817c5e0287c83830f2a36580fba76774f Mon Sep 17 00:00:00 2001 From: Mykhailo Chalyi Date: Fri, 8 May 2026 00:23:20 -0500 Subject: [PATCH] fix(bindings): derive sqlite limits from host time and memory caps --- crates/bashkit-js/src/lib.rs | 16 +++++++++++++++- crates/bashkit-python/src/lib.rs | 22 +++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/bashkit-js/src/lib.rs b/crates/bashkit-js/src/lib.rs index e77296589..edd49a735 100644 --- a/crates/bashkit-js/src/lib.rs +++ b/crates/bashkit-js/src/lib.rs @@ -2409,6 +2409,20 @@ fn build_limits(state: &SharedState) -> ExecutionLimits { limits } +fn derive_sqlite_limits(state: &SharedState) -> bashkit::SqliteLimits { + let mut limits = bashkit::SqliteLimits::default(); + if let Some(ms) = state.timeout_ms { + limits = limits.max_duration(std::time::Duration::from_millis(u64::from(ms))); + } + if let Some(memory_mb) = state.max_memory { + let max_db_bytes = (memory_mb.max(0.0) * 1024.0 * 1024.0).floor() as usize; + if max_db_bytes > 0 { + limits = limits.max_db_bytes(max_db_bytes); + } + } + limits +} + fn build_bash_from_state(state: &SharedState, files: Option<&HashMap>) -> RustBash { let mut builder = RustBash::builder(); @@ -2473,7 +2487,7 @@ fn build_bash_from_state(state: &SharedState, files: Option<&HashMap bashkit::BashBuilder { +fn apply_sqlite_config( + mut builder: bashkit::BashBuilder, + sqlite: bool, + timeout_seconds: Option, + max_memory: Option, +) -> PyResult { if sqlite { - builder = builder.sqlite(); + let mut limits = bashkit::SqliteLimits::default(); + if let Some(ts) = timeout_seconds { + limits = limits.max_duration(parse_timeout_seconds(ts)?); + } + if let Some(mm) = max_memory { + limits = limits.max_db_bytes(usize::try_from(mm).unwrap_or(usize::MAX)); + } + builder = builder.sqlite_with_limits(limits); builder = builder.env("BASHKIT_ALLOW_INPROCESS_SQLITE", "1"); } - builder + Ok(builder) } /// Core bash interpreter with virtual filesystem. @@ -2979,7 +2991,7 @@ impl PyBash { handler_clone, self.external_handler_reentry_depth.clone(), ); - builder = apply_sqlite_config(builder, self.sqlite); + builder = apply_sqlite_config(builder, self.sqlite, self.timeout_seconds, self.max_memory)?; if let Some(ref net) = self.network { builder = net.apply(builder); } @@ -3094,7 +3106,7 @@ impl PyBash { handler_for_build, external_handler_reentry_depth.clone(), ); - builder = apply_sqlite_config(builder, sqlite); + builder = apply_sqlite_config(builder, sqlite, timeout_seconds, max_memory)?; if let Some(ref net) = network { builder = net.apply(builder); }