Skip to content

Regression: Wrong value in bytesWritten for net.Socket #19562

Description

@patrickjuchli

There is a regression since v9.7.0 (Darwin-x64) until and including v9.9.0:

If I send data with a net.Socket, the property bytesWritten won't reflect the correct number of bytes
written, it's always too low. I have verified this on the receiving end – data is written/sent as expected, only bytesWritten is incorrect.

This works correctly until v9.6.1 (Darwin-x64).

Activity

  1. patrickjuchli commented on Mar 23, 2018

    @patrickjuchli
    Author

    I'm using a stream to send data like so:

    myReadableStream.pipe(mySocket)
    

    The value of mySocket.bytesWritten after the stream has ended is too low.

  2. gireeshpunathil commented on Mar 24, 2018

    @gireeshpunathil
    Member

    Unable to reproduce on mac with this code. Can you review and see where is the difference between our approaches?

    #cat 19562.js

    const n = require('net')
    const v = n.createServer((s) => {
      s.end(Buffer.alloc(process.argv[2] - 0).fill('g'), () => {
        console.log(s.bytesWritten)
      })
    }).listen(8124, () => {
      const c = n.connect(8124, () => {})
      c.on('close', () => {v.close()})
    })

    #node 19562.js 1
    1
    #node 19562.js 32
    32
    #node 19562.js 128
    128
    #node 19562.js 512
    512
    #node 19562.js 1024
    1024

    #node -v
    v9.8.0

  3. patrickjuchli commented on Mar 24, 2018

    @patrickjuchli
    Author
    #node 19562.js 600000
    338324
    
  4. gireeshpunathil commented on Mar 24, 2018

    @gireeshpunathil
    Member

    what is your /etc/sysctl.conf looking like? Mine is:

    kern.ipc.maxsockbuf=16777216
    net.inet.tcp.sendspace=1048576
    net.inet.tcp.recvspace=2097152
    
  5. patrickjuchli commented on Mar 24, 2018

    @patrickjuchli
    Author

    I don't seem to have that on my system (MacOS 10.13.3).

  6. patrickjuchli commented on Mar 24, 2018

    @patrickjuchli
    Author

    I have added an /etc/sysctl.conf with the same lines. It then works for 600000 Bytes, but continues to fail for e.g. 2600000. Again, all of this works fine with v9.6.1.

    #node 19562.js 2600000
    1159456
    
  7. richardlau commented on Mar 24, 2018

    @richardlau
    Member

    If it regressed between v9.6.1 and v9.7.0 then possibly the libuv update (1.19.2)? Maybe related to libuv/libuv#1739?

  8. gireeshpunathil commented on Mar 24, 2018

    @gireeshpunathil
    Member

    thanks @patrickjuchli - following your hint I am able to reproduce in my system too, thought with different data volume, which does not matter.
    #cat 19562.js

    const n = require('net')
    let count = 0
    const v = n.createServer((s) => {
      s.end(Buffer.alloc(process.argv[2] - 0).fill('g'), () => {
        console.log(`server: bytes written: ${s.bytesWritten}`)
      })
    }).listen(8124, () => {
      const c = n.connect(8124, () => {})
      c.on('data', (d) => {count += d.length})
      c.on('close', () => {
        console.log(`client: data received: ${count}`)
        v.close()
      })
    })
    #node 19562.js 99999
    server: bytes written: 99999
    client: data received: 99999
    #node 19562.js 999999
    server: bytes written: 999999
    client: data received: 999999
    #node 19562.js 9999999
    server: bytes written: 8611363
    client: data received: 9999999
    #
    
  9. gireeshpunathil commented on Mar 24, 2018

    @gireeshpunathil
    Member

    I tested with changes undone from libuv/libuv#1739 , but it did not solve this.

  10. gireeshpunathil commented on Mar 24, 2018

    @gireeshpunathil
    Member

    Used git bisect for the first time, and became a fan of it! It brought up 9169449 . I will manually verify it.
    /cc @addaleax

  11. gireeshpunathil commented on Mar 24, 2018

    @gireeshpunathil
    Member

    confirmed.

  12. addaleax commented on Mar 24, 2018

    @addaleax
    Member

    @gireeshpunathil thanks for investigating, and thanks for the ping!

    I think the issue is that req.bytes now only reports the write queue size in some cases, when a write completed only partially synchronously.

    The already-open #19551 should fix this particular issue with bytesWritten, too; so I’ve added a regression test there + a commit that makes sure .bytes now refers to what it originally was supposed to refer to.

  13. added
    netIssues and PRs related to the net subsystem.
    regressionIssues related to regressions.
    on Mar 24, 2018
  14. patrickjuchli commented on Mar 24, 2018

    @patrickjuchli
    Author

    Thanks so much, @gireeshpunathil and @addaleax.

  15. gireeshpunathil commented on Mar 25, 2018

    @gireeshpunathil
    Member

    I verified that #19551 indeed fixes this issue.

  16. added a commit that references this issue on Mar 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    netIssues and PRs related to the net subsystem.regressionIssues related to regressions.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions