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

Intermittent RangeError: length > kMaxLength #15

Closed
robogeek opened this issue Jan 1, 2013 · 18 comments
Closed

Intermittent RangeError: length > kMaxLength #15

robogeek opened this issue Jan 1, 2013 · 18 comments

Comments

@robogeek
Copy link

robogeek commented Jan 1, 2013

I set up a script using your first example (that runs 'uptime') and configured it for my SSH key to log in to my hosting account on dreamhost. It is intermittently giving an error - and by the way does not print the output from uptime.

tippy:akashacms.com davidherron$ node ../tsync.js 
Connection :: connect

buffer.js:242
      this.parent = new SlowBuffer(this.length);
                    ^
RangeError: length > kMaxLength
    at new Buffer (buffer.js:242:21)
    at Parser.expect (/Users/davidherron/node_modules/ssh2/lib/Parser.js:680:25)
    at Parser.execute (/Users/davidherron/node_modules/ssh2/lib/Parser.js:203:16)
    at Socket.Connection.connect._sock.once.err.level (/Users/davidherron/node_modules/ssh2/lib/Connection.js:1186:18)
    at Socket.EventEmitter.emit (events.js:93:17)
    at TCP.onread (net.js:396:14)
tippy:akashacms.com davidherron$ node ../tsync.js 
Connection :: connect
Connection :: end
Connection :: close

Client machine is Mac OS X

$ uname -a
Darwin tippy.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

Server machine is Dreamhosts version of Debian

$ ssh -l user example.com uname -a
Linux cardinals 2.6.32.36-grsec #1 SMP Sun Apr 3 00:02:25 PDT 2011 x86_64 GNU/Linux
@mscdex
Copy link
Owner

mscdex commented Jan 1, 2013

node version? ssh2 module version? OpenSSH version?

@mscdex
Copy link
Owner

mscdex commented Jan 1, 2013

Also, what kind of key is this? # bits?

@robogeek
Copy link
Author

robogeek commented Jan 1, 2013

$ node --version
v0.8.12
  "name": "ssh2",
  "version": "0.1.9",
$ ssh -v
OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011
  privateKey: require('fs').readFileSync('/Users/davidherron/.ssh/id_rsa')

How do I tell you more about the key?

@mscdex
Copy link
Owner

mscdex commented Jan 1, 2013

I'll keep digging, but try the master branch now and set debug: console.log in the object to connect(). Maybe this will help narrow it down somewhat.

@robogeek
Copy link
Author

robogeek commented Jan 1, 2013

I'm sorry, the ssh version I gave above is for my laptop.. here's the output for my server

$ ssh -l user server.com ssh -v
OpenSSH_5.1p1 Debian-5, OpenSSL 0.9.8g 19 Oct 2007

@robogeek
Copy link
Author

robogeek commented Jan 1, 2013

Using the ssh2 version mentioned above, and with debug:console.log I get the following output from two runs. The first gave no error (but no output from uptime) the second gave the error

tippy:boilerplate davidherron$ node tsync.js 
Connection :: connect
header { greeting: null,
  ident_raw: 'SSH-2.0-OpenSSH_5.1p1 Debian-5',
  versions: { protocol: '2.0', server: 'OpenSSH_5.1p1' },
  comments: 'Debian-5' }
packet KEXINIT 20 <Buffer 43 30 95 90 13 91 90 cd 68 56 d0 8c 32 85 71 b7 00 00 00 7e 64 69 66 66 69 65 2d 68 65 6c 6c 6d 61 6e 2d 67 72 6f 75 70 2d 65 78 63 68 61 6e 67 65 2d 73 ...>
packet KEXDH_REPLY 31 <Buffer 00 00 01 15 00 00 00 07 73 73 68 2d 72 73 61 00 00 00 01 23 00 00 01 01 00 d6 78 a4 56 2b 92 d0 17 a1 9f c2 c0 df de 17 36 2c 24 79 fa 76 23 54 5f 03 46 ...>
packet NEWKEYS 21 <Buffer >
Connection :: end
Connection :: close
tippy:boilerplate davidherron$ node tsync.js 
Connection :: connect
header { greeting: null,
  ident_raw: 'SSH-2.0-OpenSSH_5.1p1 Debian-5',
  versions: { protocol: '2.0', server: 'OpenSSH_5.1p1' },
  comments: 'Debian-5' }
packet KEXINIT 20 <Buffer 6c f6 3d 26 3e 6a 31 6f 4b 93 6e 7c be cb 5b d8 00 00 00 7e 64 69 66 66 69 65 2d 68 65 6c 6c 6d 61 6e 2d 67 72 6f 75 70 2d 65 78 63 68 61 6e 67 65 2d 73 ...>
packet KEXDH_REPLY 31 <Buffer 00 00 01 15 00 00 00 07 73 73 68 2d 72 73 61 00 00 00 01 23 00 00 01 01 00 d6 78 a4 56 2b 92 d0 17 a1 9f c2 c0 df de 17 36 2c 24 79 fa 76 23 54 5f 03 46 ...>
packet NEWKEYS 21 <Buffer >

buffer.js:242
      this.parent = new SlowBuffer(this.length);
                    ^
RangeError: length > kMaxLength
    at new Buffer (buffer.js:242:21)
    at Parser.expect (/Users/davidherron/node_modules/ssh2/lib/Parser.js:680:25)
    at Parser.execute (/Users/davidherron/node_modules/ssh2/lib/Parser.js:203:16)
    at Socket.Connection.connect._sock.once.err.level (/Users/davidherron/node_modules/ssh2/lib/Connection.js:1186:18)
    at Socket.EventEmitter.emit (events.js:93:17)
    at TCP.onread (net.js:396:14)

@robogeek
Copy link
Author

robogeek commented Jan 1, 2013

I have now checked out the git repository into my node_modules to get the HEAD version as you said, and rerunning the app I get this debug output

tippy:boilerplate davidherron$ node tsync.js 
Connection :: connect
DEBUG: Parser: STATE_INIT
DEBUG: Parser: STATE_GREETING
DEBUG: Parser: STATE_HEADER
DEBUG: Parser: STATE_PACKETBEFORE
DEBUG: Parser: STATE_PACKET
DEBUG: Parser: STATE_PACKETDATA
DEBUG: Parser: STATE_PACKETDATAAFTER, packet: KEXINIT
DEBUG: Parser: STATE_PACKETBEFORE
DEBUG: Parser: STATE_PACKET
DEBUG: Parser: STATE_PACKETDATA
DEBUG: Parser: STATE_PACKETDATAAFTER, packet: KEXDH_REPLY
DEBUG: Parser: STATE_PACKETBEFORE
DEBUG: Parser: STATE_PACKET
DEBUG: Parser: STATE_PACKETDATA
DEBUG: Parser: STATE_PACKETDATAAFTER, packet: NEWKEYS
DEBUG: Parser: STATE_PACKETBEFORE
DEBUG: Parser: STATE_PACKET

buffer.js:242
      this.parent = new SlowBuffer(this.length);
                    ^
RangeError: length > kMaxLength
    at new Buffer (buffer.js:242:21)
    at Parser.expect (/Users/davidherron/node_modules/ssh2/lib/Parser.js:690:25)
    at Parser.execute (/Users/davidherron/node_modules/ssh2/lib/Parser.js:209:16)
    at Socket.Connection.connect._sock.once.err.level (/Users/davidherron/node_modules/ssh2/lib/Connection.js:1182:18)
    at Socket.EventEmitter.emit (events.js:93:17)
    at TCP.onread (net.js:396:14)

@robogeek
Copy link
Author

robogeek commented Jan 1, 2013

FWIW I notice you test against OpenSSH 6 .. but the OpenSSH on both my laptop and server are 5.x ...

@mscdex
Copy link
Owner

mscdex commented Jan 2, 2013

Alright, so I've tried everything I can to duplicate this problem on my end (including installing Debian 5) but I cannot get it to break at all. I've even tried bandwidth limiting and latency simulation.

Would it be possible for you to hop on IRC sometime to coordinate a way for me to remotely debug?

@mscdex mscdex closed this as completed Jan 2, 2013
@mscdex mscdex reopened this Jan 2, 2013
@ghost
Copy link

ghost commented Jan 9, 2013

I also encountered a RangeError: length > kMaxLength and in my case it was consistent, and caused by my attempt to exec immediately upon the connect event. I suspect the connect event fires too soon but for my use don't care enough to investigate further. But I wanted to share my learnings in case it was helpful to ya'all.

Here is the code I was using:

var c, ssh2;

ssh2 = require('ssh2');

c = new ssh2();

c.on('connect', function() {
  console.log('CONNECTED');
  return c.exec('uptime', function(err, stream) {
    if (err) {
      throw err;
    }
    return stream.on('data', function(data) {
      return console.log(data.toString());
    });
  });
});

c.on('error', function(err) {
  console.log('ERROR', err);
  throw err;
  return done();
});

c.connect({
  host: '192.168.55.101',
  port: 22,
  username: 'vagrant',
  password: 'vagrant',
  debug: console.log
});

Here is that code's output with debug set to console.log on connect:

vagrant@app:/var/www/martini$ ./node_modules/.bin/coffee test.coffee 
CONNECTED
header { greeting: null,
  ident_raw: 'SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1',
  versions: { protocol: '2.0', server: 'OpenSSH_6.0p1' },
  comments: 'Debian-3ubuntu1' }

buffer.js:242
      this.parent = new SlowBuffer(this.length);
                    ^
RangeError: length > kMaxLength
    at new Buffer (buffer.js:242:21)
    at Parser.expect (/var/www/martini/node_modules/ssh2/lib/Parser.js:680:25)
    at Parser.execute (/var/www/martini/node_modules/ssh2/lib/Parser.js:203:16)
    at Socket.Connection.connect._sock.once.err.level (/var/www/martini/node_modules/ssh2/lib/Connection.js:1186:18)
    at Socket.EventEmitter.emit (events.js:96:17)
    at TCP.onread (net.js:397:14)

Here is the modified code with a settimeout that "fixed" the problem for me:

var c, ssh2;

ssh2 = require('ssh2');

c = new ssh2();

c.on('connect', function() {
  console.log('CONNECTED');
  return setTimeout(function() {
    return c.exec('uptime', function(err, stream) {
      if (err) {
        throw err;
      }
      return stream.on('data', function(data) {
        return console.log(data.toString());
      });
    });
  }, 500);
});

c.on('error', function(err) {
  console.log('ERROR', err);
  throw err;
  return done();
});

c.connect({
  host: '192.168.55.101',
  port: 22,
  username: 'vagrant',
  password: 'vagrant',
  debug: console.log
});

And the modified code's output with debug set to console.log on connect:

vagrant@app:/var/www/martini$ ./node_modules/.bin/coffee test.coffee 
CONNECTED
header { greeting: null,
  ident_raw: 'SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1',
  versions: { protocol: '2.0', server: 'OpenSSH_6.0p1' },
  comments: 'Debian-3ubuntu1' }
packet KEXINIT 20 <Buffer b9 c4 ab 9e 13 fa 73 45 6e eb 51 60 6e 44 ae c2 00 00 00 b7 65 63 64 68 2d 73 68 61 32 2d 6e 69 73 74 70 32 35 36 2c 65 63 64 68 2d 73 68 61 32 2d 6e 69 ...>
packet KEXDH_REPLY 31 <Buffer 00 00 01 17 00 00 00 07 73 73 68 2d 72 73 61 00 00 00 03 01 00 01 00 00 01 01 00 e2 a8 95 ae 26 26 d7 a7 74 63 16 2a ca 8c 6e 8c 4c c1 bf a4 5e 93 86 7e ...>
packet NEWKEYS 21 <Buffer >
packet SERVICE_ACCEPT 6 <Buffer 00 00 00 0c 73 73 68 2d 75 73 65 72 61 75 74 68>
packet USERAUTH_SUCCESS 52 <Buffer >
packet CHANNEL_OPEN_CONFIRMATION 91 <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00>
packet CHANNEL_WINDOW_ADJUST 93 <Buffer 00 00 00 00 00 20 00 00>
packet CHANNEL_SUCCESS 99 <Buffer 00 00 00 00>
packet CHANNEL_DATA 94 <Buffer 00 00 00 00 00 00 00 3e 20 32 30 3a 30 38 3a 35 32 20 75 70 20 31 30 3a 32 36 2c 20 20 30 20 75 73 65 72 73 2c 20 20 6c 6f 61 64 20 61 76 65 72 61 67 65 ...>
 20:08:52 up 10:26,  0 users,  load average: 0.10, 0.06, 0.06

packet CHANNEL_EOF 96 <Buffer 00 00 00 00>
packet CHANNEL_REQUEST 98 <Buffer 00 00 00 00 00 00 00 0b 65 78 69 74 2d 73 74 61 74 75 73 00 00 00 00 00>
packet CHANNEL_CLOSE 97 <Buffer 00 00 00 00>

@mscdex
Copy link
Owner

mscdex commented Jan 9, 2013

@proksoup you should not use the 'connect' event for issuing commands, it's merely there for if you want to do your own connection timeout algorithm. You need to listen for the 'ready' event instead.

@robogeek
Copy link
Author

I noticed you updated the package version in npm, so I updated and reran the script. Now it works fine and even prints out the data from the server.

@mscdex
Copy link
Owner

mscdex commented Jan 13, 2013

Glad to hear it's working for you now! :-)

@ghost
Copy link

ghost commented Jan 16, 2013

I am having this issue as well, I have followed what you said in this thread, and am able to replace it every time. Also seeing duplicate connections generate for some reason when it happens. Where would we be able to chat on irc?

@ghost
Copy link

ghost commented Jan 16, 2013

node -v
v0.8.17

ssh -v
OpenSSH_6.1p1, OpenSSL 1.0.1c 10 May 2012

ssh2 - 0.1.11

output happening:
Connecting to prod-con-lb3
Connecting to prod-con-lb4
Connecting to prod-con-lb3
Connecting to prod-con-lb4
DEBUG: Parser: STATE_INIT
DEBUG: Parser: STATE_GREETING
DEBUG: Parser: STATE_HEADER
DEBUG: Parser: STATE_PACKETBEFORE (expecting 8)
DEBUG: Parser: STATE_PACKET
DEBUG: Parser: remainLen === 1397966889

buffer.js:242
this.parent = new SlowBuffer(this.length);
^
RangeError: length > kMaxLength
at new Buffer (buffer.js:242:21)
at Parser.expect (/Users/jonathanmickle/Desktop/sysops/sysops/jmickle/deploy/node_modules/ssh2/lib/Parser.js:689:23)
at Parser.execute (/Users/jonathanmickle/Desktop/sysops/sysops/jmickle/deploy/node_modules/ssh2/lib/Parser.js:197:16)
at Socket.Connection.connect._sock.once.err.level (/Users/jonathanmickle/Desktop/sysops/sysops/jmickle/deploy/node_modules/ssh2/lib/Connection.js:1189:18)
at Socket.EventEmitter.emit (events.js:96:17)
at TCP.onread (net.js:397:14)

I am not sure why its doing two connections to each server or why this is happening

@ghost
Copy link

ghost commented Jan 16, 2013

exports.connect = function (hostname, command, onReady) {

  conn.connect ({
    host: hostname,
    port: port,
    username: userName,
    privateKey: privateKey,
    debug: console.log
  });

  conn.on('connect', function(){
    console.log('Connecting to ' + hostname);
  });

  conn.on('ready', function() {

    conn.exec(command, function(err, stream) {
      if (err) throw err;
      stream.on('data', function(data, extended){
        onReady(null, data);
      });

        stream.on('error', function(err) {
          console.log('TCP :: ERROR: ' + err);
        });

        stream.on('exit', function(code, signal){
          if (code != 0) throw new Error('Exit code not 0 from: ' + hostname);
          conn.end();
        });
    });
  });

  conn.on('error', function(err) {
    console.log('Connection :: error :: ' + err);
  });
};

@ghost
Copy link

ghost commented Jan 16, 2013

it simply gets called twice right now, 1 time for each server passing in hostname and command 'uptime'

@ghost
Copy link

ghost commented Jan 16, 2013

D'OH Ive made my corrections, appears to be working now sorry and thanks for the help

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

2 participants