Skip to content
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

fix(cli-repl): only ever run MongoshRepl.onExit once MONGOSH-1943 #2300

Merged
merged 2 commits into from
Dec 13, 2024
Merged
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
17 changes: 13 additions & 4 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type Mutable<T> = {
class MongoshNodeRepl implements EvaluationListener {
_runtimeState: MongoshRuntimeState | null;
closeTrace?: string;
exitPromise?: Promise<never>;
input: Readable;
lineByLineInput: LineByLineInput;
output: Writable;
Expand Down Expand Up @@ -545,6 +546,8 @@ class MongoshNodeRepl implements EvaluationListener {
});

repl.on('exit', () => {
// repl is already closed, no need to close it again via this.close()
if (this._runtimeState) this._runtimeState.repl = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the implications of this being set to null? is this to force a quicker garbage collection / flushing? might be worth leaving a comment, though perhaps I could just be lacking context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, comment is a good idea – also added in 3bcdc52. (The shell can also exit when stdin ends, which would result in us calling this listener here first, before we call .onExit() through some other means)

this.onExit().catch(() => {
/* ... */
});
Expand Down Expand Up @@ -1034,7 +1037,7 @@ class MongoshNodeRepl implements EvaluationListener {
throw new MongoshInternalError(
`mongosh not initialized yet\nCurrent trace: ${
new Error().stack
}\nClose trace: ${this.closeTrace}\n`
}\nClose trace: ${this.closeTrace}`
);
}
return this._runtimeState;
Expand All @@ -1051,7 +1054,11 @@ class MongoshNodeRepl implements EvaluationListener {
if (rs) {
this._runtimeState = null;
this.closeTrace = new Error().stack;
rs.repl?.close();
if (rs.repl) {
// Can be null if the repl already emitted 'exit'
rs.repl.close();
await once(rs.repl, 'exit');
}
await rs.instanceState.close(true);
await new Promise((resolve) => this.output.write('', resolve));
}
Expand All @@ -1063,8 +1070,10 @@ class MongoshNodeRepl implements EvaluationListener {
* @param exitCode The user-specified exit code, if any.
*/
async onExit(exitCode?: number): Promise<never> {
await this.close();
return this.ioProvider.exit(exitCode);
return (this.exitPromise ??= (async () => {
await this.close();
return this.ioProvider.exit(exitCode);
})());
}

/**
Expand Down
Loading