Skip to content

Inconsistent net/socket behavior #45382

@qdaoming

Description

@qdaoming

Version

v20.0.0-pre

Platform

Linux xeon-intel-6252N 5.15.0-41-generic #44~20.04.1-Ubuntu SMP Fri Jun 24 13:27:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

net

What steps will reproduce the bug?

  1. launch the server code below: >node myserver.js
  2. launch client code below in another console: >node myclient.js
  3. client will stuck there after printing ('afterWrite').
  4. modify buf's length in myclient.js to 2500*1024, launch client again. All works well.

client side code (myclient.js):

var net = require('net');
//2500KB is OK,2600KB will deadlock
var buf = new Uint8Array(2600*1024);
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
        client.write(buf, function() {
          console.log('all content write finished');
          client.destroy();
        });
        console.log('afterWrite');
});

server side code (myserver.js):

var net = require('net');
var server = net.createServer(function(socket) {
        console.log('server connected');
        //uncomment following line can solve the client dead issue
        // socket.on('data', function (data) { });
});
server.listen(1337, '127.0.0.1');

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

It always happen with my tested node binary from nodev16 to nodev20.

What is the expected behavior?

The client will exit and print the console output as (when client's buf length is 2500*1024):
afterWrite
all content write finished

What do you see instead?

The client will stuck(dead) and print the console output as (when client's buf length is 2600*1024):
afterWrite

Additional information

Such inconsistent behavior is quite misleading. Node.js should represent consistent behavior whatever the buf length is.

There are several workarounds:

  1. modify the server code to add socket.ondata callback
  2. modify node.js lib/internal/streams/readable.js addChunk() function to check ondata listener count

Metadata

Metadata

Assignees

No one assigned

    Labels

    netIssues and PRs related to the net subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions