microsoft / microsoft/nodejstools
ERR_INVALID_STATE: Reader released when using Discord.js with image attachment in Visual Studio 2022 debugger
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 1.8k
- Forks
- 355
- PR merge metrics
- No merged PRs in 30d
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the minimal AttachmentBuilder and interaction.reply example under Visual Studio 2022 Start Debugging, then compare it with Ctrl+F5 and command-line runs. Inspect the shortened stack through node_modules/undici/lib/web/fetch/body.js alongside the debugger’s Native WebStreams and async I/O handling. Done means the debug run no longer releases the reader unexpectedly, or a documented debugger workaround is identified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100