Skip to content

Commit

Permalink
fix: Do not close JS stream reader if closed
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Apr 19, 2021
1 parent 3207821 commit 4fa0db3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3540,6 +3540,7 @@ var Uno;
}
static async readAsync(streamId, targetArrayPointer, offset, count, position) {
var streamReader;
var readerNeedsRelease = true;
try {
const instance = NativeFileReadStream._streamMap.get(streamId);
var totalRead = 0;
Expand All @@ -3553,10 +3554,23 @@ var Uno;
totalRead += chunk.value.length;
chunk = await streamReader.read();
}
// If this is the end of stream, it closed itself
readerNeedsRelease = !chunk.done;
return totalRead.toString();
}
finally {
if (streamReader) {
// Reader must be released only if the underlying stream has not already closed it.
// Otherwise the release operation sets a new Promise.reject as reader.closed which
// raises silent but observable exception in Chromium-based browsers.
if (streamReader && readerNeedsRelease) {
// Silently handling TypeError exceptions on closed event as the releaseLock()
// raises one in case of a successful close.
streamReader.closed.catch(reason => {
if (!(reason instanceof TypeError)) {
throw reason;
}
});
streamReader.cancel();
streamReader.releaseLock();
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/Uno.UI/ts/Windows/Storage/Streams/NativeFileReadStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

public static async readAsync(streamId: string, targetArrayPointer: number, offset: number, count: number, position: number): Promise<string> {
var streamReader: ReadableStreamDefaultReader;
var readerNeedsRelease = true;
try {
const instance = NativeFileReadStream._streamMap.get(streamId);

var totalRead = 0;
var stream = await instance._file.slice(position, position + count).stream();
streamReader = stream.getReader();

var chunk = await streamReader.read();
while (!chunk.done && chunk.value) {
for (var i = 0; i < chunk.value.length; i++) {
Expand All @@ -36,10 +38,26 @@
chunk = await streamReader.read();
}

// If this is the end of stream, it closed itself
readerNeedsRelease = !chunk.done;

return totalRead.toString();
}
finally {
if (streamReader) {
// Reader must be released only if the underlying stream has not already closed it.
// Otherwise the release operation sets a new Promise.reject as reader.closed which
// raises silent but observable exception in Chromium-based browsers.
if (streamReader && readerNeedsRelease) {

// Silently handling TypeError exceptions on closed event as the releaseLock()
// raises one in case of a successful close.
streamReader.closed.catch(reason => {
if (!(reason instanceof TypeError)) {
throw reason;
}
});

streamReader.cancel();
streamReader.releaseLock();
}
}
Expand Down

0 comments on commit 4fa0db3

Please sign in to comment.