Open JAD3N opened 2 months ago
Your original code threw an error for me:
Warning: Detected unsettled top-level await at file:///index.mjs:7
But with some slight modifications, I was able to reproduce:
import { TransformStream } from "node:stream/web";
const { writable } = new TransformStream();
(async () => {
try {
let writer = writable.getWriter();
await writer.write(" ");
writer.releaseLock();
writer = writable.getWriter();
await writer.close();
writer.releaseLock();
console.log("Done!");
} catch (err) {
console.error("Error:", err);
}
})();
$ node repro.async.mjs && echo "Exit code: $?"
Exit code: 0
Compared to other streams:
const stream = new WritableStream();
try {
let writer = stream.getWriter();
await writer.write(" ");
writer.releaseLock();
writer = stream.getWriter();
await writer.close();
writer.releaseLock();
console.log("Done!");
} catch (err) {
console.error("Error:", err);
}
$ node repro.async.mjs && echo "Exit code: $?"
Done!
Exit code: 0
IIUC The issue has to do with await writer.write(" ");
:
import { TransformStream } from "node:stream/web";
await (new TransformStream()).writable.getWriter().write(" ");
Warning: Detected unsettled top-level await at file:///repro.mjs:2
Your original code threw an error for me:
Warning: Detected unsettled top-level await at file:///index.mjs:7
But with some slight modifications, I was able to reproduce:
import { TransformStream } from "node:stream/web"; const { writable } = new TransformStream(); (async () => { try { let writer = writable.getWriter(); await writer.write(" "); writer.releaseLock(); writer = writable.getWriter(); await writer.close(); writer.releaseLock(); console.log("Done!"); } catch (err) { console.error("Error:", err); } })();
$ node repro.async.mjs && echo "Exit code: $?" Exit code: 0
Compared to other streams:
const stream = new WritableStream(); try { let writer = stream.getWriter(); await writer.write(" "); writer.releaseLock(); writer = stream.getWriter(); await writer.close(); writer.releaseLock(); console.log("Done!"); } catch (err) { console.error("Error:", err); }
$ node repro.async.mjs && echo "Exit code: $?" Done! Exit code: 0
With your example I also experience a crash and no output in my terminal.
Edit: The exit code also shows as 0
IIUC The issue has to do with
await writer.write(" ");
:import { TransformStream } from "node:stream/web"; await (new TransformStream()).writable.getWriter().write(" ");
Warning: Detected unsettled top-level await at file:///repro.mjs:2
Yes, it's the write blocking entire thread, not the close line
```javascript import { TransformStream } from "node:stream/web"; const { writable } = new TransformStream(); (async () => { try { let writer = writable.getWriter(); await writer.write(" "); console.log("Done!"); } catch (err) { console.error("Error:", err); } })(); ``` ```console ./out/Release/node repros/54453.js && echo "Exit code $?" Exit code 0 ```
```javascript import { TransformStream } from "node:stream/web"; const { writable } = new TransformStream(); (async () => { try { let writer = writable.getWriter(); // await writer.write(" "); console.log("Done!"); } catch (err) { console.error("Error:", err); } })(); ``` ```console ./out/Release/node repros/54453.js && echo "Exit code $?" Done! Exit code 0 ```
I see what's going on here:
you need to read to resolve write. e.g.:
import {
TransformStream,
} from 'node:stream/web';
const transform = new TransformStream();
await Promise.all([
transform.writable.getWriter().write('A'),
transform.readable.getReader().read(),
]);
console.log("Done!");
./out/Release/node repros/54453.js && echo "Exit code $?"
Done!
Exit code 0
Without read:
import {
TransformStream,
} from 'node:stream/web';
const transform = new TransformStream();
await Promise.all([
transform.writable.getWriter().write('A'),
// transform.readable.getReader().read(),
]);
console.log("Done!");
./out/Release/node repros/54453.js && echo "Exit code $?"
Exit code 0
edit: Just tested it on chrome 127.0.6533.120, the behavior is the same
Just tested it on chrome 127.0.6533.120, the behavior is the same
@nodejs/v8
Version
v20.16.0
Platform
Subsystem
No response
What steps will reproduce the bug?
I ran this code and my Node.js process crashed with no output:
This causes a crash:
This doesn't:
How often does it reproduce? Is there a required condition?
If I close the
WritableStream
on theTransformStream
example without awaiting theclose()
promise then the process will proceed to logDone!
.What is the expected behavior? Why is that the expected behavior?
I figured the stream would close or throw some kind of error if it cannot.
What do you see instead?
The Node.js process crashes without any thrown errors and doesn't trigger the
beforeExit
orexit
process events.Additional information
I've simplified my code into the reproducible above, that's why I create multiple writers.
No response