Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_INTERNAL_ASSERTION] #48358

Closed
mStirner opened this issue Jun 6, 2023 · 5 comments
Closed

Error [ERR_INTERNAL_ASSERTION] #48358

mStirner opened this issue Jun 6, 2023 · 5 comments

Comments

@mStirner
Copy link

mStirner commented Jun 6, 2023

Version

v16.20.0

Platform

Linux stirner 5.4.0-149-generic #166~18.04.1-Ubuntu SMP Fri Apr 21 16:42:44 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Run the following code:

client.js

const { Agent } = require("http")
const { createConnection } = require("net");
const WebSocket = require("ws");

const agent = new Agent({
    maxSockets: 1,
    keepAlive: true
});


agent.createConnection = (...args) => {

    console.log("Create connection", args);

    let socket = createConnection(...args);


    return new Proxy(socket, {

    });

};



const ws1 = new WebSocket("ws://127.0.0.1:8080", {
    agent
});

const ws2 = new WebSocket("ws://127.0.0.1:8080", {
    agent
});


[ws1, ws2].forEach((ws) => {

    ws.on("open", () => {
        console.log("WebSocket opend: %s", ws.url);
    });

    ws.on("close", () => {
        console.log("WebSocket closed: %s", ws.url);
    });

});

server.js

const { Server } = require("ws");

const wss = new Server({
    port: 8080
});


wss.on("connection", (ws) => {

    console.log("Client connected");

    ws.on("close", () => {
        console.log("Client disconnected");
    });

});

wss.on("listening", () => {
    console.log("Server listening")
});

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

Every time node client.js is executed.
The root of this issue is the proxy inside of agent.createConnection.

I try to understand the anatomy/life cycle of a http request better.
Especially the role and handling of the agent.

What is the expected behavior? Why is that the expected behavior?

The proxied socket object returned from agent.createConnection, should trigger the defined traps. (Dosnt matter if there are any or not).
As soon as agent.createConnection, return the proxy object, the error is thrown.

What do you see instead?

node:internal/assert:14
    throw new ERR_INTERNAL_ASSERTION(message);
    ^

Error [ERR_INTERNAL_ASSERTION]: 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:387:5)
    at assert (node:internal/assert:14:11)
    at Socket.socketOnData (node:_http_client:532:3)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  code: 'ERR_INTERNAL_ASSERTION'
}

Additional information

Complete command output:

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Create connection [
  [Object: null prototype] {
    protocolVersion: 13,
    maxPayload: 104857600,
    perMessageDeflate: true,
    followRedirects: false,
    maxRedirects: 10,
    agent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 80,
      protocol: 'http:',
      options: [Object: null prototype],
      requests: [Object: null prototype] {},
      sockets: [Object: null prototype],
      freeSockets: [Object: null prototype] {},
      keepAliveMsecs: 1000,
      keepAlive: true,
      maxSockets: 1,
      maxFreeSockets: 256,
      scheduling: 'lifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 0,
      createConnection: [Function (anonymous)],
      [Symbol(kCapture)]: false
    },
    createConnection: [Function: netConnect],
    socketPath: undefined,
    hostname: undefined,
    protocol: undefined,
    timeout: undefined,
    method: undefined,
    host: '127.0.0.1',
    path: null,
    port: '8080',
    defaultPort: 80,
    headers: {
      'Sec-WebSocket-Version': 13,
      'Sec-WebSocket-Key': 'nVSV53R5OFgshY881raCAw==',
      Connection: 'Upgrade',
      Upgrade: 'websocket',
      'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits'
    },
    maxSockets: 1,
    keepAlive: true,
    servername: '',
    _agentKey: '127.0.0.1:8080:',
    encoding: null,
    keepAliveInitialDelay: 1000
  },
  [Function (anonymous)]
]
node:internal/assert:14
    throw new ERR_INTERNAL_ASSERTION(message);
    ^

Error [ERR_INTERNAL_ASSERTION]: 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:387:5)
    at assert (node:internal/assert:14:11)
    at Socket.socketOnData (node:_http_client:532:3)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  code: 'ERR_INTERNAL_ASSERTION'
}
@bnoordhuis
Copy link
Member

Please post a test case that does not use third-party dependencies like ws. As the error message says, it's likely incorrect usage of node.js internals.

I'll reopen this when you've updated the test case.

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2023
@mStirner
Copy link
Author

mStirner commented Jun 6, 2023

@bnoordhuis

Please post a test case that does not use third-party dependencies like ws. As the error message says, it's likely incorrect usage of node.js internals.

Sure, no problem.
The issue is still present with the native http module/request:

const { Agent, request } = require("http")
const { createConnection } = require("net");

const agent = new Agent({
    maxSockets: 1,
    keepAlive: true
});

agent.createConnection = (options) => {

    let { host, port } = options;

    console.log("Create connection", host, port);

    let socket = createConnection(options);
    return new Proxy(socket, {
        // proxy handler here
    });

};


let req = request("http://example.com", {
    agent
});

req.on("response", (res) => {
    res.pipe(process.stdout);
});

req.end();

Output:

Create connection example.com 80
node:internal/assert:14
    throw new ERR_INTERNAL_ASSERTION(message);
    ^

Error [ERR_INTERNAL_ASSERTION]: 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:387:5)
    at assert (node:internal/assert:14:11)
    at Socket.socketOnData (node:_http_client:532:3)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  code: 'ERR_INTERNAL_ASSERTION'
}

As said above, without the proxied socket, everything works fine.

@bnoordhuis
Copy link
Member

It's not surprising it doesn't work because the proxy breaks object equivalence:

const o = {}
const p = new Proxy(o, {})
assert(o === p) // false

That becomes relevant when this !== socket in callbacks.

I don't consider that a bug because you're breaking a rather fundamental assumption. Node is right to complain about "incorrect usage of Node.js internals." :-)

@mStirner
Copy link
Author

mStirner commented Jun 6, 2023

Hmm, thats not good for me 😞

I try to better understand the anatomy of a http request and the job of the agent in this context.
For that i wanted to know when/which property/method of the underlying net socket is accessed/called.

Is there somewhere a diagram of the workflow of the agent?
Like here for streams?

@bnoordhuis
Copy link
Member

The documentation for agent.createConnection() lays out the contract: in a nutshell it needs to adhere to the stream.Duplex protocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants