Skip to content

Commit 639911b

Browse files
committed
Add reconnection logic
1 parent 98276d9 commit 639911b

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Open any workspace with a single click.",
66
"repository": "https://github.com/coder/vscode-coder",
77
"preview": true,
8-
"version": "0.0.2",
8+
"version": "0.0.3",
99
"engines": {
1010
"vscode": "^1.73.0"
1111
},

src/remote.ts

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -263,36 +263,67 @@ export class Remote {
263263
}
264264
const binPath = agent.operating_system === "windows" ? "code-server" : "$HOME/.vscode-remote/bin/code-server"
265265

266-
const remotePort = await new Promise<number>((resolve, reject) => {
267-
const script =
268-
binPath +
269-
" serve-local --start-server --port 0 --without-connection-token --commit-id " +
270-
this.vscodeCommit +
271-
" --accept-server-license-terms"
272-
this.ipc
273-
?.execute(shell, script, (data) => {
274-
const lines = data.split("\n")
275-
lines.forEach((line) => {
276-
this.output.appendLine(line)
266+
let running: {
267+
commit: string
268+
process_id: number
269+
}[] = []
270+
await this.ipc.execute(shell, `${binPath} ps`, (data) => {
271+
try {
272+
running = JSON.parse(data)
273+
} catch {
274+
// We can ignore this, it's probably blank!
275+
}
276+
})
277+
// Store the running port for the current commit in a file for reconnection!
278+
const portFilePath = `/tmp/.vscode-remote-${this.vscodeCommit}-port`
279+
let remotePort = 0
280+
if (running.filter((instance) => instance.commit === this.vscodeCommit)) {
281+
await this.ipc.execute(shell, `cat ${portFilePath}`, (data) => {
282+
if (data.trim()) {
283+
remotePort = Number.parseInt(data.trim())
284+
}
285+
})
277286

278-
if (!line.startsWith("Server bound to")) {
279-
return
280-
}
281-
const parts = line.split(" ").filter((part) => part.startsWith("127.0.0.1:"))
282-
if (parts.length === 0) {
283-
return reject("No port found in output: " + line)
284-
}
285-
const port = parts[0].split(":").pop()
286-
if (!port) {
287-
return reject("No port found in parts: " + parts.join(","))
288-
}
289-
resolve(Number.parseInt(port))
287+
this.output.appendLine("Found existing server running on port: " + remotePort)
288+
}
289+
290+
if (!remotePort) {
291+
remotePort = await new Promise<number>((resolve, reject) => {
292+
const script =
293+
binPath +
294+
" serve-local --start-server --port 0 --without-connection-token --commit-id " +
295+
this.vscodeCommit +
296+
" --accept-server-license-terms"
297+
this.ipc
298+
?.execute(shell, script, (data) => {
299+
const lines = data.split("\n")
300+
lines.forEach((line) => {
301+
this.output.appendLine(line)
302+
if (!line.startsWith("Server bound to")) {
303+
return
304+
}
305+
const parts = line.split(" ").filter((part) => part.startsWith("127.0.0.1:"))
306+
if (parts.length === 0) {
307+
return reject("No port found in output: " + line)
308+
}
309+
const port = parts[0].split(":").pop()
310+
if (!port) {
311+
return reject("No port found in parts: " + parts.join(","))
312+
}
313+
resolve(Number.parseInt(port))
314+
})
290315
})
291-
})
292-
.then((exitCode) => {
293-
reject("Exited with: " + exitCode)
294-
})
295-
})
316+
.then((exitCode) => {
317+
reject("Exited with: " + exitCode)
318+
})
319+
})
320+
321+
await this.ipc.execute(
322+
shell,
323+
`echo ${remotePort} > /tmp/.vscode-remote-${this.vscodeCommit}-port`,
324+
() => undefined,
325+
)
326+
}
296327

297328
const forwarded = await this.ipc.portForward(remotePort)
298329
vscode.commands.executeCommand("setContext", "forwardedPortsViewEnabled", true)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy