Skip to content

Commit

Permalink
fix(core): dont close client on promise rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Oct 8, 2020
1 parent d2aa22f commit 37f1169
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion client/index.ts
Expand Up @@ -76,7 +76,7 @@ export function SecretAgentClientGenerator(
process.on('unhandledRejection', async (error: Error) => {
// keep core node behavior intact
process.stderr.write(`${error.stack}\n`);
await SecretAgent.shutdown(error);
await SecretAgent.recordUnhandledError(error);
});
}

Expand Down
6 changes: 3 additions & 3 deletions client/lib/CoreClient.ts
Expand Up @@ -42,11 +42,11 @@ export default class CoreClient {
return coreTabs;
}

public async shutdown(error?: Error): Promise<void> {
public async shutdown(fatalError?: Error): Promise<void> {
const tabIds = Object.keys(this.tabsById);
this.commandQueue.clearPending();
if (tabIds.length || error) {
await this.commandQueue.run('disconnect', tabIds, error);
if (tabIds.length || fatalError) {
await this.commandQueue.run('disconnect', tabIds, fatalError);
}
for (const tabId of tabIds) {
delete this.tabsById[tabId];
Expand Down
21 changes: 13 additions & 8 deletions core/index.ts
Expand Up @@ -290,8 +290,8 @@ export default class Core implements ICore {
});
}

public static async disconnect(tabIds?: string[], clientError?: Error) {
if (clientError) this.logUnhandledError(clientError);
public static async disconnect(tabIds?: string[], fatalError?: Error) {
if (fatalError) this.logUnhandledError(fatalError, true);
return this.startQueue.run(async () => {
const promises: Promise<void>[] = [];
for (const key of tabIds ?? Object.keys(Core.byTabId)) {
Expand All @@ -310,13 +310,17 @@ export default class Core implements ICore {
});
}

public static logUnhandledError(clientError: Error) {
log.error('UnhandledError', { clientError, sessionId: null });
public static logUnhandledError(clientError: Error, fatalError = false) {
if (fatalError) {
log.error('UnhandledError(fatal)', { clientError, sessionId: null });
} else {
log.error('UnhandledErrorOrRejection', { clientError, sessionId: null });
}
}

public static async shutdown(fatalError?: Error, force = false) {
public static async shutdown(force = false) {
// runs own queue, don't put inside this loop
await Core.disconnect(null, fatalError);
await Core.disconnect();
return this.startQueue.run(async () => {
log.info('Core.shutdown');
clearTimeout(Core.autoShutdownTimer);
Expand All @@ -338,12 +342,13 @@ export default class Core implements ICore {

public static registerExceptionHandlers() {
process.on('uncaughtException', async (error: Error) => {
await Core.shutdown(error);
await Core.logUnhandledError(error, true);
await Core.shutdown();
process.exit(1);
});

process.on('unhandledRejection', async (error: Error) => {
await Core.shutdown(error);
await Core.logUnhandledError(error, false);
});
}

Expand Down
2 changes: 1 addition & 1 deletion session-state/test/api.test.ts
Expand Up @@ -99,7 +99,7 @@ describe('basic Replay API tests', () => {
expect(paintEvents[0]).toHaveLength(1);
expect(paintEvents[1]).toHaveLength(13);

await Core.shutdown(null, true);
await Core.shutdown(true);
api.destroy();
}, 20e3);
});
2 changes: 1 addition & 1 deletion testing/helpers.ts
Expand Up @@ -328,7 +328,7 @@ export async function afterEach() {

export async function afterAll() {
await closeAll(true);
await Core.shutdown(null, true);
await Core.shutdown(true);
}

async function closeAll(isFinal = false) {
Expand Down

0 comments on commit 37f1169

Please sign in to comment.