docker / docker/docker-py

Leaking file descriptors

Open
#2,766 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

There are two places where file descriptors are being leaked , at least with python 3.8:

  1. _get_raw_response_socket(): in case of Unix sockets, additional referencing seems to stop them from being closed
  2. When DockerClient is not used as a context manager it does not call close() on the api session object.

I have managed to work around with:

class FixedAPIClient(docker.APIClient):
    def _get_raw_response_socket(self, response):
        sock = super()._get_raw_response_socket(response)
        if self.base_url.startswith('http+docker://'):
            sock._response = None
        return sock


class FixedDockerClient(docker.DockerClient):
    def __init__(self, *args, **kwargs):
        self.api = FixedAPIClient(*args, **kwargs)

    def __del__(self):
        try:
            if self.api:
                self.api.close()
        except AttributeError:
            pass

Our setup is quite complex. If you believe this should not happen and it is a corner case I will try to come up with a minimal example.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reducing the reported setup to a minimal reproduction for the two paths: _get_raw_response_socket() with Unix sockets and DockerClient used without a context manager. Inspect how the API session is created and closed, then verify that both paths release their file descriptors without relying on the workaround shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.