Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | 1x 40x 40x 21x 23x 23x 9x 9x 14x 1x 1x 13x 13x 13x 7x 7x 1x 7x 13x 3x 3x 13x 6x 4x 4x 4x 1x 8x 8x 8x 2x 2x 3x 8x 8x 8x 8x 8x 1x 7x 7x 8x 8x 7x 2x 1x 4x 4x 2x | import { stringify } from '@metamask/kernel-utils';
import type { Logger } from '@metamask/logger';
/** How long a post-failure shutdown may take before the process is killed. */
export const SHUTDOWN_TIMEOUT_MS = 10_000;
/** The subset of `Logger` this handler needs. */
type FailureLogger = Pick<Logger, 'error' | 'info'>;
/**
* Log without letting the transport's own failure escape.
*
* The daemon's transport is `appendFileSync`, so a full disk throws. Every
* caller is on a path where that throw would cost more than the lost line: the
* kernel swallows what the failure handler throws, so a daemon serving a dead
* kernel would stay up, and in the fatal handlers the log sits in front of the
* `process.exit` that is the only thing terminating live vat workers.
*
* @param logger - Where to record the message.
* @param level - The severity to record it at.
* @param message - The message.
* @param data - Additional detail, already stringified.
*/
export function logBestEffort(
logger: FailureLogger,
level: 'error' | 'info',
message: string,
...data: string[]
): void {
try {
logger[level](message, ...data);
} catch {
// No transport left to report the transport with.
}
}
export type RunLoopFailureHandlerOptions = {
logger: FailureLogger;
/** Shut the daemon down. Idempotent; concurrent calls coalesce. */
shutdown: (reason: string) => Promise<void>;
/** Whether there is a daemon to shut down yet. */
isStarted: () => boolean;
/** Whether a shutdown is already under way. */
isShuttingDown: () => boolean;
/** Record the failure so startup can consult it. */
recordFailure: (failure: Error) => void;
/** Remove the pid file. Must complete before the process exits. */
removePidFile: () => void;
setExitCode: (code: number) => void;
exit: (code: number) => void;
timeoutMs?: number;
};
/**
* Build the daemon's run loop failure handler.
*
* Left alone, a dead run loop leaves the daemon answering RPCs for a kernel that
* processes nothing — an outage only a client that reads `runLoop` in
* `getStatus` can spot. Terminate instead, non-zero, so the failure is visible
* and `ocap daemon start` can recover.
*
* Extracted from `daemon-entry` because that module shuts the process down as a
* side effect of being imported, which leaves this logic untestable in place.
*
* @param options - Options bag.
* @param options.logger - Where to record the failure.
* @param options.shutdown - Shut the daemon down. Idempotent; calls coalesce.
* @param options.isStarted - Whether there is a daemon to shut down yet.
* @param options.isShuttingDown - Whether a shutdown is already under way.
* @param options.recordFailure - Record the failure so startup can consult it.
* @param options.removePidFile - Remove the pid file, synchronously.
* @param options.setExitCode - Set the code the process will exit with.
* @param options.exit - Terminate the process now.
* @param options.timeoutMs - How long the shutdown may take before the process
* is killed. Defaults to {@link SHUTDOWN_TIMEOUT_MS}.
* @returns A handler suitable for `makeKernel`'s `onRunLoopFailure`.
*/
export function makeRunLoopFailureHandler({
logger,
shutdown,
isStarted,
isShuttingDown,
recordFailure,
removePidFile,
setExitCode,
exit,
timeoutMs = SHUTDOWN_TIMEOUT_MS,
}: RunLoopFailureHandlerOptions): (failure: Error) => void {
return (failure: Error): void => {
recordFailure(failure);
if (!isStarted()) {
// No daemon to close yet. Startup either unwinds at its own check or
// replays this failure once there is something to shut down.
logBestEffort(
logger,
'error',
'Kernel run loop died before the daemon started.',
stringify(failure, 0),
);
return;
}
if (isShuttingDown()) {
// Expected teardown, not an outage: don't fail a deliberate stop.
logBestEffort(
logger,
'info',
'Kernel run loop stopped during shutdown.',
stringify(failure, 0),
);
return;
}
setExitCode(1);
// `stringify` rather than `failure.stack`, which omits the cause chain. When
// a crank dies and its rollback then fails, the rollback failure is the
// outermost message and the error that actually killed the kernel is only
// reachable through `cause`.
logBestEffort(
logger,
'error',
'Kernel run loop died; shutting down the daemon.',
stringify(failure, 0),
);
// A shutdown that throws would leave the socket gone and the pid file
// removed by `shutdown`'s own cleanup, while live vat workers keep the event
// loop alive — an orphan holding kernel.sqlite that neither interlock can
// see, so the next `ocap daemon start` succeeds and two kernels contend for
// the database. A shutdown that *hangs* never reaches that cleanup, so the
// pid file survives and the pid interlock does still see the orphan — but it
// is an orphan either way. Terminate in both cases. `setExitCode` is not
// enough precisely because those worker handles keep the process running.
const exitNow = (): void => {
try {
removePidFile();
} catch (rmError) {
logBestEffort(
logger,
'error',
'Could not remove the pid file before exiting.',
stringify(rmError, 0),
);
}
exit(1);
};
const killTimer = setTimeout(() => {
logBestEffort(
logger,
'error',
`Shutdown stalled for ${timeoutMs} ms after run loop failure; exiting now.`,
);
exitNow();
}, timeoutMs);
shutdown('run loop failure')
.then(
() => clearTimeout(killTimer),
(shutdownError: unknown) => {
clearTimeout(killTimer);
logBestEffort(
logger,
'error',
'Shutdown after run loop failure failed; exiting now.',
stringify(shutdownError, 0),
);
exitNow();
},
)
// Reached only if a handler above throws, and an unhandled rejection here
// would be reported as the cause of death instead of the run loop.
.catch(() => undefined);
};
}
export type FailedStartupCleanupOptions = {
logger: FailureLogger;
/** Stop the kernel, terminating the vat workers it launched. */
stopKernel: () => Promise<void>;
/** Close the kernel database. */
closeDatabase: () => void;
/** Remove the pid file. Must complete before the process exits. */
removePidFile: () => void;
/** How long the kernel may take to stop. Defaults to {@link SHUTDOWN_TIMEOUT_MS}. */
timeoutMs?: number;
};
/**
* Tear down a kernel that startup could not finish bringing up.
*
* `Kernel.make` launches a worker thread per persisted vat before the run loop
* starts, so by the time startup can fail there are usually live workers, and a
* worker thread keeps the parent's event loop running. Nothing here can be
* fired and forgotten: `stop` is what terminates those workers, and it only
* reaches them after writing the last-active timestamp, so closing the database
* first makes that write throw and the workers survive. The caller is expected
* to terminate the process afterwards.
*
* @param options - Options bag.
* @param options.logger - Where to record what could not be cleaned up.
* @param options.stopKernel - Stop the kernel, terminating its vat workers.
* @param options.closeDatabase - Close the kernel database.
* @param options.removePidFile - Remove the pid file, synchronously.
* @param options.timeoutMs - How long the kernel may take to stop.
*/
export async function cleanUpFailedStartup({
logger,
stopKernel,
closeDatabase,
removePidFile,
timeoutMs = SHUTDOWN_TIMEOUT_MS,
}: FailedStartupCleanupOptions): Promise<void> {
// Bounded: `stop` waits for the current crank first, and a run loop that died
// mid-crank may never end it.
let giveUpTimer: ReturnType<typeof setTimeout> | undefined;
try {
await Promise.race([
stopKernel(),
new Promise<void>((resolve) => {
giveUpTimer = setTimeout(() => {
logBestEffort(
logger,
'error',
`Kernel did not stop within ${timeoutMs} ms during startup cleanup.`,
);
resolve();
}, timeoutMs);
}),
]);
} catch (stopError) {
logBestEffort(
logger,
'error',
'Could not stop the kernel during startup cleanup.',
stringify(stopError, 0),
);
} finally {
clearTimeout(giveUpTimer);
}
try {
closeDatabase();
} catch {
// A `stop` that ran to completion closed the database itself, which is the
// expected case rather than a fault. Nothing to salvage either way: the
// process exits next, which releases the file lock regardless.
}
try {
removePidFile();
} catch (rmError) {
logBestEffort(
logger,
'error',
'Could not remove the pid file during startup cleanup.',
stringify(rmError, 0),
);
}
}
export type DaemonRunLoopWiringOptions = {
logger: FailureLogger;
/** Shut the daemon down. Idempotent; concurrent calls coalesce. */
shutdown: (reason: string) => Promise<void>;
isShuttingDown: () => boolean;
/** Remove the pid file. Must complete before the process exits. */
removePidFile: () => void;
setExitCode: (code: number) => void;
exit: (code: number) => void;
timeoutMs?: number;
};
export type DaemonRunLoopWiring = {
/** Pass to `makeKernel` as `onRunLoopFailure`. */
onRunLoopFailure: (failure: Error) => void;
/** @throws If the run loop has already died, to unwind startup. */
assertSurvivedStartup: () => void;
/** Replays a failure that arrived before there was a daemon to shut down. */
daemonStarted: () => void;
};
/**
* Wire the run loop failure handler to the daemon's lifecycle.
*
* A failure can land before there is a daemon to shut down, so it is held for
* startup's own check and replayed once there is one. Extracted from
* `daemon-entry` because that module shuts the process down as a side effect of
* being imported, which leaves this untestable in place.
*
* @param options - Options bag, forwarded to {@link makeRunLoopFailureHandler}
* apart from the lifecycle state this owns.
* @param options.logger - Where to record the failure.
* @param options.shutdown - Shut the daemon down. Idempotent; calls coalesce.
* @param options.isShuttingDown - Whether a shutdown is already under way.
* @param options.removePidFile - Remove the pid file, synchronously.
* @param options.setExitCode - Set the code the process will exit with.
* @param options.exit - Terminate the process now.
* @param options.timeoutMs - How long the shutdown may take before the process
* is killed.
* @returns The wiring `daemon-entry` hangs off the kernel and its own startup.
*/
export function makeDaemonRunLoopWiring({
logger,
shutdown,
isShuttingDown,
removePidFile,
setExitCode,
exit,
timeoutMs = SHUTDOWN_TIMEOUT_MS,
}: DaemonRunLoopWiringOptions): DaemonRunLoopWiring {
let failure: Error | undefined;
let started = false;
const onRunLoopFailure = makeRunLoopFailureHandler({
logger,
shutdown,
isStarted: () => started,
isShuttingDown,
// Whatever the loop reports next is fallout, including the replay below.
recordFailure: (first) => {
failure ??= first;
},
removePidFile,
setExitCode,
exit,
timeoutMs,
});
return harden({
onRunLoopFailure,
assertSurvivedStartup: () => {
if (failure) {
throw new Error('Kernel run loop died during startup', {
cause: failure,
});
}
},
daemonStarted: () => {
started = true;
if (failure) {
onRunLoopFailure(failure);
}
},
});
}
|