net-ssh / net-sftp

Pure Ruby implementation of an SFTP (protocols 1-6) client.
http://net-ssh.github.io/
MIT License
287 stars 131 forks source link

undefined method `send_data' for nil:NilClass #94

Open kapso opened 5 years ago

kapso commented 5 years ago

Null check is missing, I keep seeing this error.

/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:814:in `send_packet'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/protocol/base.rb:45:in `send_request'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/protocol/01/base.rb:96:in `close'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:830:in `request'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:208:in `close'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:217:in `close!'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/operations/file.rb:47:in `close'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/operations/file_factory.rb:47:in `ensure in open'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/operations/file_factory.rb:47:in `open'
/app/app/models/concerns/package_concern/on_tracable.rb:151:in `block in on_trac_status_update'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:939:in `block in do_version'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:939:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:939:in `do_version'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:909:in `when_channel_polled'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/channel.rb:323:in `process'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:250:in `block in ev_preprocess'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:540:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:540:in `each_channel'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:250:in `ev_preprocess'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/event_loop.rb:101:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/event_loop.rb:101:in `ev_preprocess'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/event_loop.rb:29:in `process'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:228:in `process'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:181:in `block in loop'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:181:in `loop'
/app/vendor/bundle/ruby/2.6.0/gems/net-ssh-5.2.0/lib/net/ssh/connection/session.rb:181:in `loop'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:802:in `loop'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:787:in `connect!'
/app/vendor/bundle/ruby/2.6.0/gems/net-sftp-2.1.2/lib/net/sftp.rb:32:in `start'
asheynkman commented 5 years ago

seeing this as well, where there any resolutions on this?

ryandunlavy commented 4 years ago

I'm seeing this as well

kmeyerhofer commented 3 years ago

I was experiencing this issue when attempting to iterate over all files within a directory. I was able to connect to the server, upload! files and open directories, but if I attempted to utilize the Net::SFTP::Operations::Dir methods (#entries, #foreach), on one specific directory, I would receive this NoMethodError (undefined method `send_data' for nil:NilClass) exception.

When I first saw this error, I was using net-sftp version 2.1.2 on Ruby 2.7.2. I upgraded the gem to 3.0.0 with no change in behavior.

Here is my debug: verbose output for this connection attempt:

Net::SFTP.start(credentials.hostname, credentials.username, { password: credentials.password, port: credentials.sftp_port, timeout: 60, append_all_supported_algorithms: true, verbose: :debug, keepalive: true, keepalive_interval: 15, non_interactive: true }) { |sftp| sftp.dir.entries('/Folder') { |entry| puts entry.name } }
D, [2021-02-25T16:58:03.996420 #13] DEBUG -- net.ssh.transport.session[56e00]: establishing connection to <HOSTNAME>:<PORT>
D, [2021-02-25T16:58:04.186420 #13] DEBUG -- net.ssh.transport.session[56e00]: connection established
I, [2021-02-25T16:58:04.186524 #13]  INFO -- net.ssh.transport.server_version[56e14]: negotiating protocol version
D, [2021-02-25T16:58:04.186544 #13] DEBUG -- net.ssh.transport.server_version[56e14]: local is `SSH-2.0-Ruby/Net::SSH_6.1.0 x86_64-linux'
D, [2021-02-25T16:58:04.270073 #13] DEBUG -- net.ssh.transport.server_version[56e14]: remote is `SSH-2.0-SFTP Server Ready'
I, [2021-02-25T16:58:04.270376 #13]  INFO -- net.ssh.transport.algorithms[56e28]: sending KEXINIT
D, [2021-02-25T16:58:04.270561 #13] DEBUG -- socket[56e3c]: queueing packet nr 0 type 20 len 1332
D, [2021-02-25T16:58:04.270686 #13] DEBUG -- socket[56e3c]: sent 1336 bytes
D, [2021-02-25T16:58:04.271255 #13] DEBUG -- socket[56e3c]: read 744 bytes
D, [2021-02-25T16:58:04.271373 #13] DEBUG -- socket[56e3c]: received packet nr 0 type 20 len 740
I, [2021-02-25T16:58:04.271485 #13]  INFO -- net.ssh.transport.algorithms[56e28]: got KEXINIT from server
I, [2021-02-25T16:58:04.271772 #13]  INFO -- net.ssh.transport.algorithms[56e28]: negotiating algorithms
D, [2021-02-25T16:58:04.272126 #13] DEBUG -- net.ssh.transport.algorithms[56e28]: negotiated:
* kex: ecdh-sha2-nistp521
* host_key: ssh-rsa
* encryption_server: aes256-cbc
* encryption_client: aes256-cbc
* hmac_client: hmac-sha2-512-etm@openssh.com
* hmac_server: hmac-sha2-512-etm@openssh.com
* compression_client: none
* compression_server: none
* language_client: 
* language_server: 
D, [2021-02-25T16:58:04.272178 #13] DEBUG -- net.ssh.transport.algorithms[56e28]: exchanging keys
D, [2021-02-25T16:58:04.272824 #13] DEBUG -- socket[56e3c]: queueing packet nr 1 type 30 len 148
D, [2021-02-25T16:58:04.272867 #13] DEBUG -- socket[56e3c]: sent 152 bytes
D, [2021-02-25T16:58:04.572014 #13] DEBUG -- socket[56e3c]: read 712 bytes
D, [2021-02-25T16:58:04.572154 #13] DEBUG -- socket[56e3c]: received packet nr 1 type 31 len 708
D, [2021-02-25T16:58:04.573747 #13] DEBUG -- socket[56e3c]: queueing packet nr 2 type 21 len 20
D, [2021-02-25T16:58:04.573833 #13] DEBUG -- socket[56e3c]: sent 24 bytes
D, [2021-02-25T16:58:04.573980 #13] DEBUG -- socket[56e3c]: read 16 bytes
D, [2021-02-25T16:58:04.574151 #13] DEBUG -- socket[56e3c]: received packet nr 2 type 21 len 12
D, [2021-02-25T16:58:04.574470 #13] DEBUG -- net.ssh.authentication.session[56e50]: beginning authentication of <USERNAME>
D, [2021-02-25T16:58:04.574622 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:04.574737 #13] DEBUG -- socket[56e3c]: queueing packet nr 3 type 5 len 32
D, [2021-02-25T16:58:04.574780 #13] DEBUG -- socket[56e3c]: sent 100 bytes
D, [2021-02-25T16:58:04.777269 #13] DEBUG -- socket[56e3c]: read 100 bytes
D, [2021-02-25T16:58:04.777468 #13] DEBUG -- socket[56e3c]: received packet nr 3 type 6 len 32
D, [2021-02-25T16:58:04.777610 #13] DEBUG -- net.ssh.authentication.session[56e50]: trying none
D, [2021-02-25T16:58:04.777948 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:04.778176 #13] DEBUG -- socket[56e3c]: queueing packet nr 4 type 50 len 48
D, [2021-02-25T16:58:04.778406 #13] DEBUG -- socket[56e3c]: sent 116 bytes
D, [2021-02-25T16:58:04.860731 #13] DEBUG -- socket[56e3c]: read 100 bytes
D, [2021-02-25T16:58:04.860877 #13] DEBUG -- socket[56e3c]: received packet nr 4 type 51 len 32
D, [2021-02-25T16:58:04.861026 #13] DEBUG -- net.ssh.authentication.session[56e50]: allowed methods: password
D, [2021-02-25T16:58:04.861095 #13] DEBUG -- net.ssh.authentication.methods.none[56e64]: none failed
D, [2021-02-25T16:58:04.861156 #13] DEBUG -- net.ssh.authentication.session[56e50]: trying password
D, [2021-02-25T16:58:04.861300 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:04.861369 #13] DEBUG -- socket[56e3c]: queueing packet nr 5 type 50 len 96
D, [2021-02-25T16:58:04.861440 #13] DEBUG -- socket[56e3c]: sent 164 bytes
D, [2021-02-25T16:58:04.958536 #13] DEBUG -- socket[56e3c]: read 84 bytes
D, [2021-02-25T16:58:04.958693 #13] DEBUG -- socket[56e3c]: received packet nr 5 type 52 len 16
D, [2021-02-25T16:58:04.958794 #13] DEBUG -- net.ssh.authentication.methods.password[56e78]: password succeeded
D, [2021-02-25T16:58:04.958970 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:04.959034 #13] DEBUG -- socket[56e3c]: queueing packet nr 6 type 90 len 32
D, [2021-02-25T16:58:04.959191 #13] DEBUG -- socket[56e3c]: sent 100 bytes
D, [2021-02-25T16:58:05.041066 #13] DEBUG -- socket[56e3c]: read 100 bytes
D, [2021-02-25T16:58:05.041274 #13] DEBUG -- socket[56e3c]: received packet nr 6 type 91 len 32
I, [2021-02-25T16:58:05.041399 #13]  INFO -- net.ssh.connection.session[56e8c]: channel_open_confirmation: 0 0 0 34000
I, [2021-02-25T16:58:05.041640 #13]  INFO -- net.ssh.connection.channel[56ea0]: sending channel request "env"
D, [2021-02-25T16:58:05.041739 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:05.041815 #13] DEBUG -- socket[56e3c]: queueing packet nr 7 type 98 len 48
D, [2021-02-25T16:58:05.042034 #13] DEBUG -- net.sftp.session[56eb4]: requesting sftp subsystem
I, [2021-02-25T16:58:05.042067 #13]  INFO -- net.ssh.connection.channel[56ea0]: sending channel request "subsystem"
D, [2021-02-25T16:58:05.042133 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:05.042184 #13] DEBUG -- socket[56e3c]: queueing packet nr 8 type 98 len 32
D, [2021-02-25T16:58:05.042317 #13] DEBUG -- socket[56e3c]: sent 216 bytes
D, [2021-02-25T16:58:05.970192 #13] DEBUG -- socket[56e3c]: read 168 bytes
D, [2021-02-25T16:58:05.970424 #13] DEBUG -- socket[56e3c]: received packet nr 7 type 93 len 16
I, [2021-02-25T16:58:05.970509 #13]  INFO -- net.ssh.connection.session[56e8c]: channel_window_adjust: 0 +2097152
D, [2021-02-25T16:58:05.970577 #13] DEBUG -- socket[56e3c]: received packet nr 8 type 99 len 16
I, [2021-02-25T16:58:05.970626 #13]  INFO -- net.ssh.connection.session[56e8c]: channel_success: 0
D, [2021-02-25T16:58:05.970655 #13] DEBUG -- net.sftp.session[56eb4]: sftp subsystem successfully started
D, [2021-02-25T16:58:05.970807 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:05.970871 #13] DEBUG -- socket[56e3c]: queueing packet nr 9 type 94 len 32
D, [2021-02-25T16:58:05.970980 #13] DEBUG -- socket[56e3c]: sent 100 bytes
D, [2021-02-25T16:58:06.054089 #13] DEBUG -- socket[56e3c]: read 196 bytes
D, [2021-02-25T16:58:06.054274 #13] DEBUG -- socket[56e3c]: received packet nr 9 type 94 len 128
I, [2021-02-25T16:58:06.054340 #13]  INFO -- net.ssh.connection.session[56e8c]: channel_data: 0 111b
D, [2021-02-25T16:58:06.054414 #13] DEBUG -- net.sftp.session[56eb4]: received sftp packet 2 len 107
D, [2021-02-25T16:58:06.054438 #13] DEBUG -- net.sftp.session[56eb4]: negotiating sftp protocol version, mine is 6
D, [2021-02-25T16:58:06.054458 #13] DEBUG -- net.sftp.session[56eb4]: server reports sftp version 3
I, [2021-02-25T16:58:06.054479 #13]  INFO -- net.sftp.session[56eb4]: negotiated version is 3
I, [2021-02-25T16:58:06.054582 #13]  INFO -- net.sftp.session[56eb4]: sending opendir packet (0)
D, [2021-02-25T16:58:06.054676 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:06.054729 #13] DEBUG -- socket[56e3c]: queueing packet nr 10 type 94 len 48
D, [2021-02-25T16:58:06.223727 #13] DEBUG -- net.sftp.session[56eb4]: received sftp packet 102 len 10
I, [2021-02-25T16:58:06.223919 #13]  INFO -- net.ssh.connection.keepalive[56ec8]: sending keepalive 0
I, [2021-02-25T16:58:06.223988 #13]  INFO -- net.ssh.connection.session[56e8c]: sending global request keepalive@openssh.com
D, [2021-02-25T16:58:06.224287 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:06.224517 #13] DEBUG -- socket[56e3c]: queueing packet nr 11 type 80 len 32
I, [2021-02-25T16:58:06.224762 #13]  INFO -- net.sftp.session[56eb4]: sending readdir packet (1)
D, [2021-02-25T16:58:06.224988 #13] DEBUG -- socket[56e3c]: using encrypt-then-mac
D, [2021-02-25T16:58:06.225077 #13] DEBUG -- socket[56e3c]: queueing packet nr 12 type 94 len 32
D, [2021-02-25T16:58:06.225298 #13] DEBUG -- socket[56e3c]: sent 200 bytes
D, [2021-02-25T16:58:06.307542 #13] DEBUG -- socket[56e3c]: read 84 bytes
D, [2021-02-25T16:58:06.307903 #13] DEBUG -- socket[56e3c]: received packet nr 11 type 81 len 16
I, [2021-02-25T16:58:06.307988 #13]  INFO -- net.ssh.connection.session[56e8c]: global request success
D, [2021-02-25T16:58:06.308229 #13] DEBUG -- net.ssh.connection.keepalive[56ec8]: keepalive response successful. Missed 0 keepalives
D, [2021-02-25T16:58:06.831598 #13] DEBUG -- socket[56e3c]: read 116 bytes
D, [2021-02-25T16:58:06.831785 #13] DEBUG -- socket[56e3c]: received packet nr 12 type 1 len 48
D, [2021-02-25T16:58:06.831948 #13] DEBUG -- socket[56e3c]: read 0 bytes
D, [2021-02-25T16:58:06.832078 #13] DEBUG -- net.sftp.session[56eb4]: sftp channel closed
D, [2021-02-25T16:58:06.832294 #13] DEBUG -- net.sftp.session[56eb4]: sftp channel closed
D, [2021-02-25T16:58:06.832392 #13] DEBUG -- net.sftp.session[56eb4]: sftp channel closed
E, [2021-02-25T16:58:06.832512 #13] ERROR -- net.ssh.transport.session[56e00]: forcing connection closed
Traceback (most recent call last):
        3: from (irb):9
        2: from (irb):10:in `rescue in irb_binding'
        1: from (irb):10:in `block in irb_binding'
NoMethodError (undefined method `send_data' for nil:NilClass)
Did you mean?  send_at
               send_later

Since this issue was only affecting one directory, I suspected it had to do with the number of files within it. I resolved this issue by removing hundreds of files on the remote server (through a web portal) that I didn't need. When the file count within the directory was reduced, I was able to use the Net::SFTP::Operations::Dir methods successfully.

I hope this helps someone.

adrianodennanni commented 3 years ago

I fixed the same problem by limiting the concurrency of upload! method to 1.

mayuresh-srivastava commented 1 year ago

I fixed it as below:

Replaced:

sftp.file.open(sftp_file_name, 'w') do |f| f.puts output end

With:

string_io = StringIO.new(output) sftp.upload!(string_io, sftp_file_name)

Source: Stackoverflow