|
2 | 2 | title: Sessions |
3 | 3 | pcx_content_type: configuration |
4 | 4 | sidebar: |
5 | | - order: 5 |
| 5 | + order: 6 |
6 | 6 | --- |
7 | 7 |
|
8 | 8 | import { TypeScriptExample } from "~/components"; |
9 | 9 |
|
10 | 10 | Create isolated execution contexts within a sandbox. Each session maintains its own shell state, environment variables, and working directory. See [Session management concept](/sandbox/concepts/sessions/) for details. |
11 | 11 |
|
12 | 12 | :::note |
13 | | -Every sandbox has a default session that automatically maintains shell state. Create additional sessions when you need isolated shell contexts for different environments or parallel workflows. |
14 | | -::: |
15 | | - |
16 | | -:::note |
17 | | -For sandbox-level configuration options like `keepAlive`, refer to the [Sandbox options configuration](/sandbox/configuration/sandbox-options/). |
| 13 | +Every sandbox has a default session that automatically maintains shell state. Create additional sessions when you need isolated shell contexts for different environments or parallel workflows. For sandbox-level operations like creating containers or destroying the entire sandbox, see the [Lifecycle API](/sandbox/api/lifecycle/). |
18 | 14 | ::: |
19 | 15 |
|
20 | 16 | ## Methods |
@@ -84,6 +80,44 @@ const result = await session.exec('cd repo && npm run build'); |
84 | 80 | ``` |
85 | 81 | </TypeScriptExample> |
86 | 82 |
|
| 83 | +--- |
| 84 | + |
| 85 | +### `deleteSession()` |
| 86 | + |
| 87 | +Delete a session and clean up its resources. |
| 88 | + |
| 89 | +```ts |
| 90 | +const result = await sandbox.deleteSession(sessionId: string): Promise<SessionDeleteResult> |
| 91 | +``` |
| 92 | + |
| 93 | +**Parameters**: |
| 94 | +- `sessionId` - ID of the session to delete (cannot be `"default"`) |
| 95 | + |
| 96 | +**Returns**: `Promise<SessionDeleteResult>` containing: |
| 97 | +- `success` - Whether deletion succeeded |
| 98 | +- `sessionId` - ID of the deleted session |
| 99 | +- `timestamp` - Deletion timestamp |
| 100 | + |
| 101 | +<TypeScriptExample> |
| 102 | +``` |
| 103 | +// Create a temporary session for a specific task |
| 104 | +const tempSession = await sandbox.createSession({ id: 'temp-task' }); |
| 105 | +
|
| 106 | +try { |
| 107 | + await tempSession.exec('npm run heavy-task'); |
| 108 | +} finally { |
| 109 | + // Clean up the session when done |
| 110 | + await sandbox.deleteSession('temp-task'); |
| 111 | +} |
| 112 | +``` |
| 113 | +</TypeScriptExample> |
| 114 | + |
| 115 | +:::caution |
| 116 | +Deleting a session immediately terminates all running commands. The default session cannot be deleted. |
| 117 | +::: |
| 118 | + |
| 119 | +--- |
| 120 | + |
87 | 121 | ### `setEnvVars()` |
88 | 122 |
|
89 | 123 | Set environment variables in the sandbox. |
@@ -115,45 +149,19 @@ await sandbox.exec('python script.py'); |
115 | 149 | ``` |
116 | 150 | </TypeScriptExample> |
117 | 151 |
|
118 | | -### `destroy()` |
119 | | - |
120 | | -Destroy the sandbox container and free up resources. |
121 | | - |
122 | | -```ts |
123 | | -await sandbox.destroy(): Promise<void> |
124 | | -``` |
125 | | - |
126 | | -:::note |
127 | | -Containers automatically sleep after 10 minutes of inactivity, but still count toward account limits. Use `destroy()` to immediately free up resources. |
128 | | - |
129 | | -**Important**: When using [`keepAlive: true`](/sandbox/configuration/sandbox-options/#keepalive), containers will not automatically timeout, so calling `destroy()` is **required** to prevent containers running indefinitely. |
130 | | -::: |
131 | | - |
132 | | -<TypeScriptExample> |
133 | | -``` |
134 | | -async function executeCode(code: string): Promise<string> { |
135 | | - const sandbox = getSandbox(env.Sandbox, `temp-${Date.now()}`); |
136 | | -
|
137 | | - try { |
138 | | - await sandbox.writeFile('/tmp/code.py', code); |
139 | | - const result = await sandbox.exec('python /tmp/code.py'); |
140 | | - return result.stdout; |
141 | | - } finally { |
142 | | - await sandbox.destroy(); |
143 | | - } |
144 | | -} |
145 | | -``` |
146 | | -</TypeScriptExample> |
| 152 | +--- |
147 | 153 |
|
148 | 154 | ## ExecutionSession methods |
149 | 155 |
|
150 | 156 | The `ExecutionSession` object has all sandbox methods bound to the specific session: |
151 | 157 |
|
152 | | -**Commands**: [`exec()`](/sandbox/api/commands/#exec), [`execStream()`](/sandbox/api/commands/#execstream) |
153 | | -**Processes**: [`startProcess()`](/sandbox/api/commands/#startprocess), [`listProcesses()`](/sandbox/api/commands/#listprocesses), [`killProcess()`](/sandbox/api/commands/#killprocess), [`killAllProcesses()`](/sandbox/api/commands/#killallprocesses), [`getProcessLogs()`](/sandbox/api/commands/#getprocesslogs), [`streamProcessLogs()`](/sandbox/api/commands/#streamprocesslogs) |
154 | | -**Files**: [`writeFile()`](/sandbox/api/files/#writefile), [`readFile()`](/sandbox/api/files/#readfile), [`mkdir()`](/sandbox/api/files/#mkdir), [`deleteFile()`](/sandbox/api/files/#deletefile), [`renameFile()`](/sandbox/api/files/#renamefile), [`moveFile()`](/sandbox/api/files/#movefile), [`gitCheckout()`](/sandbox/api/files/#gitcheckout) |
155 | | -**Environment**: [`setEnvVars()`](/sandbox/api/sessions/#setenvvars) |
156 | | -**Code Interpreter**: [`createCodeContext()`](/sandbox/api/interpreter/#createcodecontext), [`runCode()`](/sandbox/api/interpreter/#runcode), [`listCodeContexts()`](/sandbox/api/interpreter/#listcodecontexts), [`deleteCodeContext()`](/sandbox/api/interpreter/#deletecodecontext) |
| 158 | +| Category | Methods | |
| 159 | +| -------- | ------- | |
| 160 | +| **Commands** | [`exec()`](/sandbox/api/commands/#exec), [`execStream()`](/sandbox/api/commands/#execstream) | |
| 161 | +| **Processes** | [`startProcess()`](/sandbox/api/commands/#startprocess), [`listProcesses()`](/sandbox/api/commands/#listprocesses), [`killProcess()`](/sandbox/api/commands/#killprocess), [`killAllProcesses()`](/sandbox/api/commands/#killallprocesses), [`getProcessLogs()`](/sandbox/api/commands/#getprocesslogs), [`streamProcessLogs()`](/sandbox/api/commands/#streamprocesslogs) | |
| 162 | +| **Files** | [`writeFile()`](/sandbox/api/files/#writefile), [`readFile()`](/sandbox/api/files/#readfile), [`mkdir()`](/sandbox/api/files/#mkdir), [`deleteFile()`](/sandbox/api/files/#deletefile), [`renameFile()`](/sandbox/api/files/#renamefile), [`moveFile()`](/sandbox/api/files/#movefile), [`gitCheckout()`](/sandbox/api/files/#gitcheckout) | |
| 163 | +| **Environment** | [`setEnvVars()`](/sandbox/api/sessions/#setenvvars) | |
| 164 | +| **Code Interpreter** | [`createCodeContext()`](/sandbox/api/interpreter/#createcodecontext), [`runCode()`](/sandbox/api/interpreter/#runcode), [`listCodeContexts()`](/sandbox/api/interpreter/#listcodecontexts), [`deleteCodeContext()`](/sandbox/api/interpreter/#deletecodecontext) | |
157 | 165 |
|
158 | 166 | ## Related resources |
159 | 167 |
|
|
0 commit comments