jeiea
jeiea9mo ago

Why Bad resource ID occurs?

I ran the following and got error: BadResource: Bad resource ID on seek.
import { toText } from "https://deno.land/std@0.204.0/streams/mod.ts";
const temp = await Deno.makeTempFile();
const file = await Deno.open(temp, { create: true, read: true, write: true });
await toText(file.readable);
await file.seek(0, Deno.SeekMode.Start);
import { toText } from "https://deno.land/std@0.204.0/streams/mod.ts";
const temp = await Deno.makeTempFile();
const file = await Deno.open(temp, { create: true, read: true, write: true });
await toText(file.readable);
await file.seek(0, Deno.SeekMode.Start);
2 Replies
Deno AI Helper
Deno AI Helper9mo ago
The error BadResource: Bad resource ID typically occurs when you're trying to perform an operation on a resource (like a file or a network connection) that has already been closed. In your case, the toText(file.readable) line is likely causing the issue. The toText function reads from a readable stream until it's closed. In the process, it also closes the Deno.File resource once it's done reading. So, when you're trying to seek back to the start of the file with file.seek(0, Deno.SeekMode.Start), you're attempting to operate on a file that has already been closed, hence the BadResource: Bad resource ID error. To avoid this, you might want to avoid closing the file until you're done with all operations on it. If you need to read the file's content as text and also perform other operations like seeking, you might want to read the file into memory first, perform your operations, and then close the file. Please note that this advice is not explicitly stated in the documentation, but is based on general file handling practices. 🦕
marvinh.
marvinh.9mo ago
@jeiea Can you file an issue for that on GItHub? That error is very likely pointing to a bug in deno