Can I use sandbox.update() to keep session alive?
Doing this will reset a session timeout to the value I pass? I want to know if I can use it to keep a session alive as long as my user is watching. await sandbox.update({ timeout: SESSION_TIMEOUT_MS })
Great. Now I have all the information needed to give a comprehensive answer.
Short answer: No, sandbox.update({ timeout }) won't keep your current session alive. It changes the timeout for the next session, not the running one.
Here's what the code shows:
###sandbox.update({ timeout }) — changes sandbox config, not the live session
In packages/vercel-sandbox/src/sandbox.ts (line 1008):
// Update the sandbox config. This config will be used on the next session.
const response = await client.updateSandbox({ ... timeout: params.timeout ... });
The comment is explicit: "This config will be used on the next session." The timeout is sent as a PATCH to /v2/sandboxes/:name, which updates the sandbox-level configuration. Notably, only networkPolicy is forwarded to the currently running session (lines 1023–1035). The timeout update is not applied to the live session.
###What you actually want: sandbox.extendTimeout(duration) or session.extendTimeout(duration)
This is the method designed for keeping a session alive. From packages/vercel-sandbox/src/session.ts (line 703–733):
/**
* Extend the timeout of the session by the specified duration.
*
* This allows you to extend the lifetime of a session up until the maximum
* execution timeout for your plan.
*
* @param duration - The duration in milliseconds to extend the timeout by
*/
async extendTimeout(duration: number, opts?: { signal?: AbortSignal }): Promise<void> {
It hits the API at POST /v2/sandboxes/sessions/:sessionId/extend-timeout with a { duration } body — this adds time to the running session.
###The keep-alive pattern from the CLI
The codebase itself implements exactly the pattern you're looking for in packages/sandbox/src/interactive-shell/extend-sandbox-timeout.ts:
const BUFFER = ms("10 seconds");
while (!signal.aborted) {
const currentTimeout = session.timeout;
if (currentTimeout == null) return;
const sleepMs = session.createdAt.getTime() + currentTimeout - Date.now() - BUFFER;
if (sleepMs > 2000) {
await setTimeout(sleepMs, null, { signal });
}
await sandbox.extendTimeout(ms("5 minutes"));
}
It periodically calls sandbox.extendTimeout() just before the session would expire (with a 10-second buffer), extending by 5 minutes each time.
###Your implementation should look something like:
// Periodically extend while the user is watching
const controller = new AbortController();
async function keepAlive() {
while (!controller.signal.aborted) {
await new Promise(r => setTimeout(r, SESSION_TIMEOUT_MS - 10_000)); // sleep until near expiry
await sandbox.extendTimeout(SESSION_TIMEOUT_MS); // add more time
}
}
keepAlive();
// When the user leaves:
controller.abort();
Note that extendTimeout extends up to the maximum execution timeout for your plan, so there is an upper bound on how long you can keep a session alive.
END OF POST