GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http2: fix endless loop when write an empty string by XadillaX · Pull Request #18673 · nodejs/node

Conversation

@XadillaX

@XadillaX XadillaX commented Feb 9, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

If we do req.write("") (an empty string) on Http2Stream, the gather
logic will be messed up:

while ((src_length = nghttp2_session_mem_send(session_, &src)) > 0) {
  DEBUG_HTTP2SESSION2(this, "nghttp2 has %d bytes to send", src_length);
  CopyDataIntoOutgoing(src, src_length);
}

The logic above is in function Http2Session::SendPendingData under
src/node_http2.cc. This while will be endless when an empty string is
inside because src_length will always be 9.

And then the main event loop thread will be blocked and the memory used
will be larger and larger.

This pull request is to ignore empty string or buffer in _write() and
_writev() of Http2Stream.

Fixes: #18169
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661

Checklist
  • make -j4 test passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

http2

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x http2 Issues and PRs related to the http2 subsystem. labels Feb 9, 2018
addaleax
addaleax reviewed Feb 9, 2018
View reviewed changes
Comment thread lib/internal/http2/core.js Outdated

@addaleax addaleax Feb 9, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first piece of the conditional is always true; Also, you’d still need to call cb.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@XadillaX XadillaX Feb 11, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused that is the logic below the same?

req.write('');
req.end();

vs

req.end();

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@XadillaX XadillaX Feb 11, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means can I actully ignore empty string stream?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/internal/http2/core.js Outdated

@addaleax addaleax Feb 9, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Basically the same comment, except that the first piece of the conditional is never true; data is an array)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@XadillaX
http2: fix endless loop when write an empty string
5ab3255
If we do `req.write("")` (an empty string) on Http2Stream, the gather
logic will be messed up:

```
while ((src_length = nghttp2_session_mem_send(session_, &src)) > 0) {
  printf("write\n");
  DEBUG_HTTP2SESSION2(this, "nghttp2 has %d bytes to send", src_length);
  CopyDataIntoOutgoing(src, src_length);
}
```

The logic above is in function `Http2Session::SendPendingData` under
src/node_http2.cc. This `while` will be endless when an empty string is
inside because `src_length` will always be 9.

And then the main event loop thread will be blocked and the memory used
will be larger and larger.

This pull request is to ignore empty string or buffer in `_write()` and
`_writev()` of Http2Stream.

Fixes: nodejs#18169
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
@XadillaX
XadillaX force-pushed the fix-18169 branch from 1f17dcc to 5ab3255 Compare February 11, 2018 07:09
@XadillaX
add test cases
83fb36a
@XadillaX XadillaX changed the title [WIP] http2: fix endless loop when write an empty string http2: fix endless loop when write an empty string Feb 11, 2018
@XadillaX

XadillaX commented Feb 11, 2018

Copy link
Copy Markdown
Contributor Author

@addaleax I've updated the code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax

addaleax commented Feb 11, 2018

Copy link
Copy Markdown
Member

@XadillaX But this still wouldn’t send out an empty frame, like we’d expect, right?

I think this line is the reason why we don’t tell nghttp2 to defer the frames:

node/src/node_http2.cc

Line 2232 in a1bab82

if (amount == 0 && stream->IsWritable() && stream->queue_.empty()) {

The queue isn’t empty when we schedule an empty write, even though we don’t have any data available. Maybe we can just drop the last check in that conditional?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@XadillaX

XadillaX commented Feb 12, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@addaleax I found that module http ignores empty stream (but write header) either.

node/lib/_http_outgoing.js

Lines 650 to 652 in a1bab82

// If we get an empty string or buffer, then just do nothing, and
// signal the user to keep writing.
if (chunk.length === 0) return true;

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax

addaleax commented Feb 12, 2018

Copy link
Copy Markdown
Member

@XadillaX You’re right, and this way is certainly an approach to fix this bug. I’d have a preference for fixing it on the native level, though:

  • One of the next things I’ll be working on wrt refactoring Node’s native streams is that I’ll try to abstract everything from net.Socket and Http2Stream so that there’ll be a common JS interface for the native StreamBase handles. That means that the _write() functions also should be merged, which would probably require undoing this change as it is now. :/
  • In a similar vein, the bug here is not part of the JS code that uses the StreamBase interface; it’s specific to how HTTP/2 implements that interface, so the implementation should be fixed rather than the JS code that just provides bindings.
    • At least one case in which we rely on empty writes being meaningful is the TLS implementation; they are explicitly used to flush the underlying protocol’s state. I think it’s reasonable to make the same assumptions about HTTP/2 (and for HTTP/1 there’s no equivalent of flushing underlying state).
  • The code line I linked above seems to just be buggy, it should be fixed anyway, and doing so should fully resolve this bug.
  • The current approach does not fix the bug fully: Replacing the req.write(""); in the original reproduction with two calls, i.e. req.write(""); req.write("");, still makes the bug appear even with this PR applied.

If you want, you can still make the case for not sending empty data frames on the HTTP/2 level, but that should still be done as part of the implementation, not part of the streams API.

jasnell and killagu reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax

addaleax commented Feb 16, 2018

Copy link
Copy Markdown
Member

Hm … @XadillaX any further thoughts on this one?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax addaleax mentioned this pull request Feb 22, 2018
http2: fix endless loop when writing empty string #18924
Closed
3 tasks done
@addaleax

addaleax commented Feb 22, 2018

Copy link
Copy Markdown
Member

@XadillaX I’ve spelled out my thoughts in #18924. I have to admit the solution is not quite as elegant as I had thought, but I still think the C++ level is the right place to address this issue

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

addaleax added a commit to addaleax/node that referenced this pull request Feb 22, 2018
@addaleax
http2: fix endless loop when writing empty string
9b72053
Refs: nodejs#18673
Fixes: nodejs#18169
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
addaleax pushed a commit to addaleax/node that referenced this pull request Feb 22, 2018
@XadillaX @addaleax
test: check endless loop while writing empty string
74f489c
Refs: nodejs#18673
@XadillaX

XadillaX commented Feb 24, 2018

Copy link
Copy Markdown
Contributor Author

@addaleax Sorry for late reply because of Chinese Spring Festival. I think you're right, if we can solve this issue in C++, then in C++.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR

BridgeAR commented Mar 2, 2018

Copy link
Copy Markdown
Member

I am closing this since @XadillaX gave a LG in #18924.

If anyone feels like that is wrong, please go ahead and reopen.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR BridgeAR closed this Mar 2, 2018
addaleax added a commit that referenced this pull request Mar 2, 2018
@addaleax
http2: fix endless loop when writing empty string
9bc333c
PR-URL: #18924
Fixes: #18169
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax pushed a commit that referenced this pull request Mar 2, 2018
@XadillaX @addaleax
test: check endless loop while writing empty string
16facd7
Refs: #18673
PR-URL: #18924
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax added a commit to addaleax/node that referenced this pull request Mar 5, 2018
@addaleax
http2: fix endless loop when writing empty string
8bc930c
PR-URL: nodejs#18924
Fixes: nodejs#18169
Refs: nodejs#18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax pushed a commit to addaleax/node that referenced this pull request Mar 5, 2018
@XadillaX @addaleax
test: check endless loop while writing empty string
8d595bb
Refs: nodejs#18673
PR-URL: nodejs#18924
Refs: nodejs#18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
kjin pushed a commit to kjin/node that referenced this pull request May 1, 2018
@addaleax @kjin
http2: fix endless loop when writing empty string
c46ee1f
PR-URL: nodejs#18924
Fixes: nodejs#18169
Refs: nodejs#18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
kjin pushed a commit to kjin/node that referenced this pull request May 1, 2018
@XadillaX @kjin
test: check endless loop while writing empty string
74212aa
Refs: nodejs#18673
PR-URL: nodejs#18924
Refs: nodejs#18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 2, 2018
@addaleax @MylesBorins
http2: fix endless loop when writing empty string
1f3ab4f
Backport-PR-URL: #20456
PR-URL: #18924
Fixes: #18169
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 2, 2018
@XadillaX @MylesBorins
test: check endless loop while writing empty string
199d9a5
Refs: #18673
Backport-PR-URL: #20456
PR-URL: #18924
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@addaleax @MayaLekova
http2: fix endless loop when writing empty string
ebdc9c7
PR-URL: nodejs#18924
Fixes: nodejs#18169
Refs: nodejs#18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@XadillaX @MayaLekova
test: check endless loop while writing empty string
33b4a05
Refs: nodejs#18673
PR-URL: nodejs#18924
Refs: nodejs#18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 15, 2018
@addaleax @MylesBorins
http2: fix endless loop when writing empty string
dc3f6c0
Backport-PR-URL: #20456
PR-URL: #18924
Fixes: #18169
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 15, 2018
@XadillaX @MylesBorins
test: check endless loop while writing empty string
bac9344
Refs: #18673
Backport-PR-URL: #20456
PR-URL: #18924
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 15, 2018
@addaleax @MylesBorins
http2: fix endless loop when writing empty string
b75897f
Backport-PR-URL: #20456
PR-URL: #18924
Fixes: #18169
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 15, 2018
@XadillaX @MylesBorins
test: check endless loop while writing empty string
b066442
Refs: #18673
Backport-PR-URL: #20456
PR-URL: #18924
Refs: #18673
Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484
Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
This was referenced Mar 7, 2020
[Snyk] Security upgrade gatsby from 2.13.73 to 2.16.2 meghasfdc/node#4
Open
[Snyk] Security upgrade gatsby from 2.13.73 to 2.16.2 kissofire/node#3
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@addaleax addaleax addaleax left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

http2 Issues and PRs related to the http2 subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Memory leak with http2 write()

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@XadillaX @addaleax @BridgeAR @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.