Skip to content

Commit 14f943d

Browse files
committed
fix(sdk): add timeout for pending_build state to prevent indefinite waits
Signed-off-by: Bruno Grbavac <[email protected]>
1 parent 3e8025b commit 14f943d

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

libs/sdk-python/src/daytona/_async/daytona.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,16 @@ async def should_terminate():
437437
SandboxState.BUILD_FAILED,
438438
]
439439

440+
pending_build_start = time.time()
441+
440442
while response_ref["response"].state == SandboxState.PENDING_BUILD:
443+
if timeout:
444+
elapsed = time.time() - pending_build_start
445+
if elapsed > timeout:
446+
raise DaytonaError(
447+
f"Sandbox build has been pending for more than {timeout} seconds."
448+
f"Please check the sandbox state again later."
449+
)
441450
await asyncio.sleep(1)
442451
response_ref["response"] = await self._sandbox_api.get_sandbox(response_ref["response"].id)
443452

libs/sdk-python/src/daytona/_sync/daytona.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,16 @@ def should_terminate():
398398
SandboxState.BUILD_FAILED,
399399
]
400400

401+
pending_build_start = time.time()
402+
401403
while response_ref["response"].state == SandboxState.PENDING_BUILD:
404+
if timeout:
405+
elapsed = time.time() - pending_build_start
406+
if elapsed > timeout:
407+
raise DaytonaError(
408+
f"Sandbox build has been pending for more than {timeout} seconds."
409+
f"Please check the sandbox state again later."
410+
)
402411
time.sleep(1)
403412
response_ref["response"] = self._sandbox_api.get_sandbox(response_ref["response"].id)
404413

libs/sdk-typescript/src/Daytona.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,17 @@ export class Daytona {
505505
SandboxState.BUILD_FAILED,
506506
]
507507

508+
const pendingBuildStart = Date.now()
509+
508510
while (sandboxInstance.state === SandboxState.PENDING_BUILD) {
511+
if (options.timeout) {
512+
const elapsed = (Date.now() - pendingBuildStart) / 1000
513+
if (elapsed > options.timeout) {
514+
throw new DaytonaError(
515+
`Sandbox build has been pending for more than ${options.timeout} seconds. Please check the sandbox state again later.`,
516+
)
517+
}
518+
}
509519
await new Promise((resolve) => setTimeout(resolve, 1000))
510520
sandboxInstance = (await this.sandboxApi.getSandbox(sandboxInstance.id)).data
511521
}

0 commit comments

Comments
 (0)