2020-11-18 19:01:46 +00:00
|
|
|
import { field, Logger, logger } from "@coder/logger"
|
2020-02-04 19:27:46 +00:00
|
|
|
import * as cp from "child_process"
|
2020-07-23 17:51:08 +00:00
|
|
|
import * as path from "path"
|
|
|
|
import * as rfs from "rotating-file-stream"
|
2020-02-04 19:27:46 +00:00
|
|
|
import { Emitter } from "../common/emitter"
|
2020-11-18 19:01:46 +00:00
|
|
|
import { DefaultedArgs } from "./cli"
|
2020-07-23 17:51:08 +00:00
|
|
|
import { paths } from "./util"
|
2020-02-04 19:27:46 +00:00
|
|
|
|
2020-11-18 18:05:24 +00:00
|
|
|
const timeoutInterval = 10000 // 10s, matches VS Code's timeouts.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to a single message from a process. Reject if the process errors,
|
|
|
|
* exits, or times out.
|
|
|
|
*
|
|
|
|
* `fn` is a function that determines whether the message is the one we're
|
|
|
|
* waiting for.
|
|
|
|
*/
|
|
|
|
export function onMessage<M, T extends M>(
|
|
|
|
proc: cp.ChildProcess | NodeJS.Process,
|
|
|
|
fn: (message: M) => message is T,
|
|
|
|
customLogger?: Logger,
|
|
|
|
): Promise<T> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const cleanup = () => {
|
|
|
|
proc.off("error", onError)
|
|
|
|
proc.off("exit", onExit)
|
|
|
|
proc.off("message", onMessage)
|
|
|
|
clearTimeout(timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
cleanup()
|
|
|
|
reject(new Error("timed out"))
|
|
|
|
}, timeoutInterval)
|
|
|
|
|
|
|
|
const onError = (error: Error) => {
|
|
|
|
cleanup()
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onExit = (code: number) => {
|
|
|
|
cleanup()
|
|
|
|
reject(new Error(`exited unexpectedly with code ${code}`))
|
|
|
|
}
|
|
|
|
|
|
|
|
const onMessage = (message: M) => {
|
|
|
|
;(customLogger || logger).trace("got message", field("message", message))
|
|
|
|
if (fn(message)) {
|
|
|
|
cleanup()
|
|
|
|
resolve(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc.on("message", onMessage)
|
|
|
|
// NodeJS.Process doesn't have `error` but binding anyway shouldn't break
|
|
|
|
// anything. It does have `exit` but the types aren't working.
|
|
|
|
;(proc as cp.ChildProcess).on("error", onError)
|
|
|
|
;(proc as cp.ChildProcess).on("exit", onExit)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:01:46 +00:00
|
|
|
interface ParentHandshakeMessage {
|
|
|
|
type: "handshake"
|
|
|
|
args: DefaultedArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ChildHandshakeMessage {
|
2020-02-04 19:27:46 +00:00
|
|
|
type: "handshake"
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RelaunchMessage {
|
|
|
|
type: "relaunch"
|
|
|
|
version: string
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:01:46 +00:00
|
|
|
type ChildMessage = RelaunchMessage | ChildHandshakeMessage
|
|
|
|
type ParentMessage = ParentHandshakeMessage
|
2020-02-04 19:27:46 +00:00
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
class ProcessError extends Error {
|
2020-02-04 19:27:46 +00:00
|
|
|
public constructor(message: string, public readonly code: number | undefined) {
|
|
|
|
super(message)
|
|
|
|
this.name = this.constructor.name
|
|
|
|
Error.captureStackTrace(this, this.constructor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-18 17:43:25 +00:00
|
|
|
* Wrapper around a process that tries to gracefully exit when a process exits
|
|
|
|
* and provides a way to prevent `process.exit`.
|
2020-02-04 19:27:46 +00:00
|
|
|
*/
|
2020-11-18 17:43:25 +00:00
|
|
|
abstract class Process {
|
|
|
|
/**
|
|
|
|
* Emit this to trigger a graceful exit.
|
|
|
|
*/
|
|
|
|
protected readonly _onDispose = new Emitter<NodeJS.Signals | undefined>()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emitted when the process is about to be disposed.
|
|
|
|
*/
|
2020-02-04 19:27:46 +00:00
|
|
|
public readonly onDispose = this._onDispose.event
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
/**
|
|
|
|
* Uniquely named logger for the process.
|
|
|
|
*/
|
|
|
|
public abstract logger: Logger
|
|
|
|
|
|
|
|
public constructor() {
|
2020-02-04 19:27:46 +00:00
|
|
|
process.on("SIGINT", () => this._onDispose.emit("SIGINT"))
|
|
|
|
process.on("SIGTERM", () => this._onDispose.emit("SIGTERM"))
|
|
|
|
process.on("exit", () => this._onDispose.emit(undefined))
|
|
|
|
|
2020-11-04 22:49:01 +00:00
|
|
|
this.onDispose((signal, wait) => {
|
2020-02-04 19:27:46 +00:00
|
|
|
// Remove listeners to avoid possibly triggering disposal again.
|
|
|
|
process.removeAllListeners()
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
// Try waiting for other handlers to run first then exit.
|
|
|
|
this.logger.debug("disposing", field("code", signal))
|
2020-11-04 22:49:01 +00:00
|
|
|
wait.then(() => this.exit(0))
|
|
|
|
setTimeout(() => this.exit(0), 5000)
|
2020-02-04 19:27:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-14 20:57:58 +00:00
|
|
|
/**
|
2020-11-18 17:43:25 +00:00
|
|
|
* Ensure control over when the process exits.
|
2020-09-14 20:57:58 +00:00
|
|
|
*/
|
|
|
|
public preventExit(): void {
|
2020-11-18 17:43:25 +00:00
|
|
|
;(process.exit as any) = (code?: number) => {
|
|
|
|
this.logger.warn(`process.exit() was prevented: ${code || "unknown code"}.`)
|
|
|
|
}
|
2020-09-14 20:57:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
private readonly processExit: (code?: number) => never = process.exit
|
2020-09-14 20:57:58 +00:00
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
/**
|
|
|
|
* Will always exit even if normal exit is being prevented.
|
|
|
|
*/
|
2020-08-17 19:17:55 +00:00
|
|
|
public exit(error?: number | ProcessError): never {
|
|
|
|
if (error && typeof error !== "number") {
|
|
|
|
this.processExit(typeof error.code === "number" ? error.code : 1)
|
|
|
|
} else {
|
|
|
|
this.processExit(error)
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 17:43:25 +00:00
|
|
|
}
|
2020-08-17 19:17:55 +00:00
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
/**
|
|
|
|
* Child process that will clean up after itself if the parent goes away and can
|
|
|
|
* perform a handshake with the parent and ask it to relaunch.
|
|
|
|
*/
|
|
|
|
class ChildProcess extends Process {
|
|
|
|
public logger = logger.named(`child:${process.pid}`)
|
|
|
|
|
|
|
|
public constructor(private readonly parentPid: number) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
// Kill the inner process if the parent dies. This is for the case where the
|
|
|
|
// parent process is forcefully terminated and cannot clean up.
|
|
|
|
setInterval(() => {
|
|
|
|
try {
|
|
|
|
// process.kill throws an exception if the process doesn't exist.
|
|
|
|
process.kill(this.parentPid, 0)
|
|
|
|
} catch (_) {
|
|
|
|
// Consider this an error since it should have been able to clean up
|
|
|
|
// the child process unless it was forcefully killed.
|
|
|
|
this.logger.error(`parent process ${parentPid} died`)
|
|
|
|
this._onDispose.emit(undefined)
|
|
|
|
}
|
|
|
|
}, 5000)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate the handshake and wait for a response from the parent.
|
|
|
|
*/
|
2020-11-18 19:01:46 +00:00
|
|
|
public async handshake(): Promise<DefaultedArgs> {
|
2020-11-18 18:05:24 +00:00
|
|
|
this.send({ type: "handshake" })
|
2020-11-18 19:01:46 +00:00
|
|
|
const message = await onMessage<ParentMessage, ParentHandshakeMessage>(
|
2020-11-18 18:05:24 +00:00
|
|
|
process,
|
2020-11-18 19:01:46 +00:00
|
|
|
(message): message is ParentHandshakeMessage => {
|
2020-11-18 18:05:24 +00:00
|
|
|
return message.type === "handshake"
|
|
|
|
},
|
|
|
|
this.logger,
|
|
|
|
)
|
2020-11-18 19:01:46 +00:00
|
|
|
return message.args
|
2020-02-04 19:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
/**
|
|
|
|
* Notify the parent process that it should relaunch the child.
|
|
|
|
*/
|
2020-02-04 19:27:46 +00:00
|
|
|
public relaunch(version: string): void {
|
|
|
|
this.send({ type: "relaunch", version })
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
/**
|
|
|
|
* Send a message to the parent.
|
|
|
|
*/
|
2020-11-18 19:01:46 +00:00
|
|
|
private send(message: ChildMessage): void {
|
2020-02-04 19:27:46 +00:00
|
|
|
if (!process.send) {
|
|
|
|
throw new Error("not spawned with IPC")
|
|
|
|
}
|
|
|
|
process.send(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-18 17:43:25 +00:00
|
|
|
* Parent process wrapper that spawns the child process and performs a handshake
|
|
|
|
* with it. Will relaunch the child if it receives a SIGUSR1 or is asked to by
|
|
|
|
* the child. If the child otherwise exits the parent will also exit.
|
2020-02-04 19:27:46 +00:00
|
|
|
*/
|
2020-11-18 17:43:25 +00:00
|
|
|
export class ParentProcess extends Process {
|
|
|
|
public logger = logger.named(`parent:${process.pid}`)
|
|
|
|
|
|
|
|
private child?: cp.ChildProcess
|
2020-02-04 19:27:46 +00:00
|
|
|
private started?: Promise<void>
|
2020-07-23 17:51:08 +00:00
|
|
|
private readonly logStdoutStream: rfs.RotatingFileStream
|
|
|
|
private readonly logStderrStream: rfs.RotatingFileStream
|
2020-02-04 19:27:46 +00:00
|
|
|
|
2020-11-18 19:01:46 +00:00
|
|
|
protected readonly _onChildMessage = new Emitter<ChildMessage>()
|
2020-11-18 17:43:25 +00:00
|
|
|
protected readonly onChildMessage = this._onChildMessage.event
|
|
|
|
|
2020-11-18 19:01:46 +00:00
|
|
|
private args?: DefaultedArgs
|
|
|
|
|
2020-11-18 19:08:57 +00:00
|
|
|
public constructor(private currentVersion: string) {
|
2020-11-18 17:43:25 +00:00
|
|
|
super()
|
|
|
|
|
2020-11-18 19:01:46 +00:00
|
|
|
process.on("SIGUSR1", async () => {
|
|
|
|
this.logger.info("Received SIGUSR1; hotswapping")
|
|
|
|
this.relaunch()
|
|
|
|
})
|
|
|
|
|
2020-07-23 17:51:08 +00:00
|
|
|
const opts = {
|
|
|
|
size: "10M",
|
|
|
|
maxFiles: 10,
|
|
|
|
}
|
|
|
|
this.logStdoutStream = rfs.createStream(path.join(paths.data, "coder-logs", "code-server-stdout.log"), opts)
|
|
|
|
this.logStderrStream = rfs.createStream(path.join(paths.data, "coder-logs", "code-server-stderr.log"), opts)
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
this.onDispose(() => {
|
2020-09-14 20:57:58 +00:00
|
|
|
this.disposeChild()
|
2020-02-04 19:27:46 +00:00
|
|
|
})
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
this.onChildMessage((message) => {
|
2020-02-04 19:27:46 +00:00
|
|
|
switch (message.type) {
|
|
|
|
case "relaunch":
|
2020-11-18 17:43:25 +00:00
|
|
|
this.logger.info(`Relaunching: ${this.currentVersion} -> ${message.version}`)
|
2020-02-04 19:27:46 +00:00
|
|
|
this.currentVersion = message.version
|
2020-08-17 19:17:55 +00:00
|
|
|
this.relaunch()
|
2020-02-04 19:27:46 +00:00
|
|
|
break
|
|
|
|
default:
|
2020-11-18 17:43:25 +00:00
|
|
|
this.logger.error(`Unrecognized message ${message}`)
|
2020-02-04 19:27:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2020-08-17 19:17:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:57:58 +00:00
|
|
|
private disposeChild(): void {
|
2020-08-17 19:17:55 +00:00
|
|
|
this.started = undefined
|
2020-11-18 17:43:25 +00:00
|
|
|
if (this.child) {
|
|
|
|
this.child.removeAllListeners()
|
|
|
|
this.child.kill()
|
2020-08-17 19:17:55 +00:00
|
|
|
}
|
2020-09-14 20:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async relaunch(): Promise<void> {
|
|
|
|
this.disposeChild()
|
2020-08-17 19:17:55 +00:00
|
|
|
try {
|
2020-11-18 19:01:46 +00:00
|
|
|
this.started = this._start()
|
|
|
|
await this.started
|
2020-08-17 19:17:55 +00:00
|
|
|
} catch (error) {
|
2020-11-18 17:43:25 +00:00
|
|
|
this.logger.error(error.message)
|
|
|
|
this.exit(typeof error.code === "number" ? error.code : 1)
|
2020-08-17 19:17:55 +00:00
|
|
|
}
|
2020-02-04 19:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 19:01:46 +00:00
|
|
|
public start(args: DefaultedArgs): Promise<void> {
|
|
|
|
// Store for relaunches.
|
|
|
|
this.args = args
|
2020-02-04 19:27:46 +00:00
|
|
|
if (!this.started) {
|
2020-09-14 22:22:24 +00:00
|
|
|
this.started = this._start()
|
2020-02-04 19:27:46 +00:00
|
|
|
}
|
|
|
|
return this.started
|
|
|
|
}
|
|
|
|
|
2020-09-14 22:22:24 +00:00
|
|
|
private async _start(): Promise<void> {
|
|
|
|
const child = this.spawn()
|
2020-11-18 17:43:25 +00:00
|
|
|
this.child = child
|
2020-09-14 22:22:24 +00:00
|
|
|
|
|
|
|
// Log both to stdout and to the log directory.
|
|
|
|
if (child.stdout) {
|
|
|
|
child.stdout.pipe(this.logStdoutStream)
|
|
|
|
child.stdout.pipe(process.stdout)
|
|
|
|
}
|
|
|
|
if (child.stderr) {
|
|
|
|
child.stderr.pipe(this.logStderrStream)
|
|
|
|
child.stderr.pipe(process.stderr)
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
this.logger.debug(`spawned inner process ${child.pid}`)
|
2020-09-14 22:22:24 +00:00
|
|
|
|
2020-11-18 17:43:25 +00:00
|
|
|
await this.handshake(child)
|
2020-09-14 22:22:24 +00:00
|
|
|
|
|
|
|
child.once("exit", (code) => {
|
2020-11-18 17:43:25 +00:00
|
|
|
this.logger.debug(`inner process ${child.pid} exited unexpectedly`)
|
|
|
|
this.exit(code || 0)
|
2020-09-14 22:22:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private spawn(): cp.ChildProcess {
|
2020-02-21 18:32:58 +00:00
|
|
|
// Use spawn (instead of fork) to use the new binary in case it was updated.
|
2020-02-25 22:20:47 +00:00
|
|
|
return cp.spawn(process.argv[0], process.argv.slice(1), {
|
2020-02-04 19:27:46 +00:00
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
CODE_SERVER_PARENT_PID: process.pid.toString(),
|
2020-11-18 19:08:57 +00:00
|
|
|
NODE_OPTIONS: `--max-old-space-size=2048 ${process.env.NODE_OPTIONS || ""}`,
|
2020-02-04 19:27:46 +00:00
|
|
|
},
|
2020-07-23 17:51:08 +00:00
|
|
|
stdio: ["ipc"],
|
2020-02-04 19:27:46 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-18 17:43:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for a handshake from the child then reply.
|
|
|
|
*/
|
2020-11-18 18:05:24 +00:00
|
|
|
private async handshake(child: cp.ChildProcess): Promise<void> {
|
2020-11-18 19:01:46 +00:00
|
|
|
if (!this.args) {
|
|
|
|
throw new Error("started without args")
|
|
|
|
}
|
|
|
|
await onMessage<ChildMessage, ChildHandshakeMessage>(
|
2020-11-18 18:05:24 +00:00
|
|
|
child,
|
2020-11-18 19:01:46 +00:00
|
|
|
(message): message is ChildHandshakeMessage => {
|
2020-11-18 18:05:24 +00:00
|
|
|
return message.type === "handshake"
|
|
|
|
},
|
|
|
|
this.logger,
|
|
|
|
)
|
2020-11-18 19:01:46 +00:00
|
|
|
this.send(child, { type: "handshake", args: this.args })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message to the child.
|
|
|
|
*/
|
|
|
|
private send(child: cp.ChildProcess, message: ParentMessage): void {
|
|
|
|
child.send(message)
|
2020-11-18 17:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process wrapper.
|
|
|
|
*/
|
|
|
|
export const wrapper =
|
|
|
|
typeof process.env.CODE_SERVER_PARENT_PID !== "undefined"
|
|
|
|
? new ChildProcess(parseInt(process.env.CODE_SERVER_PARENT_PID))
|
|
|
|
: new ParentProcess(require("../../package.json").version)
|
|
|
|
|
|
|
|
export function isChild(proc: ChildProcess | ParentProcess): proc is ChildProcess {
|
|
|
|
return proc instanceof ChildProcess
|
2020-02-04 19:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:32:58 +00:00
|
|
|
// It's possible that the pipe has closed (for example if you run code-server
|
|
|
|
// --version | head -1). Assume that means we're done.
|
2020-02-04 19:27:46 +00:00
|
|
|
if (!process.stdout.isTTY) {
|
2020-11-18 17:43:25 +00:00
|
|
|
process.stdout.on("error", () => wrapper.exit())
|
2020-02-04 19:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:56:08 +00:00
|
|
|
// Don't let uncaught exceptions crash the process.
|
|
|
|
process.on("uncaughtException", (error) => {
|
2020-11-18 17:43:25 +00:00
|
|
|
wrapper.logger.error(`Uncaught exception: ${error.message}`)
|
2020-09-14 20:56:08 +00:00
|
|
|
if (typeof error.stack !== "undefined") {
|
2020-11-18 17:43:25 +00:00
|
|
|
wrapper.logger.error(error.stack)
|
2020-09-14 20:56:08 +00:00
|
|
|
}
|
|
|
|
})
|