Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wide-ants-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sandbox": patch
---

Ensure that the interactive server is available before creating websocket connection requests.
5 changes: 3 additions & 2 deletions packages/sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"version": "4.1.0",
"scripts": {
"clean": "rm -rf node_modules dist",
"sandbox": "ts-node ./src/sandbox.ts",
"sandbox": "tsx ./src/sandbox.ts",
"build": "tsdown",
"vercel-build": "ts-node ./scripts/create-preview-deployment-tgz.ts",
"vercel-build": "tsx ./scripts/create-preview-deployment-tgz.ts",
"test": "vitest",
"example-whoami": "ts-node ./src/example-whoami.ts",
"example-next": "ts-node ./src/example-next.ts",
Expand Down Expand Up @@ -61,6 +61,7 @@
"open": "^10.2.0",
"ora": "^8.2.0",
"tsdown": "catalog:",
"tsx": "4.20.3",
"vitest": "catalog:",
"xdg-app-paths": "5.1.0"
}
Expand Down
54 changes: 51 additions & 3 deletions packages/sandbox/src/interactive-shell/interactive-shell.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Sandbox } from "@vercel/sandbox";
import createDebugger from "debug";
import retry from "async-retry";
import { WebSocket } from "ws";
import { printCommand } from "../util/print-command";
import ora from "ora";
import { acquireRelease, createAbortController, defer } from "../util/disposables";
import {
acquireRelease,
createAbortController,
defer,
} from "../util/disposables";
import chalk from "chalk";
import { extendSandboxTimeoutPeriodically } from "./extend-sandbox-timeout";

Expand Down Expand Up @@ -68,6 +73,7 @@ export async function startInteractiveShell(options: {
(s) => s.stop(),
);

debug("Opening interactive session...");
progress.text = "Opening interactive session...";
const { url, token } = await options.sandbox.openInteractive();

Expand All @@ -76,8 +82,47 @@ export async function startInteractiveShell(options: {
? ["sudo", command, ...args]
: [command, ...args];

debug("Connecting...");
progress.text = "Connecting...";
const client = new WebSocket(`${url}?token=${encodeURIComponent(token)}`);

let client: WebSocket;
try {
// Wait for the websocket server to be ready
// to accept traffic. This is important for
// cross region connections because the sevrer
// will have been instructed to start, but may not
// be fully ready to accept connections yet.
const { hostname } = new URL(url);
await retry(
async (_, attempt) => {
debug("Validating server health... (attempt %d)", attempt);
const response = await fetch(`https://${hostname}/health`);
if (!response.ok) {
throw new Error(`Server health check failed: ${response.statusText}`);
}
},
{
onRetry: (error: unknown, attempt: number) => {
debug(
"Server health check failed: %s (attempt %d)",
(error as Error).message,
attempt,
);
},
retries: 5,
minTimeout: 100,
Comment thread
LukeSheard marked this conversation as resolved.
randomize: true,
},
);

client = new WebSocket(`${url}?token=${encodeURIComponent(token)}`);
} catch (e) {
console.error(
chalk.dim(`\n╰▶ connection to ▲ ${options.sandbox.name} failed.`),
);
process.exitCode = 1;
return;
}
using _client = defer(() => {
try {
client.close();
Expand All @@ -91,6 +136,7 @@ export async function startInteractiveShell(options: {
client.once("error", (err) => reject(err));
});
debug("connected to %s", url);
progress.text = "Connected successfully.";

client.send(
JSON.stringify({
Expand Down Expand Up @@ -165,7 +211,9 @@ export async function startInteractiveShell(options: {
process.removeListener("SIGWINCH", onResize);
process.stdin.removeListener("data", onStdin);

console.error(chalk.dim(`\n╰▶ connection to ▲ ${options.sandbox.name} closed.`));
console.error(
chalk.dim(`\n╰▶ connection to ▲ ${options.sandbox.name} closed.`),
);
}

function toEnvArray(env: Record<string, string>): string[] {
Expand Down
12 changes: 4 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading