[model-gateway] fix WASM memory limit per module (#14600)
This commit is contained in:
@@ -15,7 +15,7 @@ use tokio::sync::oneshot;
|
|||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
use wasmtime::{
|
use wasmtime::{
|
||||||
component::{Component, Linker, ResourceTable},
|
component::{Component, Linker, ResourceTable},
|
||||||
Config, Engine, Store, UpdateDeadline,
|
Config, Engine, Store, StoreLimitsBuilder, UpdateDeadline,
|
||||||
};
|
};
|
||||||
use wasmtime_wasi::WasiCtx;
|
use wasmtime_wasi::WasiCtx;
|
||||||
|
|
||||||
@@ -342,14 +342,34 @@ impl WasmThreadPool {
|
|||||||
let mut linker = Linker::<WasiState>::new(engine);
|
let mut linker = Linker::<WasiState>::new(engine);
|
||||||
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
|
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
|
||||||
let mut builder = WasiCtx::builder();
|
let mut builder = WasiCtx::builder();
|
||||||
|
|
||||||
|
// Create memory limits from config.
|
||||||
|
// Use the config helper to get total bytes, then safely convert to usize.
|
||||||
|
let memory_limit_bytes =
|
||||||
|
usize::try_from(config.get_total_memory_bytes()).map_err(|_| {
|
||||||
|
WasmError::from(WasmRuntimeError::CallFailed(
|
||||||
|
"Configured WASM memory limit exceeds addressable space on this platform."
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let limits = StoreLimitsBuilder::new()
|
||||||
|
.memory_size(memory_limit_bytes)
|
||||||
|
.trap_on_grow_failure(true) // Trap instead of returning -1 for easier debugging
|
||||||
|
.build();
|
||||||
|
|
||||||
let mut store = Store::new(
|
let mut store = Store::new(
|
||||||
engine,
|
engine,
|
||||||
WasiState {
|
WasiState {
|
||||||
ctx: builder.build(),
|
ctx: builder.build(),
|
||||||
table: ResourceTable::new(),
|
table: ResourceTable::new(),
|
||||||
|
limits,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Apply resource limits to the store.
|
||||||
|
// This enforces max_memory_pages by preventing memory.grow beyond the limit.
|
||||||
|
store.limiter(|state| &mut state.limits);
|
||||||
|
|
||||||
// Set epoch deadline for timeout enforcement.
|
// Set epoch deadline for timeout enforcement.
|
||||||
// The deadline is the number of epoch ticks before execution is interrupted.
|
// The deadline is the number of epoch ticks before execution is interrupted.
|
||||||
// With EPOCH_INTERVAL_MS=100ms and max_execution_time_ms=1000ms, deadline=10 epochs.
|
// With EPOCH_INTERVAL_MS=100ms and max_execution_time_ms=1000ms, deadline=10 epochs.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
//! Provides generic input/output types for WASM component execution
|
//! Provides generic input/output types for WASM component execution
|
||||||
//! based on attach points.
|
//! based on attach points.
|
||||||
|
|
||||||
use wasmtime::component::ResourceTable;
|
use wasmtime::{component::ResourceTable, StoreLimits};
|
||||||
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
|
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
|
||||||
|
|
||||||
use crate::wasm::{
|
use crate::wasm::{
|
||||||
@@ -89,6 +89,7 @@ impl WasmComponentOutput {
|
|||||||
pub struct WasiState {
|
pub struct WasiState {
|
||||||
pub ctx: WasiCtx,
|
pub ctx: WasiCtx,
|
||||||
pub table: ResourceTable,
|
pub table: ResourceTable,
|
||||||
|
pub limits: StoreLimits,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WasiView for WasiState {
|
impl WasiView for WasiState {
|
||||||
|
|||||||
Reference in New Issue
Block a user