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
3 Replies
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 ...
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 filinga new issue with a detailed description of the problem and the steps to reproduce it. 🦕