Finalize log stream before exiting process

Reviewed By: antonk52

Differential Revision: D51229230

fbshipit-source-id: 0e7f657a170eb8602ade9abf1db1976c5b51dc3f
This commit is contained in:
Andrey Goncharov
2023-11-11 08:21:12 -08:00
committed by Facebook GitHub Bot
parent 0cbd640e5c
commit a400eb2872
8 changed files with 87 additions and 13 deletions

View File

@@ -30,6 +30,7 @@ import {
startFlipperServer,
startServer,
tracker,
processExit,
} from 'flipper-server-core';
import {addLogTailer, isProduction, isTest, LoggerFormat} from 'flipper-common';
import exitHook from 'exit-hook';
@@ -265,7 +266,7 @@ async function start() {
console.error(
'[flipper-server] state changed to error, process will exit.',
);
process.exit(1);
processExit(1);
}
});
}
@@ -335,7 +336,7 @@ process.on('uncaughtException', (error) => {
error,
);
reportBrowserConnection(false);
process.exit(1);
processExit(1);
});
process.on('unhandledRejection', (reason, promise) => {
@@ -347,8 +348,15 @@ process.on('unhandledRejection', (reason, promise) => {
);
});
start().catch((e) => {
console.error(chalk.red('Server startup error: '), e);
reportBrowserConnection(false);
process.exit(1);
});
// Node.js process never waits for all promises to settle and exits as soon as there is not pending timers or open sockets or tasks in teh macroqueue
const runtimeTimeout = setTimeout(() => {}, Number.MAX_SAFE_INTEGER);
// eslint-disable-next-line promise/catch-or-return
start()
.catch((e) => {
console.error(chalk.red('Server startup error: '), e);
reportBrowserConnection(false);
return processExit(1);
})
.finally(() => {
clearTimeout(runtimeTimeout);
});

View File

@@ -21,7 +21,10 @@ import fsRotator from 'file-stream-rotator';
import {ensureFile} from 'fs-extra';
import {access} from 'fs/promises';
import {constants} from 'fs';
import {initializeLogger as initLogger} from 'flipper-server-core';
import {
initializeLogger as initLogger,
setProcessExitRoutine,
} from 'flipper-server-core';
export const loggerOutputFile = 'flipper-server-log.out';
@@ -64,4 +67,14 @@ export async function initializeLogger(
logStream?.write(`${name}: \n${stack}\n`);
}
});
const finalizeLogger = async () => {
const logStreamToEnd = logStream;
// Prevent future writes
logStream = undefined;
await new Promise<void>((resolve) => {
logStreamToEnd?.end(resolve);
});
};
setProcessExitRoutine(finalizeLogger);
}