The hand-patched diagnostic build becomes a feature: --verbose gates the timing lines (request answered / notification handled / HELD behind a rescan / scan and widen lifecycle / flush / reindex_one, the exact wordings of the two diagnostic builds) as LOG messages into the client's output channel; without the flag not one line is sent and the cost is one branch, measured unmeasurable against HEAD. Unknown args exit 2 with usage before the transport opens. The extension gains lucisv.verbose (default off) and its first restart-on-settings machinery: restart.js, one serialized chain reconciling to the last toggle, no client transition outside it — activation and deactivate included — after QA twice caught the alternatives (a mid-start toggle silently rejoining the stale start; deactivate's out-of-chain stop orphaning a spawn). Headless node harness restart_test.js, 44 checks, five mutations caught. Spawn contract amended everywhere it is written: no args required, --verbose optional. Co-Authored-By: Claude Fable 5 <[email protected]>
171 lines
7.7 KiB
JavaScript
171 lines
7.7 KiB
JavaScript
// One serialized restart chain for the language server, and nothing else.
|
|
//
|
|
// The server's arguments are decided at spawn time (`--verbose`, from
|
|
// `lucisv.verbose`), so the only way to change them is to start a new process.
|
|
// Doing that naively loses toggles: `vscode-languageclient` 9's `start()`
|
|
// returns the *in-flight* start whenever one exists, and its `stop()` rejects
|
|
// while the client is still `Starting` — so a toggle that lands during a slow
|
|
// first scan can stop nothing, rejoin the start that is already running with
|
|
// the old arguments, and report success. The user then believes the setting
|
|
// took effect while the server has no idea about it (QA finding 1, 2026-08-29).
|
|
//
|
|
// The fix is one rule: **there is exactly one chain of transitions, and it
|
|
// always reconciles to the last arguments anybody asked for.** Every toggle
|
|
// records what it wants and queues onto the same promise, so two transitions
|
|
// can never overlap, and a burst of toggles collapses into at most one restart
|
|
// that lands on the final answer.
|
|
//
|
|
// The rule has a second half, and it is the same rule: **no transition exists
|
|
// outside the chain either** — teardown included. `deactivate()` used to stop
|
|
// the client itself, attached to a *snapshot* of the chain, so a toggle landing
|
|
// in the same tick queued a whole reconcile — stop and then **spawn** — beside
|
|
// it, with nothing left to await the new process. On an extension disable or an
|
|
// extension-host restart, where the host outlives the module, that server is an
|
|
// orphan (QA finding 3, 2026-08-29). `dispose()` is the answer: it closes the
|
|
// door (`want()` and any already-queued reconcile become no-ops) and puts the
|
|
// final stop on the same chain as everything else.
|
|
//
|
|
// This file knows nothing about VS Code — it takes a client-like object with
|
|
// `start()`/`stop()` — which is what lets `restart_test.js` drive it headlessly
|
|
// without re-implementing the language client's own state machine.
|
|
// See docs/features/verbose-timing/README.md.
|
|
|
|
/**
|
|
* Whether two argument lists are the same list.
|
|
*
|
|
* `null` is "nothing is running", and it is equal to nothing — not even to
|
|
* another `null` — so a reconcile after a failed start always tries again.
|
|
*/
|
|
function sameArgs(a, b) {
|
|
if (a === null || b === null) return false;
|
|
return a.length === b.length && a.every((arg, index) => arg === b[index]);
|
|
}
|
|
|
|
/**
|
|
* A restarter for one language client.
|
|
*
|
|
* - `client` — anything with `start()` and `stop()` returning promises.
|
|
* - `serverOptions` — the object the client reads its spawn arguments from at
|
|
* every `start()`; the desired arguments are written into `.args` here.
|
|
* - `onStarted` — called after each successful start (the decorations went
|
|
* with the old connection and have to be repainted).
|
|
* - `onStartFailed` — called with the error when a start rejects. Not
|
|
* optional, and not a place to swallow: the chain survives, so the next
|
|
* toggle retries.
|
|
*
|
|
* `start()` is the first link of the chain — the activation spawn — so a
|
|
* toggle arriving while it is still running queues behind it instead of
|
|
* racing it. `dispose()` is the last one. That ordering *is* the fix: calling
|
|
* `client.start()` or `client.stop()` anywhere else puts a transition outside
|
|
* the chain again, which is how both of this feature's restart bugs happened.
|
|
*/
|
|
function createRestarter({ client, serverOptions, onStarted, onStartFailed }) {
|
|
/** What the process that is running (or starting) was given. */
|
|
let runningArgs = null;
|
|
/** What the settings ask for now. Written synchronously by every toggle, so
|
|
* a change that lands mid-restart is recorded even though it cannot act
|
|
* yet. */
|
|
let desiredArgs = serverOptions.args.slice();
|
|
/** The tail of the chain. Everything queues onto this and nothing else. */
|
|
let chain = Promise.resolve();
|
|
/** How many spawns have actually been attempted — for the tests, and for
|
|
* anyone wondering whether a burst of toggles cost one restart or four. */
|
|
let spawns = 0;
|
|
/** Whether the door is shut. Set synchronously by `dispose`, and checked
|
|
* both by `want` (nothing new is accepted) and by `reconcile` (whatever was
|
|
* already queued does nothing) — the second check is the one that matters,
|
|
* because a toggle can beat `dispose` into the queue by a tick. */
|
|
let disposed = false;
|
|
|
|
/** Make what is running match what is wanted, or do nothing if it already
|
|
* does. Never called concurrently with itself: it only runs on the chain. */
|
|
async function reconcile() {
|
|
// Queued before the door shut. Whatever it wanted, the answer is now
|
|
// "nothing" — this is what stops a teardown from spawning a server.
|
|
if (disposed) return;
|
|
if (sameArgs(runningArgs, desiredArgs)) return;
|
|
const wanted = desiredArgs.slice();
|
|
if (runningArgs !== null) {
|
|
try {
|
|
await client.stop();
|
|
} catch {
|
|
// A client that is not running cannot be stopped, and one already
|
|
// stopping is being stopped by somebody else. Neither is a reason not
|
|
// to start the process we actually want — and because this runs on the
|
|
// chain, "somebody else" is never another reconcile.
|
|
}
|
|
}
|
|
// Written before the start, because that is when the client reads them.
|
|
serverOptions.args = wanted;
|
|
runningArgs = wanted;
|
|
spawns += 1;
|
|
try {
|
|
await client.start();
|
|
} catch (error) {
|
|
// Nothing is running, so the next toggle must try again whatever it
|
|
// asks for — including the arguments that just failed.
|
|
runningArgs = null;
|
|
throw error;
|
|
}
|
|
onStarted();
|
|
}
|
|
|
|
/** The last transition there will ever be: stop whatever is running, and
|
|
* leave nothing to start. Never rejects — a client that will not stop while
|
|
* the host is going away is not something anyone can act on, and the client
|
|
* says so itself if it has anything to say. */
|
|
async function stopForGood() {
|
|
if (runningArgs === null) return;
|
|
runningArgs = null;
|
|
try {
|
|
await client.stop();
|
|
} catch {
|
|
// See above: teardown, and nothing to be done about it.
|
|
}
|
|
}
|
|
|
|
/** Queue one reconcile, and report a failed start rather than swallowing it.
|
|
* The `catch` is what keeps the chain usable for every later toggle — and
|
|
* what makes `chain` a promise that always resolves. */
|
|
function queue() {
|
|
chain = chain.then(reconcile).catch(onStartFailed);
|
|
return chain;
|
|
}
|
|
|
|
return {
|
|
/** Spawn the server for the first time. The first link of the chain. */
|
|
start: queue,
|
|
/** Ask for these arguments. Returns when the chain has reconciled.
|
|
* A no-op, silently, once `dispose` has been called: nothing may spawn a
|
|
* server after teardown has begun. */
|
|
want(args) {
|
|
if (disposed) return chain;
|
|
desiredArgs = args.slice();
|
|
return queue();
|
|
},
|
|
/**
|
|
* Shut the door and stop the server, as the last link of the same chain.
|
|
*
|
|
* Two halves, and both are needed: the flag makes `want` and any reconcile
|
|
* already queued do nothing, and the final stop goes *through the chain*
|
|
* rather than beside it, so nothing can interleave with it. Await what it
|
|
* returns — that is teardown finished. Idempotent.
|
|
*/
|
|
dispose() {
|
|
if (disposed) return chain;
|
|
disposed = true;
|
|
chain = chain.then(stopForGood);
|
|
return chain;
|
|
},
|
|
/** Everything queued so far, for a test or a `deactivate`. */
|
|
settled: () => chain,
|
|
/** The arguments the running process was spawned with, or `null` if none
|
|
* is running. */
|
|
spawnedWith: () => (runningArgs === null ? null : runningArgs.slice()),
|
|
/** How many spawns have been attempted. */
|
|
spawns: () => spawns,
|
|
};
|
|
}
|
|
|
|
module.exports = { createRestarter, sameArgs };
|