Skip to content

Commit 0f2a2b5

Browse files
edisigcidustinbyrne
authored andcommitted
fix: Crash in emit attempt when stream closed
1 parent fef98fa commit 0f2a2b5

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Recording.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { parameter } from "./parameter";
1515
import type { FunctionInfo } from "./registry";
1616
import compactObject from "./util/compactObject";
1717
import { getTime, getTimeInMilliseconds } from "./util/getTime";
18+
import { shouldRecord } from "./recorderControl";
1819
import { warn } from "./message";
1920

2021
export default class Recording {
@@ -239,10 +240,24 @@ export default class Recording {
239240
private bufferedEvents = new Map<number, AppMap.Event>();
240241

241242
public emit(event: AppMap.Event) {
243+
// Check here if we should record instead of requiring each
244+
// possible hook to check it.
245+
// This is also checked in recorder.record() to prevent
246+
// unnecessary event object creation. Checking this inside hooks,
247+
// (http, sqlite, pg, mysql, ...) will save some CPU cycles but
248+
// will complicate their code.
249+
if (!shouldRecord()) return;
250+
242251
if (!this.running) {
243252
warn("event emitted while recording not running");
244253
return;
245254
}
255+
256+
if (this.stream == undefined) {
257+
warn("Event emitted while stream is closed");
258+
return;
259+
}
260+
246261
// If the current buffer is alive more than allowed pass its events
247262
// to the stream and clear it recursively.
248263
if (Recording.buffering && !Recording.buffer.disposed && config().asyncTrackingTimeout != 0) {

src/__tests__/Recording.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ afterEach(() => {
8484
resetObjectIds();
8585
});
8686

87+
describe("Recording", () => {
88+
it("Does not throw when emit is called with stream closed", () => {
89+
const recording = new Recording("tests", "test", "test");
90+
const funInfo = createTestFn("testFun");
91+
const call = recording.functionCall(funInfo, undefined, []);
92+
recording.finish();
93+
recording.functionReturn(call.id, "result", undefined);
94+
});
95+
});
96+
8797
jest.mock("../AppMapStream");
8898

8999
// DO NOT REMOVE!

0 commit comments

Comments
 (0)