@@ -158,6 +158,7 @@ class ModalSandboxClientOptions(BaseSandboxClientOptions):
158158 timeout : int = 300 # Lifetime of a sandbox from creation in seconds, defaults to 5 minutes
159159 use_sleep_cmd : bool = True
160160 image_builder_version : str | None = _DEFAULT_IMAGE_BUILDER_VERSION
161+ idle_timeout : int | None = None
161162
162163 def __init__ (
163164 self ,
@@ -171,6 +172,7 @@ def __init__(
171172 timeout : int = 300 , # 5 minutes
172173 use_sleep_cmd : bool = True ,
173174 image_builder_version : str | None = _DEFAULT_IMAGE_BUILDER_VERSION ,
175+ idle_timeout : int | None = None ,
174176 * ,
175177 type : Literal ["modal" ] = "modal" ,
176178 ) -> None :
@@ -186,6 +188,7 @@ def __init__(
186188 timeout = timeout ,
187189 use_sleep_cmd = use_sleep_cmd ,
188190 image_builder_version = image_builder_version ,
191+ idle_timeout = idle_timeout ,
189192 )
190193
191194
@@ -319,6 +322,7 @@ class ModalSandboxSessionState(SandboxSessionState):
319322 timeout : int = 300 # 5 minutes
320323 use_sleep_cmd : bool = True
321324 image_builder_version : str | None = _DEFAULT_IMAGE_BUILDER_VERSION
325+ idle_timeout : int | None = None
322326
323327
324328@dataclass
@@ -571,6 +575,7 @@ async def _ensure_sandbox(self) -> bool:
571575 volumes = volumes ,
572576 gpu = self .state .gpu ,
573577 timeout = self .state .timeout ,
578+ idle_timeout = self .state .idle_timeout ,
574579 )
575580 async with _override_modal_image_builder_version (self .state .image_builder_version ):
576581 if self .state .sandbox_create_timeout_s is None :
@@ -1625,6 +1630,7 @@ async def _run_restore() -> None:
16251630 volumes = self ._modal_cloud_bucket_mounts_for_manifest (),
16261631 gpu = self .state .gpu ,
16271632 timeout = self .state .timeout ,
1633+ idle_timeout = self .state .idle_timeout ,
16281634 )
16291635 try :
16301636 mkdir_proc = await sb .exec .aio ("mkdir" , "-p" , "--" , root .as_posix (), text = False )
@@ -1839,6 +1845,7 @@ async def create(
18391845 - snapshot_filesystem_restore_timeout_s: float | None
18401846 (async timeout for snapshot restore call)
18411847 - timeout: int (maximum sandbox lifetime in seconds, default 300)
1848+ - idle_timeout: int | None (maximum sandbox inactivity in seconds, default None)
18421849 - image_builder_version: str | None (Modal image builder version, default "2025.06")
18431850 """
18441851
@@ -1960,6 +1967,7 @@ async def create(
19601967 timeout = options .timeout ,
19611968 use_sleep_cmd = options .use_sleep_cmd ,
19621969 image_builder_version = image_builder_version ,
1970+ idle_timeout = options .idle_timeout ,
19631971 )
19641972 if sandbox_create_timeout_s is not None :
19651973 state .sandbox_create_timeout_s = float (sandbox_create_timeout_s )
0 commit comments