node icon indicating copy to clipboard operation
node copied to clipboard

http: fix the reverted commit which would swallow some errors

Open Archkon opened this issue 4 months ago • 6 comments

Alternative PR to follow-up #64507

Follow-up to: https://github.com/nodejs/node/pull/64507 Original PR Refs: https://github.com/nodejs/node/pull/64278 Fixes: https://github.com/nodejs/node/pull/64272 Refs:https://github.com/nodejs/node/pull/64511 Refs: https://github.com/libuv/libuv/issues/5196 Refs: https://github.com/nodejs/node/pull/64507#issuecomment-4999378702 Refs: https://github.com/nodejs/node/pull/64511#issuecomment-4978451178

Archkon avatar Jul 17 '26 21:07 Archkon

Review requested:

  • [ ] @nodejs/http
  • [ ] @nodejs/net
  • [ ] @nodejs/streams

nodejs-github-bot avatar Jul 17 '26 21:07 nodejs-github-bot

@pimterry I think you could edit my PR branch, so could you add empty commit with only sign-off-by in commit messgae and I would squash into one commit beacuse I use the some test code authored by you?

Archkon avatar Jul 17 '26 22:07 Archkon

Codecov Report

:x: Patch coverage is 74.41860% with 11 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 90.28%. Comparing base (2ddade6) to head (dcbb130). :warning: Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
lib/_http_client.js 75.00% 8 Missing :warning:
lib/internal/stream_base_commons.js 66.66% 2 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64566      +/-   ##
==========================================
- Coverage   90.28%   90.28%   -0.01%     
==========================================
  Files         762      762              
  Lines      247646   247687      +41     
  Branches    46695    46705      +10     
==========================================
+ Hits       223596   223623      +27     
  Misses      15496    15496              
- Partials     8554     8568      +14     
Files with missing lines Coverage Δ
lib/internal/streams/utils.js 97.80% <100.00%> (+0.01%) :arrow_up:
lib/internal/stream_base_commons.js 95.25% <66.66%> (-0.92%) :arrow_down:
lib/_http_client.js 97.08% <75.00%> (-0.56%) :arrow_down:

... and 35 files with indirect coverage changes

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jul 17 '26 23:07 codecov[bot]

@pimterry Could you take a look at this and evaluate this pr proposal? Thanks !

Archkon avatar Jul 24 '26 03:07 Archkon

Sorry I haven't reviewed this yet @Archkon. I think it does match the description before, but I did some digging and I think there's an existing bug with writableFinished, which means even though that's correct we can't enable this yet... We'll need to fix that as well to ship this. Demo:

'use strict';
const assert = require('assert');
const http = require('http');
const net = require('net');

// A server that never reads the request body and aborts the connection.
const server = net.createServer((socket) => socket.destroy());

server.listen(0, () => {
  const req = http.request({ port: server.address().port, method: 'POST' });
  let writeError = null;
  let finishEmitted = false;

  req.on('finish', () => { finishEmitted = true; });
  req.on('error', () => {});

  req.on('close', () => {
    server.close();

    // The write failed, so the body was never flushed to the socket.
    assert.strictEqual(writeError.code, 'EPIPE');

    assert.strictEqual(req.writableFinished, false,
                       'writableFinished is true after a failed write');
    assert.strictEqual(finishEmitted, false,
                       "'finish' was emitted after a failed write");
  });

  // 1 MiB, so the write cannot fit in the socket buffers and must fail.
  req.write(Buffer.alloc(1024 * 1024), (err) => { writeError = err; });
  req.end();
});

If you tweak your tests slightly you'll hit the same thing. This seems to be an existing bug, but it doesn't normally matter because error emits first and we don't base anything on writableFinished. Once we do, this will swallow write errors just like before. Do you have a little time to take a look?

pimterry avatar Jul 25 '26 13:07 pimterry

Sorry I haven't reviewed this yet @Archkon. I think it does match the description before, but I did some digging and I think there's an existing bug with writableFinished, which means even though that's correct we can't enable this yet... We'll need to fix that as well to ship this. Demo:

'use strict';
const assert = require('assert');
const http = require('http');
const net = require('net');

// A server that never reads the request body and aborts the connection.
const server = net.createServer((socket) => socket.destroy());

server.listen(0, () => {
  const req = http.request({ port: server.address().port, method: 'POST' });
  let writeError = null;
  let finishEmitted = false;

  req.on('finish', () => { finishEmitted = true; });
  req.on('error', () => {});

  req.on('close', () => {
    server.close();

    // The write failed, so the body was never flushed to the socket.
    assert.strictEqual(writeError.code, 'EPIPE');

    assert.strictEqual(req.writableFinished, false,
                       'writableFinished is true after a failed write');
    assert.strictEqual(finishEmitted, false,
                       "'finish' was emitted after a failed write");
  });

  // 1 MiB, so the write cannot fit in the socket buffers and must fail.
  req.write(Buffer.alloc(1024 * 1024), (err) => { writeError = err; });
  req.end();
});

If you tweak your tests slightly you'll hit the same thing. This seems to be an existing bug, but it doesn't normally matter because error emits first and we don't base anything on writableFinished. Once we do, this will swallow write errors just like before. Do you have a little time to take a look?

Ok, but I was busy with other pr so I think this would be delayed a bit to solve

Archkon avatar Jul 25 '26 14:07 Archkon

I've opened a separate PR to fix writableFinished: https://github.com/nodejs/node/pull/64847. Once that's merged, I think this will work correctly.

pimterry avatar Jul 30 '26 14:07 pimterry

I've opened a separate PR to fix writableFinished: #64847. Once that's merged, I think this will work correctly.

Fine,I hope this would push this pr forward smoothly

Archkon avatar Jul 30 '26 15:07 Archkon

https://github.com/nodejs/node/pull/64847 is now merging any second (just waiting for the commit queue). As soon as that's on main, you can rebase this and then hopefully the above example will now work correctly and we can get this merged too.

pimterry avatar Aug 04 '26 07:08 pimterry

Failed agian, and I need some time to deeply dive into this. It would be very weird to fail only on aarch64

Archkon avatar Aug 04 '26 11:08 Archkon