mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-10 00:02:10 -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
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "objspace"
|
||||||
require "stringio"
|
require "stringio"
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
require "fileutils"
|
require "fileutils"
|
||||||
@ -92,6 +93,16 @@ module HTTPX
|
|||||||
@length = 0
|
@length = 0
|
||||||
@buffer = nil
|
@buffer = nil
|
||||||
@state = :idle
|
@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
|
end
|
||||||
|
|
||||||
def closed?
|
def closed?
|
||||||
@ -148,8 +159,6 @@ module HTTPX
|
|||||||
content.force_encoding(@encoding)
|
content.force_encoding(@encoding)
|
||||||
rescue ArgumentError # ex: unknown encoding name - utf
|
rescue ArgumentError # ex: unknown encoding name - utf
|
||||||
content
|
content
|
||||||
# ensure
|
|
||||||
# close
|
|
||||||
end
|
end
|
||||||
when nil
|
when nil
|
||||||
"".b
|
"".b
|
||||||
|
Loading…
x
Reference in New Issue
Block a user