mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
defining finalizer on response body, which keeps the reference to the buffer, which will get closed on GC; this will ensure that file descriptors don't leak
This commit is contained in:
parent
e43d5eddcd
commit
a1be10cbc6
@ -1,5 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "objspace"
|
||||
require "stringio"
|
||||
require "tempfile"
|
||||
require "fileutils"
|
||||
@ -92,6 +93,16 @@ module HTTPX
|
||||
@length = 0
|
||||
@buffer = nil
|
||||
@state = :idle
|
||||
ObjectSpace.define_finalizer(self, self.class.finalize(@buffer))
|
||||
end
|
||||
|
||||
def self.finalize(buffer)
|
||||
proc {
|
||||
return unless buffer
|
||||
|
||||
@buffer.close
|
||||
@buffer.unlink if @buffer.respond_to?(:unlink)
|
||||
}
|
||||
end
|
||||
|
||||
def closed?
|
||||
@ -148,8 +159,6 @@ module HTTPX
|
||||
content.force_encoding(@encoding)
|
||||
rescue ArgumentError # ex: unknown encoding name - utf
|
||||
content
|
||||
# ensure
|
||||
# close
|
||||
end
|
||||
when nil
|
||||
"".b
|
||||
|
Loading…
x
Reference in New Issue
Block a user