-
Notifications
You must be signed in to change notification settings - Fork 160
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
ReadableStream: releaseLock cannot be called if read() resolves #1000
Comments
I don't believe this is a bug in the spec, but rather a bug in your code. In your One way to fix this is by checking if public async _read() {
// Should start pushing data into the queue
// Read data from the underlying Web-API-readable-stream
if (this.released) {
return;
}
let readPromise = this.reader.read();
this.pendingRead = readPromise;
try {
const data = await readPromise;
if (data.done || this.released) {
this.push(null);
} else {
this.bytesRead += data.value.length;
this.push(data.value);
}
} finally {
if (readPromise === this.pendingRead) { // <<<
delete this.pendingRead;
}
}
} If that still doesn't fix it, I could try to dig a bit deeper into your code. If you have a small reproduction case for this issue, that'd be great. 😁 |
Thank a lot @MattiasBuelens for your explanation. I think you are absolutely right, it is caused on my end, by a second read. But I think the issue is even simpler to solve. It's a node stream calling Web-API ReadableStream-read. The _read() (Node stream implementation) will not call another _read until data is pushed. The second read is result "in the same call" as the push. To it looks like simply deleting the cached |
Or maybe I should say, I cannot find any way cancelling a ReadableStream without running into a synchronization issues.
To cancel a ReadableStream the reader has to release the lock on the stream.
Releasing the Reader requires to call releaseLock().
This call will throw an error of there is an outstanding read.
Let's assume the read is still pending here....
At the same time, I would like to get rid of the stream.
Aiming to call releaseLock(), when there is no ongoing read I try to used the promise returned by the read.
It looks like, the internal mechanism is resolved after the client side of the promise is resolved.
The issue can be demonstrated on Google Chrome, I am not sure that reflects this reference implementation. I use a Readable stream return by fetch.
The text was updated successfully, but these errors were encountered: