Skip to content

How to use fd with net.Socket? #8297

@cookiengineer

Description

@cookiengineer

This code will cause problems with an error:

let connector = new _net.Socket({
  fd: null,
// fd: new _stream.Duplex() won't work either
  readable: true,
  writable: true
});

// some time later
connector.connect({ host: host, port: port });

Randomly, depending on how stdout and stderr are repiped of the process, it will give this error:

TypeError: Unsupported fd type: FILE

What the heck does that mean? Everything on stackoverflow is 100% unrelated where people try to create sockets to files. Here I want to create a Socket to a TCP remote port (client-side socket implementation), but the pipe itself is later created using connector.connect({host: host, port: port}).

So at the constructor point in time there can't be such a thing as an already existing TCP Pipe(!?!?!?!).

Debugging

I figured out that the TCP type file descriptor is necessary lib/net.js#L31.

The tty_wrap.guessHandleType(fd) uses internal representations to return type TCP tty_wrap.cc#L66.

Solution?

How to solve this and create a file descriptor for net.Socket?

Having a FILE fd does not help obviously, but there is literally zero documentation on how to create a file descriptor for TCP. I tried to read all the TCP relevant code, but UV_TCP is pretty much everywhere in code related to streams, files, handles and uv ... seems as this is a if/else construct in the whole codebase.

OS Information

Debian, Ubuntu, Archlinux. All amd64 and verified the identical problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    netIssues and PRs related to the net subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions