Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/kap-server/src/services/pinoLoggerService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { pino, type Logger, type LoggerOptions } from 'pino';
import { pino, type DestinationStream, type Logger, type LoggerOptions } from 'pino';

export type ServerLogger = Logger;

export type ServerLogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent';

export interface CreateLoggerOptions {
level: ServerLogLevel;
stream?: DestinationStream;
}

export function createServerLogger(opts: CreateLoggerOptions): ServerLogger {
Expand All @@ -14,5 +15,5 @@ export function createServerLogger(opts: CreateLoggerOptions): ServerLogger {
base: { name: 'kimi-server-v2' },
timestamp: pino.stdTimeFunctions.isoTime,
};
return pino(base);
return opts.stream === undefined ? pino(base) : pino(base, opts.stream);
}
3 changes: 1 addition & 2 deletions packages/kap-server/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ export async function startServer(opts: ServerStartOptions): Promise<RunningServ
);
};
const onUncaughtException = (err: unknown): void => {
logger.fatal(
logger.error(
{ err: err instanceof Error ? err : new Error(String(err)) },
'uncaughtException',
);
process.exit(1);
};
const authFailureLimiter =
exposureClass === 'loopback' ? undefined : createAuthFailureLimiter({ logger });
Expand Down
47 changes: 39 additions & 8 deletions packages/kap-server/test/boot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { createServer, type Server } from 'node:net';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Writable } from 'node:stream';

import { pino } from 'pino';
import { afterEach, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -233,26 +234,56 @@ describe('server-v2 boot', () => {
expect(await listLiveServerInstances(home)).toEqual([]);
});

it('installs process-level rejection handlers while running and removes them on close', async () => {
it('logs process-level exceptions without exiting and removes the handlers on close', async () => {
home = await mkdtemp(join(tmpdir(), 'kimi-server-v2-'));
const rejectionBefore = process.listenerCount('unhandledRejection');
const exceptionBefore = process.listenerCount('uncaughtException');
const lines: string[] = [];
const stream = new Writable({
write(chunk, _encoding, callback) {
lines.push(String(chunk));
callback();
},
});
const rejectionBefore = process.listeners('unhandledRejection');
const exceptionBefore = process.listeners('uncaughtException');
server = await startServer({
hostIdentity: TEST_HOST_IDENTITY,
host: '127.0.0.1',
port: 0,
homeDir: home,
logLevel: 'silent',
logger: pino({ level: 'error' }, stream),
});

expect(process.listenerCount('unhandledRejection')).toBe(rejectionBefore + 1);
expect(process.listenerCount('uncaughtException')).toBe(exceptionBefore + 1);
expect(process.listenerCount('unhandledRejection')).toBe(rejectionBefore.length + 1);
expect(process.listenerCount('uncaughtException')).toBe(exceptionBefore.length + 1);

const onUncaughtException = process
.listeners('uncaughtException')
.find((listener) => !exceptionBefore.includes(listener)) as
| ((error: Error) => void)
| undefined;
const onUnhandledRejection = process
.listeners('unhandledRejection')
.find((listener) => !rejectionBefore.includes(listener)) as
| ((reason: unknown) => void)
| undefined;
expect(onUncaughtException).toBeDefined();
expect(onUnhandledRejection).toBeDefined();

onUncaughtException?.(new Error('synthetic uncaught'));
onUnhandledRejection?.(new Error('synthetic rejection'));

const output = lines.join('');
expect(output).toContain('"msg":"uncaughtException"');
expect(output).toContain('"msg":"unhandledRejection"');

const healthz = await fetch(`http://127.0.0.1:${server.port}/api/v1/healthz`);
expect(healthz.status).toBe(200);

await server.close();
server = undefined;

expect(process.listenerCount('unhandledRejection')).toBe(rejectionBefore);
expect(process.listenerCount('uncaughtException')).toBe(exceptionBefore);
expect(process.listenerCount('unhandledRejection')).toBe(rejectionBefore.length);
expect(process.listenerCount('uncaughtException')).toBe(exceptionBefore.length);
});

it('does not leave process handlers installed when startup fails', async () => {
Expand Down
Loading