Fix --socket-fd when code-server is socket-activated (#8083)

The --socket-fd flag never worked. The wrapper forks the process that
listens, but only the IPC channel was inherited as fd 3, so the socket
passed by the caller was not available to the child. Node then tried to
listen on the IPC pipe and failed with "listen EEXIST".

Pass the socket to the child as stdio entry 4 and tell the child to use
that fd in the handshake.

Node also reports no address for a Unix socket adopted by fd, which made
startup fail with "Server has no address". Log the listener as fd:<n>
in that case.

Add an integration test that starts the code-server binary with a Unix
or TCP socket as fd 3 and checks that /healthz answers over it.
This commit is contained in:
Andy Piper
2026-10-28 19:11:06 -04:00
committed by GitHub
parent 70fc911358
commit 27808460b1
6 changed files with 103 additions and 4 deletions

View File

@@ -111,9 +111,14 @@ export const createApp = async (args: DefaultedArgs): Promise<App> => {
*
* The address might be a URL or it might be a pipe or socket path.
*/
export const ensureAddress = (server: http.Server, protocol: string): URL | string => {
export const ensureAddress = (server: http.Server, protocol: string, socketFd?: number): URL | string => {
const addr = server.address()
// Node reports no address for a Unix socket adopted by fd.
if (!addr && typeof socketFd === "number") {
return `fd:${socketFd}`
}
if (!addr) {
throw new Error("Server has no address")
}

View File

@@ -141,7 +141,7 @@ export const runCodeServer = async (
const app = await createApp(args)
const protocol = args.cert ? "https" : "http"
const serverAddress = ensureAddress(app.server, protocol)
const serverAddress = ensureAddress(app.server, protocol, args["socket-fd"])
const { disposeRoutes, heart } = await register(app, args)
logger.info(`Using config file ${args.config}`)

View File

@@ -8,6 +8,9 @@ import { paths } from "./util"
const timeoutInterval = 10000 // 10s, matches VS Code's timeouts.
// Fd 3 in the child is the IPC channel, so the inherited socket goes to fd 4.
const CHILD_SOCKET_FD = 4
/**
* Listen to a single message from a process. Reject if the process errors,
* exits, or times out.
@@ -334,13 +337,14 @@ export class ParentProcess extends Process {
}
private spawn(): cp.ChildProcess {
const socketFd = this.args?.["socket-fd"]
return cp.fork(path.join(__dirname, "entry"), {
env: {
...process.env,
CODE_SERVER_PARENT_PID: process.pid.toString(),
NODE_EXEC_PATH: process.execPath,
},
stdio: ["pipe", "pipe", "pipe", "ipc"],
stdio: typeof socketFd === "number" ? ["pipe", "pipe", "pipe", "ipc", socketFd] : ["pipe", "pipe", "pipe", "ipc"],
})
}
@@ -359,7 +363,8 @@ export class ParentProcess extends Process {
this.logger,
)
this.logger.debug("got message", field("message", message))
this.send(child, { type: "handshake", args: this.args })
const args = typeof this.args["socket-fd"] === "number" ? { ...this.args, "socket-fd": CHILD_SOCKET_FD } : this.args
this.send(child, { type: "handshake", args })
}
/**