Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Editor } from '@mongosh/editor';
import { redactSensitiveData } from '@mongosh/history';
import Analytics from 'analytics-node';
import askpassword from 'askpassword';
import { EventEmitter } from 'events';
import { EventEmitter, once } from 'events';
import yaml from 'js-yaml';
import ConnectionString from 'mongodb-connection-string-url';
import semver from 'semver';
Expand Down Expand Up @@ -680,6 +680,20 @@ class CliRepl implements MongoshIOProvider {
if (this.closing) {
return;
}
if (!this.output.destroyed) {
// Wait for output to be fully flushed before exiting.
if (this.output.writableEnded) {
// .end() has been called but not finished; 'close' will be emitted in that case.
// (This should not typically happen in the context of mongosh, but there's also
// no reason not to handle this case properly.)
try {
await once(this.output, 'close');
} catch { /* ignore */ }
} else {
// .end() has not been called; write an empty chunk and wait for it to be fully written.
await new Promise(resolve => this.output.write('', resolve));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we write this empty chunk? To verify it is still writable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As the comment says, the goal here is to wait for the chunk to be fully written; because when the new chunk has been written, that implies that all previously pending data has also been written to the underlying stream.

}
}
this.closing = true;
const analytics = this.segmentAnalytics;
let flushError: string | null = null;
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-repl/src/tls-certificate-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export function getTlsCertificateSelector(
}

declare global {
// eslint-disable-next-line camelcase
const __non_webpack_require__: undefined | typeof require;
}

function getCertificateExporter(): TlsCertificateExporter | undefined {
if (process.env.TEST_OS_EXPORT_CERTIFICATE_AND_KEY_PATH) {
// eslint-disable-next-line camelcase
if (typeof __non_webpack_require__ === 'function') {
return __non_webpack_require__(process.env.TEST_OS_EXPORT_CERTIFICATE_AND_KEY_PATH);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ describe('e2e', function() {
expect(await shell.executeLine('[crypto.getFips()]')).to.include('[ 1 ]');
}
});
it('prints full output even when that output is buffered', async() => {
shell = TestShell.start({
args: ['--nodb', '--quiet', '--eval', 'EJSON.stringify([...Array(100_000).keys()].map(i=>({i})),null,2)'],
consumeStdio: false
});
let buffer = '';
let hasWaited = false;
// Start reading data, wait a bit, then read the rest
for await (const chunk of shell.process.stdout.setEncoding('utf8')) {
buffer += chunk;
if (buffer.includes('"i": 100') && !hasWaited) {
// This delay is relevant for reproducing this bug; it gives
// the mongosh process time to exit before all output has been printed.
await new Promise(resolve => setTimeout(resolve, 1000));
hasWaited = true;
}
}
expect(buffer).to.include('"i": 99999');
});
});

describe('set db', () => {
Expand Down
23 changes: 13 additions & 10 deletions packages/cli-repl/test/test-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class TestShell {
removeSigintListeners?: boolean;
cwd?: string;
forceTerminal?: boolean;
consumeStdio?: boolean;
} = { args: [] }): TestShell {
let shellProcess: ChildProcess;

Expand Down Expand Up @@ -64,7 +65,7 @@ export class TestShell {
});
}

const shell = new TestShell(shellProcess);
const shell = new TestShell(shellProcess, options.consumeStdio);
TestShell._openShells.push(shell);

return shell;
Expand Down Expand Up @@ -95,20 +96,22 @@ export class TestShell {
private _rawOutput: string;
private _onClose: Promise<number>;

constructor(shellProcess: ChildProcess) {
constructor(shellProcess: ChildProcess, consumeStdio = true) {
this._process = shellProcess;
this._output = '';
this._rawOutput = '';

shellProcess.stdout.setEncoding('utf8').on('data', (chunk) => {
this._output += stripAnsi(chunk);
this._rawOutput += chunk;
});
if (consumeStdio) {
shellProcess.stdout.setEncoding('utf8').on('data', (chunk) => {
this._output += stripAnsi(chunk);
this._rawOutput += chunk;
});

shellProcess.stderr.setEncoding('utf8').on('data', (chunk) => {
this._output += stripAnsi(chunk);
this._rawOutput += chunk;
});
shellProcess.stderr.setEncoding('utf8').on('data', (chunk) => {
this._output += stripAnsi(chunk);
this._rawOutput += chunk;
});
}

this._onClose = (async() => {
const [ code ] = await once(shellProcess, 'close');
Expand Down