Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Xlib/support/unix_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,21 @@ def get_display(display):

def _get_tcp_socket(host, dno):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 6000 + dno))
try:
s.connect((host, 6000 + dno))
except Exception:
s.close()
raise
return s


def _get_unix_socket(address):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(address)
try:
s.connect(address)
except Exception:
s.close()
raise
return s


Expand Down
18 changes: 18 additions & 0 deletions test/test_unix_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# -*- coding: utf-8 -*

from functools import partial
import gc
import socket
import sys
import unittest
import warnings

from mock import patch

Expand Down Expand Up @@ -152,6 +154,22 @@ def ensure_not_inheritable(*args):
expected_calls.append(('ensure_not_inheritable', s))
self.assertEqual(calls, expected_calls)

def test_leaking_sockets(self):
with warnings.catch_warnings():
warnings.simplefilter("error", ResourceWarning)
try:
unix_connect._get_tcp_socket("invalid!", 0x10000)
except:
pass
finally:
gc.collect()
try:
unix_connect._get_unix_socket(f"/tmp/too_long_socket_name{' ' * 1024}")
except:
pass
finally:
gc.collect()


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