Skip to content

Fixes #42 QueueTimer keeps Node process from terminating gracefully #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2023
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
3 changes: 3 additions & 0 deletions packages/core/src/ExceptionlessClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EventContext } from "./models/EventContext.js";
import { EventPluginContext } from "./plugins/EventPluginContext.js";
import { EventPluginManager } from "./plugins/EventPluginManager.js";
import { PluginContext } from "./plugins/PluginContext.js";
import { allowProcessToExitWithoutWaitingForTimerOrInterval } from "./Utils.js";

export class ExceptionlessClient {
private _intervalId: ReturnType<typeof setInterval> | undefined;
Expand Down Expand Up @@ -92,9 +93,11 @@ export class ExceptionlessClient {
void SettingsManager.updateSettings(this.config);
if (initialDelay < interval) {
this._timeoutId = setTimeout(updateSettings, initialDelay);
allowProcessToExitWithoutWaitingForTimerOrInterval(this._timeoutId);
}

this._intervalId = setInterval(updateSettings, interval);
allowProcessToExitWithoutWaitingForTimerOrInterval(this._intervalId);
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,13 @@ export function toError(errorOrMessage: unknown, defaultMessage = "Unknown Error

return new Error(JSON.stringify(errorOrMessage) || defaultMessage);
}


/**
* Unrefs a timeout or interval. When called, the active Timeout object will not require the Node.js event loop to remain active
*/
export function allowProcessToExitWithoutWaitingForTimerOrInterval(timeoutOrIntervalId: ReturnType<typeof setTimeout> | ReturnType<typeof setInterval> | undefined): void {
if (typeof timeoutOrIntervalId === "object" && "unref" in timeoutOrIntervalId) {
(timeoutOrIntervalId as { unref: () => ReturnType<typeof setTimeout> | ReturnType<typeof setInterval> }).unref();
}
}
3 changes: 2 additions & 1 deletion packages/core/src/plugins/default/DuplicateCheckerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InnerErrorInfo } from "../../models/data/ErrorInfo.js";
import { KnownEventDataKeys } from "../../models/Event.js";
import { getHashCode } from "../../Utils.js";
import { allowProcessToExitWithoutWaitingForTimerOrInterval, getHashCode } from "../../Utils.js";
import { EventPluginContext } from "../EventPluginContext.js";
import { IEventPlugin } from "../IEventPlugin.js";

Expand All @@ -25,6 +25,7 @@ export class DuplicateCheckerPlugin implements IEventPlugin {
public startup(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = setInterval(() => void this.submitEvents(), this._interval);
allowProcessToExitWithoutWaitingForTimerOrInterval(this._intervalId);
return Promise.resolve();
}

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/plugins/default/HeartbeatPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { KnownEventDataKeys } from "../../models/Event.js";
import { allowProcessToExitWithoutWaitingForTimerOrInterval } from "../../Utils.js";
import { EventPluginContext } from "../EventPluginContext.js";
import { IEventPlugin } from "../IEventPlugin.js";

Expand Down Expand Up @@ -49,6 +50,8 @@ export class HeartbeatPlugin implements IEventPlugin {
() => void context.client.submitSessionHeartbeat(<string>config.currentSessionIdentifier),
this._interval
);

allowProcessToExitWithoutWaitingForTimerOrInterval(this._intervalId);
}

return Promise.resolve();
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/queue/DefaultEventQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ILog } from "../logging/ILog.js";
import { Event } from "../models/Event.js";
import { IEventQueue } from "../queue/IEventQueue.js";
import { Response } from "../submission/Response.js";
import { allowProcessToExitWithoutWaitingForTimerOrInterval } from "../Utils.js";

interface EventQueueItem {
file: string,
Expand Down Expand Up @@ -133,6 +134,7 @@ export class DefaultEventQueue implements IEventQueue {
if (!this._queueIntervalId) {
// TODO: Fix awaiting promise.
this._queueIntervalId = setInterval(() => void this.onProcessQueue(), 10000);
allowProcessToExitWithoutWaitingForTimerOrInterval(this._queueIntervalId);
}

return Promise.resolve();
Expand Down
9 changes: 9 additions & 0 deletions packages/node/src/plugins/NodeLifeCyclePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ export class NodeLifeCyclePlugin implements IEventPlugin {

this._client = context.client;

let processingBeforeExit: boolean = false;
process.on("beforeExit", (code: number) => {
// NOTE: We need to check if we are already processing a beforeExit event
// as async work will cause the runtime to call this handler again.
if (processingBeforeExit) {
return;
}

processingBeforeExit = true;

const message = this.getExitCodeReason(code);
if (message) {
void this._client?.submitLog("beforeExit", message, "Error");
Expand Down