Commit 18a499e
authored
fix(cli-repl): only ever run MongoshRepl.onExit once MONGOSH-1943 (#2300)
Previously, a task in our CI was flaky because it was running `exit\n`
as literal input to the shell, expecting it to reliably make the shell exit.
`exit` as a command leads to the `MongoshRepl.onExit` function called,
which then calls a) `.close()` and, indirectly, `repl.close()` on the
Node.js REPL instance as well b) the `CliRepl.exit` function (and through
that, indirectly, `process.exit()`).
However, closing the Node.js REPL instance makes it flush the history
file to disk as a typically fast but asynchronous operation, and,
after that, emit `exit` on the REPL; that in turn called
`MongoshRepl.onExit` again (i.e. calling `MongoshRepl.onExit` leads
to another call to the same function, just asynchronously with
a short delay).
The flaky test in CI failed because of a combination of issues,
including the fact that `process.exit()` did not necessarily actually
exit the process because of coverage tooling interfering with it
(which in turn happened because it was running with an altered
working directory pointing at a temporary directory where `nyc`
had not created a subdirectory for storing coverage output).
In typical operations, the REPL would flush history quickly, and
calling `.onExit()` again would allow the process to exit
normally (by ways of calling `CliRepl.exit` and `process.exit()`
a second time). However, in the flaky failure case, the history
flushing operation would take long enough that the original
`process.exit()` call (which failed due to nyc) would set the
internal `process._exiting` flag before throwing, which in turn
prevents `process.nextTick()` from scheduling callbacks, and which
then ultimately prevented the REPL from cleaning itself up,
leading to the process timing out instead of exiting.
This commit introduces safeguards against calling `.onExit()` twice,
always returning the same (never-resolving) `Promise` instance,
and explicitly keeps track of whether the REPL was closed or not.1 parent 5405fe4 commit 18a499e
1 file changed
+13
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| 130 | + | |
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
| |||
545 | 546 | | |
546 | 547 | | |
547 | 548 | | |
| 549 | + | |
| 550 | + | |
548 | 551 | | |
549 | 552 | | |
550 | 553 | | |
| |||
1034 | 1037 | | |
1035 | 1038 | | |
1036 | 1039 | | |
1037 | | - | |
| 1040 | + | |
1038 | 1041 | | |
1039 | 1042 | | |
1040 | 1043 | | |
| |||
1051 | 1054 | | |
1052 | 1055 | | |
1053 | 1056 | | |
1054 | | - | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
1055 | 1062 | | |
1056 | 1063 | | |
1057 | 1064 | | |
| |||
1063 | 1070 | | |
1064 | 1071 | | |
1065 | 1072 | | |
1066 | | - | |
1067 | | - | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
1068 | 1077 | | |
1069 | 1078 | | |
1070 | 1079 | | |
| |||
0 commit comments