vwkd
vwkd•5mo ago

Streaming download throws `Bad resource ID`

Anyone have an idea why this errors when closing the file even though the download was successfully written to the file? deno 1.45.2
const res = await fetch(url);

if (!res.ok) {
throw new Error(`Got error ${res.status} ${res.statusText}`);
}

const body = res.body;

if (!body) {
throw new Error(`Got no body`);
}

const file = await Deno.create(path);

try {
await body.pipeTo(file.writable);
} catch (e) {
await Deno.remove(path);
throw new Error(`Download interrupted ${e}`);
} finally {
file.close();
}
const res = await fetch(url);

if (!res.ok) {
throw new Error(`Got error ${res.status} ${res.statusText}`);
}

const body = res.body;

if (!body) {
throw new Error(`Got no body`);
}

const file = await Deno.create(path);

try {
await body.pipeTo(file.writable);
} catch (e) {
await Deno.remove(path);
throw new Error(`Download interrupted ${e}`);
} finally {
file.close();
}
error: Uncaught (in promise) BadResource: Bad resource ID
file.close();
^
at FsFile.close (ext:deno_fs/30_fs.js:743:10)
at loadFile (file:///foobar/loadFile.ts:142:16)
at eventLoopTick (ext:core/01_core.js:168:7)
at async file:///foobar/main.ts:42:7
error: Uncaught (in promise) BadResource: Bad resource ID
file.close();
^
at FsFile.close (ext:deno_fs/30_fs.js:743:10)
at loadFile (file:///foobar/loadFile.ts:142:16)
at eventLoopTick (ext:core/01_core.js:168:7)
at async file:///foobar/main.ts:42:7
3 Replies
vwkd
vwkdOP•5mo ago
Is it because a successful pipeTo already closes the file? https://github.com/denoland/deno/issues/19653#issuecomment-1613519066 If so, should I still close the file in the catch block in case pipeTo throws?
GitHub
console.log right after pipe'ing stream to Deno.stdout.writable...
Reproduction: const command = new Deno.Command("deno", { args: ["eval", '"throw 1"'], stderr: "piped", }); const subprocess = command.spawn(); await ...
Deno AI Helper
Deno AI Helper•5mo ago
The error message "Bad resource ID" typically indicates that there's an attempt to perform an operation on a resource (like a file or a network connection) using an invalid resource identifier (rid). This can happen if the resource has already been closed or if the rid is incorrect. In your case, the error occurs when you're trying to close the file after piping the response body to it. The code you've provided seems correct at first glance, and it should work as expected in Deno. However, there have been some changes in Deno's API related to resources and streams. According to the documentation, starting from Deno 1.40, the Deno.close() method has been deprecated in favor of calling the .close() method directly on the resource object. This aligns with your usage of file.close() in the finally block, which is the correct way to close a file in the newer versions of Deno. Given that your Deno version is 1.45.2, it's unlikely that the issue is due to a deprecated API. It's possible that the error might be caused by something else in your environment or in a part of the code that's not shown here. Here are a few steps you can take to debug this issue: 1. Ensure that the file object is not being closed elsewhere in your code before reaching the finally block. 2. Check if there are any other asynchronous operations that might be interfering with the file stream. 3. Make sure that the path variable is a valid file path and that there are no permission issues that might prevent the file from being written or closed. 4. Update Deno to the latest version and try running the code again to see if the issue persists. If after checking these points the issue still occurs, you might want to look for any open issues on the Deno GitHub repository or consider filing
Deno AI Helper
Deno AI Helper•5mo ago
a new issue with a detailed description of the problem and the steps to reproduce it. 🦕

Did you find this page helpful?