From 4ff8b260a11c25648df81e4f876aab17b283e3af Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Mon, 6 Jan 2020 01:38:40 +0000 Subject: [PATCH] Don't close raw data tempfiles until the response gets GC'd Closing the raw data tempfiles immediately on connection reuse caused any response reading to fail with an I/O error if another request started on the same connection in the meantime. Delaying the closing until the response object falls out of scope and gets GC'd ensures that as long as there is a reference to that object, it can be read from, at the expense of a possibly larger memory overhead. --- qwarc/aiohttp.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/qwarc/aiohttp.py b/qwarc/aiohttp.py index e8199ec..36e99e8 100644 --- a/qwarc/aiohttp.py +++ b/qwarc/aiohttp.py @@ -45,8 +45,6 @@ class ResponseHandler(aiohttp.client_proto.ResponseHandler): self.rawData.responseData.write(data) def reset_raw_data(self): - if self.rawData: - self.rawData.close() self.rawData = RawData() @@ -170,6 +168,11 @@ class ClientResponse(aiohttp.client_reqrep.ClientResponse): self.connection.reset_raw_data() await super().release() + def __del__(self): + if self._rawData: + self._rawData.close() + super().__del__() + class Payload: # A class implementing the minimal subset used by the HttpPayloadParser to retrieve the data