-
Notifications
You must be signed in to change notification settings - Fork 355
Description
Environment
Visual Studio 2022 (latest, tested with Node.js workload)
Node.js v22.16.0
discord.js v14.14.1
undici (via @discordjs/rest)
Windows 10/11
Reproducible only with: "Start Debugging" (F5)
✅ Works fine with "Start Without Debugging" (Ctrl+F5)
✅ Works fine from command prompt
✅ Works fine from Visual Studio Code (with and without debug)
Problem
Using AttachmentBuilder from discord.js to send an image as a file attachment throws the following error only when run in debug mode from Visual Studio 2022:
TypeError [ERR_INVALID_STATE]: Invalid state: Reader released
Minimal Reproduction
import { AttachmentBuilder } from 'discord.js';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.resolve(__dirname, 'assets/yoshi_approved.png');
const buffer = await fs.readFile(filePath);
const image = new AttachmentBuilder(buffer, { name: 'yoshi_approved.png' }); (use any image in place of this)
// then use it in an interaction.reply({ files: [image] })
This works in every other environment, but fails in VS2022 only while debugging.
Stack Trace (shortened)
TypeError [ERR_INVALID_STATE]: Invalid state: Reader released
at readableStreamReaderGenericRelease
at readableStreamClose
at ReadableByteStreamController.close
at node_modules/undici/lib/web/fetch/body.js
at node:internal/process/task_queues
at node_modules/discord.js
...
Full stack trace available on request.
Notes
Using fs.readFileSync() and Buffer still causes the same failure.
This means it's not the file reading that’s the issue, but likely undici's internal WebStreams or stream hooks being affected by the debugger.
The issue appears to stem from how VS2022's debugger instruments async/stream behavior, which might not be compatible with how modern Node.js and undici use ReadableStream and async hooks.
What's Being Asked
Please investigate the debugger’s async/stream handling when working with:
Native WebStreams
ReadableStreamDefaultReader
Async I/O in Node.js 18+ and 20+ (especially in libraries using undici/fetch).
A fix or workaround (even a flag to disable affected instrumentation) would help tremendously.