Skip to content

Commit aa8cc66

Browse files
authored
fix: MaxListenersExceededWarning memory leak (#782)
1 parent c6963c9 commit aa8cc66

4 files changed

Lines changed: 28 additions & 19 deletions

File tree

apps/twig/src/main/lib/typed-event-emitter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { EventEmitter, on } from "node:events";
22

33
export class TypedEventEmitter<TEvents> extends EventEmitter {
4+
constructor() {
5+
super();
6+
this.setMaxListeners(50);
7+
}
8+
49
emit<K extends keyof TEvents & string>(
510
event: K,
611
payload: TEvents[K],

apps/twig/src/main/services/agent/service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { MAIN_TOKENS } from "../../di/tokens.js";
2626
import { logger } from "../../lib/logger.js";
2727
import { TypedEventEmitter } from "../../lib/typed-event-emitter.js";
2828
import type { ProcessTrackingService } from "../process-tracking/service.js";
29-
import { SleepService } from "../sleep/service.js";
29+
import type { SleepService } from "../sleep/service.js";
3030
import {
3131
AgentServiceEvent,
3232
type AgentServiceEvents,
@@ -873,6 +873,14 @@ For git operations while detached:
873873
sessionCount: sessionIds.length,
874874
});
875875

876+
for (const session of this.sessions.values()) {
877+
try {
878+
await session.agent.flushAllLogs();
879+
} catch {
880+
log.debug("Failed to flush session logs during shutdown");
881+
}
882+
}
883+
876884
for (const taskRunId of sessionIds) {
877885
await this.cleanupSession(taskRunId);
878886
}

packages/agent/src/agent.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ export class Agent {
9999
});
100100
}
101101

102+
async flushAllLogs(): Promise<void> {
103+
await this.sessionLogWriter?.flushAll();
104+
}
105+
102106
async cleanup(): Promise<void> {
107+
if (this.sessionLogWriter && this.taskRunId) {
108+
await this.sessionLogWriter.flush(this.taskRunId);
109+
}
103110
await this.acpConnection?.cleanup();
104111
}
105112
}

packages/agent/src/session-log-writer.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,14 @@ export class SessionLogWriter {
1818
this.posthogAPI = posthogAPI;
1919
this.logger =
2020
logger ?? new Logger({ debug: false, prefix: "[SessionLogWriter]" });
21+
}
2122

22-
const flushAllAndExit = async () => {
23-
const flushPromises: Promise<void>[] = [];
24-
for (const sessionId of this.configs.keys()) {
25-
flushPromises.push(this.flush(sessionId));
26-
}
27-
await Promise.all(flushPromises);
28-
process.exit(0);
29-
};
30-
31-
process.on("beforeExit", () => {
32-
flushAllAndExit().catch((e) => this.logger.error("Flush failed:", e));
33-
});
34-
process.on("SIGINT", () => {
35-
flushAllAndExit().catch((e) => this.logger.error("Flush failed:", e));
36-
});
37-
process.on("SIGTERM", () => {
38-
flushAllAndExit().catch((e) => this.logger.error("Flush failed:", e));
39-
});
23+
async flushAll(): Promise<void> {
24+
const flushPromises: Promise<void>[] = [];
25+
for (const sessionId of this.configs.keys()) {
26+
flushPromises.push(this.flush(sessionId));
27+
}
28+
await Promise.all(flushPromises);
4029
}
4130

4231
register(sessionId: string, config: SessionLogConfig): void {

0 commit comments

Comments
 (0)