title: Net::SFTP connection not closing
body: <p>I have a Ruby on Rails (Rails 3.2.14 and ruby 1.9.3) application that uploads 2 files to a remote SFTP server. The SFTP code is:</p>
  
  <pre><code>require 'net/sftp'
  Rails.logger.info("Creating SFTP connection")
  uri = URI.parse('sftp://'+ host)
  Net::SFTP.start(uri.host,'user', :password=&gt;'password',:port=&gt;port) do |sftp|
      Rails.logger.info("SFTP Connection created, uploading files.")
      sftp.upload!("public/file1.txt", "./file1.txt")
      Rails.logger.info("First file uploaded.")
      sftp.upload!("file2.txt", "./file2.txt")
      Rails.logger.info("Both files uploaded, terminating connection.")
  end
  Rails.logger.info("Connection terminated.")
  </code></pre>
  
  <p>Both files are uploading properly to the remote server, but the connection doesn't seem to close. I keep getting an error when I execute this function and on analyzing my console, I see that the "Both files uploaded, terminating connection." logger message is running, but nothing after that. I've tried using</p>
  
  <pre><code>sftp.close(:handle)
  sftp.close!(:handle)
  #and
  sftp.close_connection()
  </code></pre>
  
  <p>but none of them are working. Any idea on why this is happening and how I can rectify it? I'm running this through a single instance Engine Yard cloud server.</p>
  
  <p><strong>EDIT</strong>
  These are the last few lines in my log:
  Creating SFTP connection
  SFTP Connection created, uploading files.
  First file uploaded.
  Both files uploaded, terminating connection.</p>
  
  <p>After that, nothing. When viewing my log with the 'tail -f' command, the log goes up to that last line, and the app redirects to the internal server error page.</p>