Skip to content

Commit c361857

Browse files
authored
[wasm][nodejs] Ensure that stdout/stderr have been flushed out before exiting (dotnet#70416)
When the results xml is large, and we are writing the base64 representation in one line, `node` can exit before all the output gets flushed out. This results in xharness getting an incomplete `STARTRESULTXML <len> <base64> ... ` with missing `ENDRESULTXML`, thus no `testResults.xml` is generated. This can be seen in the case of `Microsoft.Extensions.Primitives.Tests` which has xml ~140KB, and `System.Memory.Tests` which has a xml ~13MB. So, wait for the two streams to be flushed out, with a timeout of 3secs. - use the `drain` event only if `stream.write('')` returns `false` * Address review feedback @kg, @maraf
1 parent ed1595e commit c361857

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/mono/wasm/test-main.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,30 @@ function set_exit_code(exit_code, reason) {
105105
}
106106

107107
} else if (App && App.INTERNAL) {
108-
App.INTERNAL.mono_wasm_exit(exit_code);
108+
if (is_node) {
109+
let _flush = function(_stream) {
110+
return new Promise((resolve, reject) => {
111+
if (!_stream.write('')) {
112+
_stream.on('drain', () => resolve());
113+
setTimeout(reject, 3000);
114+
} else {
115+
resolve();
116+
}
117+
});
118+
};
119+
let stderrFlushed = _flush(process.stderr);
120+
let stdoutFlushed = _flush(process.stdout);
121+
122+
Promise.all([ stdoutFlushed, stderrFlushed ])
123+
.then(
124+
() => App.INTERNAL.mono_wasm_exit(exit_code),
125+
reason => {
126+
console.error(`flushing std* streams failed: ${reason}`);
127+
App.INTERNAL.mono_wasm_exit(123);
128+
});
129+
} else {
130+
App.INTERNAL.mono_wasm_exit(exit_code);
131+
}
109132
}
110133
}
111134

0 commit comments

Comments
 (0)