Skip to content

Commit 3c2f607

Browse files
committed
Explicitly close the connection pool using at_exit
This fixes the `zlib(finalizer): the stream was freed prematurely` warning that is printed by net-ssh when Ruby exits without closing the SSH connection first.
1 parent 6a94cdc commit 3c2f607

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ appear at the top.
77

88
* Add your entries below here, remember to credit yourself however you want
99
to be credited!
10+
* SSHKit now explicitly closes its pooled SSH connections when Ruby exits;
11+
this fixes `zlib(finalizer): the stream was freed prematurely` warnings
12+
[PR #343](https://github.com/capistrano/sshkit/pull/343) @mattbrictson
1013
* Allow command map entries (`SSHKit::CommandMap#[]`) to be Procs
1114
[PR #310]((https://github.com/capistrano/sshkit/pull/310)
1215
@mikz

lib/sshkit/backends/netssh.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'English'
12
require 'net/ssh'
23
require 'net/scp'
34

@@ -44,7 +45,12 @@ def download!(remote, local=nil, options = {})
4445
end
4546
end
4647

48+
# Note that this pool must be explicitly closed before Ruby exits to
49+
# ensure the underlying IO objects are properly cleaned up. We register an
50+
# at_exit handler to do this automatically, as long as Ruby is exiting
51+
# cleanly (i.e. without an exception).
4752
@pool = SSHKit::Backend::ConnectionPool.new
53+
at_exit { @pool.close_connections if @pool && !$ERROR_INFO }
4854

4955
class << self
5056
attr_accessor :pool

0 commit comments

Comments
 (0)