Skip to content

Commit be19290

Browse files
committed
Change finalizer for closing HTTP connections to a class method.
See https://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
1 parent 0ce3b32 commit be19290

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/sparql/client.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def initialize(url, **options, &block)
102102
@http = http_klass(@url.scheme)
103103

104104
# Close the http connection when object is deallocated
105-
ObjectSpace.define_finalizer(self, proc {@http.shutdown if @http.respond_to?(:shutdown)})
105+
ObjectSpace.define_finalizer(self, self.class.finalize(@http))
106106
end
107107

108108
if block_given?
@@ -113,6 +113,13 @@ def initialize(url, **options, &block)
113113
end
114114
end
115115

116+
# Close the http connection when object is deallocated
117+
def self.finalize(klass)
118+
proc do
119+
klass.shutdown if klass.respond_to?(:shutdown)
120+
end
121+
end
122+
116123
##
117124
# Closes a client instance by finishing the connection.
118125
# The client is unavailable for any further data operations; an IOError is raised if such an attempt is made. I/O streams are automatically closed when they are claimed by the garbage collector.

0 commit comments

Comments
 (0)