Skip to content

Commit

Permalink
fix: Crash in emit attempt when stream closed
Browse files Browse the repository at this point in the history
  • Loading branch information
zermelo-wisen committed Aug 6, 2024
1 parent a8b9fbf commit 815bb3a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getDefaultMetadata } from "./metadata";
import type { FunctionInfo } from "./registry";
import compactObject from "./util/compactObject";
import { shouldRecord } from "./recorderControl";
import { warn } from "./message";

export default class Recording {
constructor(type: AppMap.RecorderType, recorder: string, ...names: string[]) {
Expand Down Expand Up @@ -180,7 +181,11 @@ export default class Recording {
// (http, sqlite, pg, mysql, ...) will save some CPU cycles but
// will complicate their code.
if (!shouldRecord()) return;
assert(this.stream);

if (this.stream == undefined) {
warn("Event emitted while stream is closed");
return;
}
this.stream.emit(event);
}

Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/Recording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ describe(Recording.prototype.fixup, () => {
});
});

describe("Recording", () => {
it("Does not throw when emit is called with stream closed", () => {
const recording = new Recording("tests", "test", "test");
const funInfo = createTestFn("testFun");
const call = recording.functionCall(funInfo, undefined, []);
recording.finish();
recording.functionReturn(call.id, "result", undefined);
});
});

jest.mock("../AppMapStream");

0 comments on commit 815bb3a

Please sign in to comment.