Open Dadeos-Menlo opened 2 months ago
I am experiencing the same problem using version docker==7.1.0
.
My use case is slightly different but we can simplify it as:
logs = container.logs(stdout=True, stderr=True, stream=True)
count = 0
for line in logs:
print(line.strip())
count+=1
if count >= 10:
break
container.stop()
container.remove()
if I add the logs._response.close()
, I no longer encounter the problem
Attempting to use container.attach(stream=True) to stream logs from a container appears to leak unclosed sockets.
The following test case:
yields the following ouput:
The root cause appears to relate to a combination of APIClient._read_from_socket(…) and CancellableStream: https://github.com/docker/docker-py/blob/a3652028b1ead708bd9191efb286f909ba6c2a49/docker/api/container.py#L61-L65
The documentation for APIClient._read_from_socket(…) states:
and if
stream
is notTrue
then the implementation callsresponse.close()
: https://github.com/docker/docker-py/blob/a3652028b1ead708bd9191efb286f909ba6c2a49/docker/api/client.py#L443-L447however, the current implementation of CancellableStream.close() does not call
self._response.close()
.Modifying the test case to be:
(i.e. not bothering to call
CancellableStream.close()
, but manually callingCancellableStream._response.close()
appears to resolve the "ResourceWarning: unclosed <socket.socket …>" warnings; which appears to suggest thatCancellableStream.close()
should callself._response.close()
and perhaps the rest of the current implementation that appears to be seeking a socket in order to close it is unnecessary?This issue may, or may not, be that same as that reported in #3268.