node icon indicating copy to clipboard operation
node copied to clipboard

ERR_INTERNAL_ASSERTION using open on tmpdir

Open Ethan-Arrowood opened this issue 2 years ago • 2 comments

Version

16.16.0

Platform

Darwin Venusaur.local 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

Run this script:

import { once } from 'node:events';
import { tmpdir } from 'node:os';
import { open, readFile } from 'node:fs/promises';
import { join } from 'node:path';

const tmp = await open(join(tmpdir(), 'tmp'), 'w+');

const write = process.stdout.write;
process.stdout.write = tmp.write;

console.log('foo');

process.stdout.write = write;

console.log(await readFile(tmp))

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior?

not sure, just trying to patch stdout 😅

What do you see instead?

❯ node tests/test.mjs
node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_INTERNAL_ASSERTION]: handle must be an instance of FileHandle
This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
Please open an issue with this stack trace at https://github.com/nodejs/node/issues

    at new NodeError (node:internal/errors:372:5)
    at assert (node:internal/assert:14:11)
    at fsCall (node:internal/fs/promises:301:3)
    at WriteStream.write (node:internal/fs/promises:179:12)
    at console.value (node:internal/console/constructor:286:16)
    at console.log (node:internal/console/constructor:360:26)
    at file:///Users/ethanarrowood/Documents/Programming/github/jsperf.dev/jsperf.dev/packages/benchmark/tests/test.mjs:11:9 {
  code: 'ERR_INTERNAL_ASSERTION'
}

Additional information

It is probably my fault - but error message said to open an issue so I am!

Ethan-Arrowood avatar Aug 05 '22 01:08 Ethan-Arrowood

I think this qualifies for "incorrect usage of Node.js internals". Thoughts @nodejs/fs?

aduh95 avatar Aug 05 '22 15:08 aduh95

const tmp = await open(join(tmpdir(), 'tmp'), 'w+');
process.stdout.write = tmp.write;

tmp is an instance of FileHandle, process.stdout is not. Calling Filehandle.write() with this not being a Filehandle won't work and is indeed an incorrect usage.

Depending on what you want to achieve, it should be possible to use process.stdout.fd property which is a file descriptor (number, not a filehandle), or writable.write(chunk[, encoding][, callback]) method of writable streams.

LiviaMedeiros avatar Aug 05 '22 15:08 LiviaMedeiros