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

TypeError: Cannot read property 'unref' of undefined #44

Closed
lacker opened this issue Mar 26, 2019 · 3 comments
Closed

TypeError: Cannot read property 'unref' of undefined #44

lacker opened this issue Mar 26, 2019 · 3 comments
Labels

Comments

@lacker
Copy link

lacker commented Mar 26, 2019

I'm getting an error from within the bittorrent-protocol module when running a simple WebTorrent download.

/home/lacker/js/node_modules/bittorrent-protocol/index.js:617
    if (this._timeoutUnref && this._timeout.unref) this._timeout.unref()
                                            ^

TypeError: Cannot read property 'unref' of undefined
    at Wire._updateTimeout (/home/lacker/js/node_modules/bittorrent-protocol/index.js:617:45)
    at Wire._callback (/home/lacker/js/node_modules/bittorrent-protocol/index.js:602:52)
    at Wire._onPiece (/home/lacker/js/node_modules/bittorrent-protocol/index.js:507:10)
    at Wire._onMessage (/home/lacker/js/node_modules/bittorrent-protocol/index.js:673:21)
    at Wire._write (/home/lacker/js/node_modules/bittorrent-protocol/index.js:591:12)
    at doWrite (/home/lacker/js/node_modules/bittorrent-protocol/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (/home/lacker/js/node_modules/bittorrent-protocol/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at Wire.Object.<anonymous>.Writable.write (/home/lacker/js/node_modules/bittorrent-protocol/node_modules/readable-stream/lib/_stream_writable.js:334:11)
    at Socket.ondata (_stream_readable.js:639:20)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:607:20)

Looks like this._timeoutUnref is set but this._timeout isn't. I'm just creating a client, calling client.add(magnet) on it, and waiting for the "done" event on the response torrent.

@feross
Copy link
Member

feross commented Sep 11, 2019

@lacker What environment are you running this in? It looks like setTimeout() is returning undefined for you, which should not happen. This is the full code:

    this._timeout = setTimeout(() => this._onTimeout(), this._timeoutMs)
    if (this._timeoutUnref && this._timeout.unref) this._timeout.unref()

@lacker
Copy link
Author

lacker commented Sep 11, 2019

Ah, I should have updated this issue. I tracked it down, it's from within a unit test that uses jest, which in turn uses jsdom for its "window" object, during the cleanup phase. The jsdom window setTimeout() returns undefined after the window has been closed, which I believe is incorrect behavior, and I reported this against jsdom here:

jsdom/jsdom#2617

It would be handy just to make webtorrent work within test environments if this code also checked that this._timeout was defined before calling this._timeout.unref(). This does seem to be a bug within jsdom rather than a bug within webtorrent per se, but it seems low-priority for jsdom folks.

@feross
Copy link
Member

feross commented Sep 11, 2019

Thanks for following up, @lacker. We use setTimeout all over the codebase. I'd rather not scatter a hack across the codebase to solve a bug in jsdom. I know this probably isn't what you wanted to hear, but I don't think WebTorrent is the right place to fix this.

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

No branches or pull requests

2 participants