Skip to content
Merged
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,18 @@ async function writeFile(

if (data instanceof ReadableStream) {
const file = await open(path, options)
for await (const chunk of data) {
await file.write(chunk)
const reader = data.getReader()

try {
while (true) {
const { done, value } = await reader.read()
if (done) break
await file.write(value)
}
} finally {
reader.releaseLock()
}

await file.close()
Copy link
Member

@amrbashir amrbashir Mar 19, 2025

Choose a reason for hiding this comment

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

I think we should propagate the errors and not ignore them

Suggested change
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
await file.write(value)
}
} finally {
reader.releaseLock()
}
await file.close()
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
await file.write(value)
}
} catch(e) {
reader.releaseLock()
await file.close()
throw e
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@amrbashir - with a try / finally block, errors will still propagate as expected, no? Also, I think we should release the lock and close the file handle, regardless of an error, so it seems like finally is the right place for this. Let me know your thoughts.

function test() {
  try {
    throw new Error("Oops!");
  } finally {
    console.log("Finally block runs!");
  }
}

test(); // Logs "Finally block runs!", then throws "Oops!"

Copy link
Member

Choose a reason for hiding this comment

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

this does work indeed, thank you

} else {
await invoke('plugin:fs|write_file', data, {
Expand Down
Loading