Skip to content

Commit cf0f111

Browse files
committed
Handle upgrade from binary to loose files
As best we can, anyway.
1 parent 9b7a203 commit cf0f111

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/node/wrapper.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { logger, field } from "@coder/logger"
22
import * as cp from "child_process"
3+
import * as fs from "fs-extra"
4+
import * as path from "path"
35
import { Emitter } from "../common/emitter"
46

57
interface HandshakeMessage {
@@ -175,40 +177,53 @@ export class WrapperProcess {
175177

176178
public start(): Promise<void> {
177179
if (!this.started) {
178-
const child = this.spawn()
179-
logger.debug(`spawned inner process ${child.pid}`)
180-
this.started = ipcMain()
181-
.handshake(child)
182-
.then(() => {
183-
child.once("exit", (code) => {
184-
logger.debug(`inner process ${child.pid} exited unexpectedly`)
185-
ipcMain().exit(code || 0)
180+
this.started = this.spawn().then((child) => {
181+
logger.debug(`spawned inner process ${child.pid}`)
182+
ipcMain()
183+
.handshake(child)
184+
.then(() => {
185+
child.once("exit", (code) => {
186+
logger.debug(`inner process ${child.pid} exited unexpectedly`)
187+
ipcMain().exit(code || 0)
188+
})
186189
})
187-
})
188-
this.process = child
190+
this.process = child
191+
})
189192
}
190193
return this.started
191194
}
192195

193-
private spawn(): cp.ChildProcess {
196+
private async spawn(): Promise<cp.ChildProcess> {
194197
// Flags to pass along to the Node binary.
195198
let nodeOptions = `${process.env.NODE_OPTIONS || ""} ${(this.options && this.options.nodeOptions) || ""}`
196199
if (!/max_old_space_size=(\d+)/g.exec(nodeOptions)) {
197200
nodeOptions += ` --max_old_space_size=${(this.options && this.options.maxMemory) || 2048}`
198201
}
199202

200-
return cp.fork(process.argv[1], process.argv.slice(2), {
203+
// This is to handle the upgrade from binary release to loose release. This
204+
// will only work for users that restart code-server entirely between
205+
// upgrading to this version and the loose file version since this is the
206+
// wrapper code that does not get updated. The hope is that it'll be likely
207+
// for that to happen to most users in that timeframe to minimize disruption
208+
// when loose files are release. This can be removed with that release.
209+
const bundledNodePath = path.join(process.argv[0], "node")
210+
const binary = (await fs.pathExists(bundledNodePath)) ? bundledNodePath : process.argv[0]
211+
212+
// Use spawn (instead of fork) to use the new binary in case it was updated.
213+
return cp.spawn(binary, process.argv.slice(1), {
201214
env: {
202215
...process.env,
203216
CODE_SERVER_PARENT_PID: process.pid.toString(),
204217
NODE_OPTIONS: nodeOptions,
218+
NBIN_BYPASS: undefined,
205219
},
220+
stdio: ["inherit", "inherit", "inherit", "ipc"],
206221
})
207222
}
208223
}
209224

210-
// // It's possible that the pipe has closed (for example if you run code-server
211-
// // --version | head -1). Assume that means we're done.
225+
// It's possible that the pipe has closed (for example if you run code-server
226+
// --version | head -1). Assume that means we're done.
212227
if (!process.stdout.isTTY) {
213228
process.stdout.on("error", () => ipcMain().exit())
214229
}

0 commit comments

Comments
 (0)