-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathproxyCommandRetry.ts
More file actions
71 lines (65 loc) · 2.2 KB
/
proxyCommandRetry.ts
File metadata and controls
71 lines (65 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { renameWithRetry, tempFilePath } from "../util";
import type { Logger } from "../logging/logger";
/**
* POSIX shell script that wraps `coder ssh` with retry logic for the SSH
* ProxyCommand. After sleep/wake, DNS failures cause `coder ssh` to exit
* instantly, producing unparsable output that the Remote SSH extension
* treats as a permanent error ("Reload Window"). Retrying with a delay
* allows DNS to recover, including across system suspend/resume.
*
* Only retries when the command exits quickly (< CODER_RETRY_MIN_RUNTIME).
*/
const RETRY_SCRIPT = `#!/bin/sh
# Coder SSH ProxyCommand retry wrapper.
# Written by the Coder VS Code extension; do not edit.
max_retries=\${CODER_RETRY_MAX_RETRIES:-10}
retry_sleep=\${CODER_RETRY_SLEEP:-5}
min_runtime=\${CODER_RETRY_MIN_RUNTIME:-10}
n=0
while [ $n -lt $max_retries ]; do
start=$(date +%s)
"$@"
rc=$?
elapsed=$(($(date +%s) - start))
[ $elapsed -ge $min_runtime ] && exit $rc
[ $rc -eq 0 ] && exit 0
n=$((n + 1))
echo "coder-retry: attempt $n/$max_retries failed (rc=$rc, elapsed=\${elapsed}s)" >&2
[ $n -lt $max_retries ] && sleep $retry_sleep
done
exit "$rc"
`;
const SCRIPT_NAME = "coder-ssh-retry.sh";
/**
* Ensure the retry wrapper script exists on disk and return its path.
*/
export async function ensureRetryScript(
baseDir: string,
logger: Logger,
): Promise<string> {
await fs.mkdir(baseDir, { recursive: true });
const scriptPath = path.join(baseDir, SCRIPT_NAME);
// Atomic write: temp file + rename to avoid races between concurrent
// VS Code windows writing the same script simultaneously.
const tmpPath = tempFilePath(scriptPath, "tmp");
await fs.writeFile(tmpPath, RETRY_SCRIPT, { mode: 0o755 });
try {
await renameWithRetry(
(src, dest) => fs.rename(src, dest),
tmpPath,
scriptPath,
);
} catch (error) {
await fs.unlink(tmpPath).catch((unlinkErr: NodeJS.ErrnoException) => {
if (unlinkErr.code !== "ENOENT") {
logger.warn("Failed to clean up temp retry script", tmpPath, unlinkErr);
}
});
throw new Error(`Failed to write retry script to ${scriptPath}`, {
cause: error,
});
}
return scriptPath;
}