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

Socket.readyState is initially open #38247

Open
morkai opened this issue Apr 15, 2021 · 3 comments
Open

Socket.readyState is initially open #38247

morkai opened this issue Apr 15, 2021 · 3 comments
Labels
net Issues and PRs related to the net subsystem.

Comments

@morkai
Copy link
Contributor

morkai commented Apr 15, 2021

  • Version: v14.16.1
  • Platform: Microsoft Windows NT 10.0.19042.0 x64
  • Subsystem: net

What steps will reproduce the bug?

const {Socket} = require('net');
const socket = new Socket();
socket.on('connect', () => console.log('readyState in connect=', socket.readyState));
socket.on('close', () => console.log('readyState in close=', socket.readyState));
socket.on('error', (err) => console.log('readyState in error=', socket.readyState, err.message));
console.log('process.version=', process.version);
console.log('readyState before connect=', socket.readyState);
console.log('connecting...');
socket.connect(502, '192.168.1.1');
console.log('readyState after connect=', socket.readyState);

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

Always. No.

What is the expected behavior?

net.Socket.readyState should be initially closed like in the v12:

process.version= v12.22.1
readyState before connect= closed
connecting...
readyState after connect= opening
readyState in error= closed connect ECONNREFUSED 192.168.1.1:502
readyState in close= closed

What do you see instead?

net.Socket.readyState is open after initialization, before any call to connect():

process.version= v14.16.1
readyState before connect= open
connecting...
readyState after connect= opening
readyState in error= closed connect ECONNREFUSED 192.168.1.1:502
readyState in close= closed

Additional information

This change made the Socket start as readable & writable: eeccd52

In v12 one could check if the socket is open by socket.readyState === 'open'
In v14 another condition must be added !socket.pending && socket.readyState === 'open'

@Ayase-252 Ayase-252 added the net Issues and PRs related to the net subsystem. label Apr 15, 2021
@EladKeyshawn
Copy link

Can confirm on master as well

@Ayase-252
Copy link
Member

Per docs

This property represents the state of the connection as a string.
If the stream is connecting socket.readyState is opening.
If the stream is readable and writable, it is open.
If the stream is readable and not writable, it is readOnly.
If the stream is not readable and writable, it is writeOnly.

#32272 mades Socket starting as readable and writable then socket.readyState is working as intended? 🤔

@EladKeyshawn
Copy link

@Ayase-252 If that's the raw definition of 'open' socket means then yes, but I don't think it's very intuitive IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net Issues and PRs related to the net subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants