question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ResourceWarning: unclosed <socket.socket>

See original GitHub issue

Hello.

It seems that in certain situations http socket isn’t properly closed.

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py:395: ResourceWarning: unclosed <socket.socket fd=7, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, raddr=/Users/stas/Library/Containers/com.docker.docker/Data/s60>

Python 3.5.1 on OSX.

Can be reproduced with following script:

import unittest
import docker

# Warning depends on code layout, so add here some ballast classes,
# probably that puts GC in specific position.

class A(object):
    def __init__(self, node):
        print('A')

class B(object):
    def __init__(self, node):
        print('B')

class C(object):
    def __init__(self, node):
        print('C')

class D(object):
    def __init__(self, node):
        print('D')

class E(object):
    def __init__(self, node):
        print('E')

class SingleNodePartition(object):

    def __init__(self, node):
        self.node = node
        self.docker_api = docker.Client()

    def container_exec(self, node, command):
        exec_id = self.docker_api.exec_create(node, command, user='root')
        output = self.docker_api.exec_start(exec_id)

    def start(self):
        self.container_exec(self.node, "ls")
        self.container_exec(self.node, "ls")
        self.container_exec(self.node, "ls")
        self.container_exec(self.node, "ls")

class RecoveryTest(unittest.TestCase):

    def test_node_partition(self):
        failure = SingleNodePartition('node3')
        failure.start()

if __name__ == '__main__':
    unittest.main()

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:19
  • Comments:6

github_iconTop GitHub Comments

1reaction
caaespincommented, Feb 24, 2020

Has this been fixed yet? I get this with docker py v4.2.0

0reactions
asottile-sentrycommented, Sep 15, 2022

as a workaround:

def docker_from_env() -> ContextManager[docker.DockerClient]:
    return contextlib.closing(docker.from_env())

usage:

with docker_from_env() as client:
    ...
Read more comments on GitHub >

github_iconTop Results From Across the Web

ResourceWarning unclosed socket in Python 3 Unit Test
unittest 's tearDown method allows you to define some code that will be run after each test. Using this hook to close the...
Read more >
unclosed socket.socket when I run a unittest? · Issue #3912 ...
This warning happens because Requests uses a keep-alive model that means we try not to explicitly close sockets in many cases. Exactly why...
Read more >
ResourceWarning: unclosed <socket.socket [closed] fd=3 ...
_socket.socket object destructor emits a ResourceWarning if the socket is not closed. The problem is this warning: build/Lib/contextlib.py:60: ...
Read more >
[PYTHON-2338] _RttMonitor causes ResourceWarning
Noticed this ResourceWarning for an unclosed socket while running the tests. The ResourceWarning seem to randomly occur throughout the test ...
Read more >
ResourceWarning unclosed socket in Python 3 Unit Test
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py:601: ResourceWarning: unclosed socket.socket fd=4, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found