8000 use setImmediate in peek by max-mapper · Pull Request #2 · dominictarr/level-peek · GitHub
[go: up one dir, main page]

Skip to content

use setImmediate in peek - #2

Open
max-mapper wants to merge 1 commit into
dominictarr:masterfrom
max-mapper:setimmediate
Open

max-mapper wants to merge 1 commit into
dominictarr:masterfrom
max-mapper:setimmediate

Conversation

@max-mapper
Copy link
Copy Markdown

this fixes a bug where I was destroying things in the callback of a peek() which was mistakenly causing the peek error event to get called

@dominictarr
Copy link
Copy Markdown
Owner

hmm, can you show me a stacktrace this produced, there may be a better place to put this fix.

@max-mapper
Copy link
Copy Markdown
Author

sure:

/Users/max/src/js/dat/node_modules/tape/index.js:51
        throw err
              ^
TypeError: Cannot call method 'toString' of undefined
    at /Users/max/src/js/dat/node_modules/level-mutex/node_modules/level-peek/index.js:32:22
    at Stream.onEvent (/Users/max/src/js/dat/node_modules/level-mutex/node_modules/level-peek/index.js:12:10)
    at Stream.EventEmitter.emit (events.js:95:17)
    at Stream.<anonymous> (/Users/max/src/js/dat/node_modules/multilevel/node_modules/tmp-stream/index.js:42:10)
    at Stream.EventEmitter.emit (events.js:117:20)
    at destroyAll (/Users/max/src/js/dat/node_modules/multilevel/node_modules/mux-demux/inject.js:80:11)
    at Stream.<anonymous> (/Users/max/src/js/dat/node_modules/multilevel/node_modules/mux-demux/inject.js:65:5)
    at Stream.EventEmitter.emit (events.js:117:20)
    at Stream.stream.end (/Users/max/src/js/dat/node_modules/multilevel/node_modules/mux-demux/node_modules/duplex/index.js:76:12)
    at Stream.onend (stream.js:79:10)

@max-mapper
Copy link
Copy Markdown
Author

now that you mention it I think it should go in mux-demux or multilevel

@dominictarr
Copy link
Copy Markdown
Owner

my guess is there is a try...catch around an emit('data') or something like that, but it should just let the error throw.

@dominictarr
Copy link
Copy Markdown
Owner

do you have a test case that reproduces this?
I notice that the top line of the stack-trace is tape.

hmm, so basically, this is being triggered, which happens when the stream is ended, but there are inner stream that havn't been closed yet. Can you think what might be closing the websocket(? or whatever sort of stream it is)

https://github.com/dominictarr/mux-demux/blob/master/inject.js#L73

@max-mapper
Copy link
Copy Markdown
Author

yea I'm closing the connection in my test: https://github.com/maxogden/dat/blob/master/test/tests/init.js#L130 (in the cleanup function)

I did notice that in multilevel you can't access or pass options in to rpcStream here https://github.com/juliangruber/multilevel/blob/master/lib/client.js#L51 which is what is emitting the error from inject.js in your last comment

@max-mapper
max-mapper commented 8000 Jan 25, 2014
Copy link
Copy Markdown
Author

p.s. if you take the setImmediate out of that test it will throw an error

@dominictarr
Copy link
Copy Markdown
Owner

multilevel uses that mux-demux setting so that if any callbacks are in progress when the other stream closes they will callback an error.

Ah, I think I see it. I think if you connection.end() rather than destroy it might allow the last messages to get through - level-peek uses a very short stream - and the end message should arrive immediately after the peeked value, except that you are calling destroy syncly, after the value, so it's aborting the stream,
and then mux-demux thinks it's been cut off, so it emits an error (correctly).

try calling end otherwise just use setImmediate in your tests, like you have now.
I don't think you'll encounter this problem in production. since you most surely won't call dat.close().

https://github.com/maxogden/dat/blob/master/lib/commands.js#L322-L329

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

Successfully merging this pull request may close these issues.

2 participants

0