Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Streams v2 problem with SSL #4657

Closed
jerem opened this issue Jan 24, 2013 · 11 comments
Closed

Streams v2 problem with SSL #4657

jerem opened this issue Jan 24, 2013 · 11 comments
Assignees
Milestone

Comments

@jerem
Copy link

jerem commented Jan 24, 2013

It seems streams 2 with SSL doesn't work properly. Indeed, as soon as you read something on the stream it internally keeps reading the whole content of the stream. (Tested on both 0.9.7 and master branch as of today)

Have a look at the following code and see how the memory and the buffer length keep increasing:

require('https').get('https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.7.4.tar.bz2', function(response) {
    response.read(1);
    setInterval(function() {
      console.log('RSS', process.memoryUsage().rss, 'Buffer Length:', response._readableState.length);
    }, 1000);
});

Which outputs something like this on my machine:

RSS 21098496 Buffer Length: 573440
RSS 21839872 Buffer Length: 884736
RSS 22654976 Buffer Length: 1245184
RSS 23351296 Buffer Length: 1589248
RSS 24072192 Buffer Length: 1949696
RSS 25018368 Buffer Length: 2310144
...
RSS 38875136 Buffer Length: 11583488
RSS 39202816 Buffer Length: 11911168
RSS 39596032 Buffer Length: 12304384
RSS 39956480 Buffer Length: 12648448

The exact same code without SSL works properly:

require('http').get('http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.7.4.tar.bz2', function(response) {
    response.read(1);
    setInterval(function() {
      console.log('RSS', process.memoryUsage().rss, 'Buffer Length:', response._readableState.length);
    }, 1000);
});

Output:

RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
RSS 17129472 Buffer Length: 17063
@TooTallNate
Copy link

/cc @isaacs

@isaacs
Copy link

isaacs commented Jan 30, 2013

Wow, CryptoStream never got streams2-ified. That's a pretty huge oversight on my part. Nice catch.

Short term, this will stop the leak:

diff --git a/lib/http.js b/lib/http.js
index ae6976a..f52624b 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -36,13 +36,21 @@ if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
 }

 function readStart(socket) {
-  if (!socket || !socket._handle || !socket._handle.readStart) return;
-  socket._handle.readStart();
+  if (!socket)
+    return;
+  if (socket._handle && socket._handle.readStart)
+    socket._handle.readStart();
+  else if (socket.resume)
+    socket.resume();
 }

 function readStop(socket) {
-  if (!socket || !socket._handle || !socket._handle.readStop) return;
-  socket._handle.readStop();
+  if (!socket)
+    return;
+  if (socket._handle && socket._handle.readStop)
+    socket._handle.readStop();
+  else if (socket.pause)
+    socket.pause();
 }

 // Only called in the slow case where slow means

but more properly, CryptoStream needs to be refactored.

@ghost ghost assigned isaacs Jan 30, 2013
@indutny
Copy link
Member

indutny commented Jan 30, 2013

I can probably look into it. (CryptoStream 2.0)

@isaacs
Copy link

isaacs commented Jan 30, 2013

@indutny Your help would be much appreciated. Also, it'd be a good thing to get your head around streams2 so we have another person who can debug issues.

@indutny
Copy link
Member

indutny commented Feb 6, 2013

Done in d59beb9

@indutny indutny closed this as completed Feb 6, 2013
@jerem
Copy link
Author

jerem commented Feb 21, 2013

Gentlemen, my test case posted in the issue is still not working properly. 😱

@indutny
Copy link
Member

indutny commented Feb 21, 2013

Confirming, the problem is so obvious that it makes me feel sad. We should stop using ondata in http this is causing such weird stuff to happen...

@indutny
Copy link
Member

indutny commented Feb 21, 2013

@jerem can you try commit above? ^

@jerem
Copy link
Author

jerem commented Feb 21, 2013

It fixed my test case. 👏

indutny added a commit to indutny/node that referenced this issue Feb 21, 2013
lib/http.js is using stream._handle.readStart/readStop to control
data-flow coming out from underlying stream. If this methods are not
present - data might be buffered regardless of whether it'll be read.

see nodejs#4657
@indutny
Copy link
Member

indutny commented Feb 21, 2013

Fixed in d534c2b

indutny added a commit that referenced this issue Feb 21, 2013
lib/http.js is using stream._handle.readStart/readStop to control
data-flow coming out from underlying stream. If this methods are not
present - data might be buffered regardless of whether it'll be read.

see #4657
@indutny
Copy link
Member

indutny commented Feb 21, 2013

Oops, in ebc95f0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants