D
Deno

help

Why Bad resource ID occurs?

Jjeiea10/15/2023
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);
DAHDeno AI Helper10/15/2023
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. 🦕
Mmarvinh.10/15/2023
@jeiea Can you file an issue for that on GItHub? That error is very likely pointing to a bug in deno

Looking for more? Join the community!