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

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

Closed
patrickjuchli opened this issue Mar 23, 2018 · 14 comments
Closed

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

patrickjuchli opened this issue Mar 23, 2018 · 14 comments
Labels
net Issues and PRs related to the net subsystem. regression Issues related to regressions.

Comments

@patrickjuchli
Copy link

patrickjuchli commented Mar 23, 2018

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).

@patrickjuchli
Copy link
Author

patrickjuchli commented Mar 23, 2018

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.

@gireeshpunathil
Copy link
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

@patrickjuchli
Copy link
Author

patrickjuchli commented Mar 24, 2018

#node 19562.js 600000
338324

@gireeshpunathil
Copy link
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

@patrickjuchli
Copy link
Author

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

@patrickjuchli
Copy link
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

@richardlau
Copy link
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?

@gireeshpunathil
Copy link
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
#

@gireeshpunathil
Copy link
Member

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

@gireeshpunathil
Copy link
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

@gireeshpunathil
Copy link
Member

confirmed.

@addaleax
Copy link
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.

@addaleax addaleax added net Issues and PRs related to the net subsystem. regression Issues related to regressions. labels Mar 24, 2018
@patrickjuchli
Copy link
Author

Thanks so much, @gireeshpunathil and @addaleax.

@gireeshpunathil
Copy link
Member

I verified that #19551 indeed fixes this issue.

addaleax added a commit to addaleax/node that referenced this issue Mar 29, 2018
addaleax added a commit to addaleax/node that referenced this issue Mar 29, 2018
Simply always tell the caller how many bytes were written, rather
than letting them track it.

In the case of writing a string, also keep track of the bytes
written by the earlier `DoTryWrite()`.

Refs: nodejs#19562
addaleax added a commit that referenced this issue Mar 30, 2018
Simply always tell the caller how many bytes were written, rather
than letting them track it.

In the case of writing a string, also keep track of the bytes
written by the earlier `DoTryWrite()`.

Refs: #19562

PR-URL: #19551
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR pushed a commit to BridgeAR/node that referenced this issue May 1, 2018
Fixes: nodejs#19562

PR-URL: nodejs#19551
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR pushed a commit to BridgeAR/node that referenced this issue May 1, 2018
Simply always tell the caller how many bytes were written, rather
than letting them track it.

In the case of writing a string, also keep track of the bytes
written by the earlier `DoTryWrite()`.

Refs: nodejs#19562

PR-URL: nodejs#19551
Reviewed-By: James M Snell <jasnell@gmail.com>
addaleax added a commit that referenced this issue Jun 29, 2018
Fixes: #19562

PR-URL: #19551
Reviewed-By: James M Snell <jasnell@gmail.com>
rvagg pushed a commit that referenced this issue Aug 16, 2018
Fixes: #19562

PR-URL: #19551
Reviewed-By: James M Snell <jasnell@gmail.com>
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. regression Issues related to regressions.
Projects
None yet
Development

No branches or pull requests

4 participants