Invalid Zlib Instance uncatchable error
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 5.8k
- Forks
- 734
- PR merge metrics
- No merged PRs in 30d
Description
I'm currently using this module through the ssh2-sftp-client module (using the latest version, v8.0.0) and ran across this sporadic error:
Error: Invalid Zlib instance
at Zlib.writeSync (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/zlib.js:73:13)
at ZlibPacketWriter.finalize (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/zlib.js:190:33)
at Protocol.channelData (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/Protocol.js:462:43)
at tryWritePayload (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:2523:22)
at sendOrBuffer (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:2491:15)
at SFTP.close (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:397:24)
at onerror (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:2098:13)
at writeCb (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:2178:22)
at Object.cb (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:478:13)
at cleanupRequests (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:2584:11)
at SFTP.push (.../ssh2-sftp-client/node_modules/ssh2/lib/protocol/SFTP.js:191:7)
at onCHANNEL_CLOSE (.../ssh2-sftp-client/node_modules/ssh2/lib/utils.js:50:13)
at ChannelManager.cleanup (.../ssh2-sftp-client/node_modules/ssh2/lib/utils.js:200:7)
at Socket.<anonymous> (.../ssh2-sftp-client/node_modules/ssh2/lib/client.js:769:21)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:475:12)
It looks like this lib is not gracefully handling disconnections with pending SFTP requests. In our current usage, we'll send a few files in parallel and if we get an error somehow (SFTP related or not), we'll end the client via SFTP client end() method. In its first appearance, I started waiting for the currently running fastPut/put calls before propagating the error expecting this issue would be gone but it still happens.
After checking the related code and following the stack trace, I figured it may be related to how the lib handles disconnections in its pending requests. In the client.end() method, we end its internal socket and in its close event here, we call proto.cleanup() (which nullifies the Zlib instance handle that will be checked later and throw) and this._chanMgr.cleanup(err) afterwards, which will cleanup pending requests with errors, some of which will (incorrectly) try to write data after the connection has been closed, relying on the (now null) handle and throwing this error.
I'd expect the lib would just disregard the remaining read/write requests as it's an intended disconnection and move on but we end up with this uncaught error instead. If I'm missing something and this is the intended behavior, having a way to catch and handle this error would be appreciated.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with lib/protocol/zlib.js and trace the cleanup sequence through lib/client.js, lib/protocol/SFTP.js, and lib/protocol/Protocol.js. Reproduce a disconnect while parallel SFTP requests are pending, then verify that cleanup does not produce an uncaught Invalid Zlib instance error and that pending request errors are handled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100