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

ReadableStreamBuffer stalls if not used right away #30

Open
peller opened this issue Jan 27, 2017 · 6 comments
Open

ReadableStreamBuffer stalls if not used right away #30

peller opened this issue Jan 27, 2017 · 6 comments

Comments

@peller
Copy link

peller commented Jan 27, 2017

I instantiated a ReadableStreamBuffer, attached a 'data' event, and called an asynchronous call which took much longer than the frequency of the running timeout. In the initial run of sendData, amount was zero so sendMore was false and the timer did not continue to run. Only after that did my data come in, and I called put() but the contents were never seen by the observer.

My workaround was to call _read() to wake up the stream.

@peller peller changed the title ReadableStreamBuffer dies if not used right away ReadableStreamBuffer stalls if not used right away Jan 27, 2017
@timdp
Copy link

timdp commented Mar 14, 2017

Confirmed. Easy to reproduce as well:

const {ReadableStreamBuffer} = require('stream-buffers')

const readable = new ReadableStreamBuffer()

readable.on('data', (data) => {
  console.log('data:', data.toString())
})

readable.on('end', () => {
  console.log('end')
})

setTimeout(() => {
  console.log('putting')
  readable.put('abcdef')
  readable.stop()
}, 100)

@rcoenen
Copy link

rcoenen commented Apr 28, 2017

Confirmed - my first NodeJS project ever - I ran into this

in /node_modules/lib/streambuffers/readable_streambuffer.js

this seems to be the culprit: _read probably was underscored indicating it's private and shouldn't be overloaded. The function gets called right away regardless the TimeOut spficied for the StreamBuffer. It finds an empty buffer, returns false, and kille the timer - result: StreamBuffer is now dead.

 this._read = function() {
	  console.log('_read just called');
    if (!sendData.timeout) {
      sendData.timeout = setTimeout(sendData, frequency);
    }
  };

de

@mvachhar
Copy link
Contributor

mvachhar commented Apr 2, 2018

The override of _read here is fine, as documented here https://nodejs.org/api/stream.html#stream_implementing_a_readable_stream

The issue is as follows:

  1. ReadableStreamBuffer is instantiated
  2. The stream is unpaused (via pipe, read, data event, etc.)
  3. _read is called and sets the sendData timeout event
  4. The timeout expires but no data has been put yet
  5. Now, call ReadableStreamBuffer.put(...)
  6. The code in put buffers the data but does not kick the sendData event
  7. Stream hangs

I'd love to get input on a fix before I make a pull request.

mvachhar added a commit to mvachhar/node-stream-buffer that referenced this issue Apr 3, 2018
mvachhar added a commit to mvachhar/node-stream-buffer that referenced this issue Apr 3, 2018
samcday added a commit that referenced this issue May 21, 2018
Fix Issue #30 where ReadableStreamBuffer stalls in certain cases.
@samcday
Copy link
Owner

samcday commented May 21, 2018

3.0.2 has been released with the fix from @mvachhar (thanks!)

@genrym
Copy link

genrym commented Aug 31, 2018

I think this issue can be marked as resolved :)

genrym pushed a commit to genrym/streamable-buffers that referenced this issue Aug 31, 2018
where ReadableStreamBuffer stalls in certain cases.
@mvachhar
Copy link
Contributor

mvachhar commented Aug 31, 2018 via email

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

No branches or pull requests

6 participants