Skip to content

Commit bccbca2

Browse files
committed
Fix some file descriptor leaks
Symptom was that eswitchd would become unresponsive after some time. The following error was observed: GetVnics failed - [Errno 24] Too many open files: OSError: [Errno 24] Too many open files This was tracked down to forgetting to call close on IPRoute objects as per the documentation. See: - https://docs.pyroute2.org/iproute.html - svinota/pyroute2#631
1 parent 0752bd6 commit bccbca2

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

networking_mlnx/eswitchd/utils/pci_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def get_vfs_info(self, pf):
7171

7272
def get_dev_attr(self, attr_path):
7373
try:
74-
fd = open(attr_path)
75-
return fd.readline().strip()
74+
with open(attr_path) as fd:
75+
return fd.readline().strip()
7676
except IOError:
7777
return
7878

networking_mlnx/internal/netdev_ops/impl_pyroute2.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ def set_vf_admin_state(self, pf_ifname, vf_idx, state):
3333
:param state: desired admin state as defined in
3434
networking_mlnx.internal.netdev_ops.constants
3535
"""
36-
try:
37-
ip = pyroute2.IPRoute()
38-
link_idx = ip.link_lookup(ifname=pf_ifname)[0]
39-
ip.link(
40-
'set', index=link_idx, vf={'vf': int(vf_idx),
41-
'link_state': state})
42-
except IndexError:
43-
raise exceptions.NetworkInterfaceNotFound(pf_ifname)
44-
except pyroute2.NetlinkError as e:
45-
raise exceptions.NetlinkRuntimeError(e)
36+
with pyroute2.IPRoute() as ip:
37+
try:
38+
link_idx = ip.link_lookup(ifname=pf_ifname)[0]
39+
ip.link(
40+
'set', index=link_idx, vf={'vf': int(vf_idx),
41+
'link_state': state})
42+
except IndexError:
43+
raise exceptions.NetworkInterfaceNotFound(pf_ifname)
44+
except pyroute2.NetlinkError as e:
45+
raise exceptions.NetlinkRuntimeError(e)
4646

4747
def set_link_state(self, ifname, state):
4848
"""Set net device link state
@@ -51,14 +51,14 @@ def set_link_state(self, ifname, state):
5151
:param state: desired link state as defined in
5252
networking_mlnx.internal.netdev_ops.constants
5353
"""
54-
try:
55-
ip = pyroute2.IPRoute()
56-
link_idx = ip.link_lookup(ifname=ifname)[0]
57-
ip.link('set', index=link_idx, state=state)
58-
except IndexError:
59-
raise exceptions.NetworkInterfaceNotFound(ifname)
60-
except pyroute2.NetlinkError as e:
61-
raise exceptions.NetlinkRuntimeError(e)
54+
with pyroute2.IPRoute() as ip:
55+
try:
56+
link_idx = ip.link_lookup(ifname=ifname)[0]
57+
ip.link('set', index=link_idx, state=state)
58+
except IndexError:
59+
raise exceptions.NetworkInterfaceNotFound(ifname)
60+
except pyroute2.NetlinkError as e:
61+
raise exceptions.NetlinkRuntimeError(e)
6262

6363
def set_vf_guid(self, pf_ifname, vf_idx, guid):
6464
"""Set vf administrative port and node GUID
@@ -68,17 +68,17 @@ def set_vf_guid(self, pf_ifname, vf_idx, guid):
6868
:param guid: 64bit guid str in xx:xx:xx:xx:xx:xx:xx:xx format
6969
where x is a hexadecimal digit.
7070
"""
71-
try:
72-
ip = pyroute2.IPRoute()
73-
link_idx = ip.link_lookup(ifname=pf_ifname)[0]
74-
ip.link('set', index=link_idx, vf={'vf': int(vf_idx),
75-
'ib_port_guid': guid})
76-
ip.link('set', index=link_idx,
77-
vf={'vf': int(vf_idx), 'ib_node_guid': guid})
78-
except IndexError:
79-
raise exceptions.NetworkInterfaceNotFound(pf_ifname)
80-
except pyroute2.NetlinkError as e:
81-
raise exceptions.NetlinkRuntimeError(e)
71+
with pyroute2.IPRoute() as ip:
72+
try:
73+
link_idx = ip.link_lookup(ifname=pf_ifname)[0]
74+
ip.link('set', index=link_idx, vf={'vf': int(vf_idx),
75+
'ib_port_guid': guid})
76+
ip.link('set', index=link_idx,
77+
vf={'vf': int(vf_idx), 'ib_node_guid': guid})
78+
except IndexError:
79+
raise exceptions.NetworkInterfaceNotFound(pf_ifname)
80+
except pyroute2.NetlinkError as e:
81+
raise exceptions.NetlinkRuntimeError(e)
8282

8383
def get_vf_guid(self, pf_ifname, vf_idx):
8484
"""Get vf administrative GUID
@@ -91,14 +91,14 @@ def get_vf_guid(self, pf_ifname, vf_idx):
9191
NOTE: while there are two GUIDs assigned per VF (port and node GUID)
9292
we assume they are the same and return just one value.
9393
"""
94-
try:
95-
ip = pyroute2.IPRoute()
96-
link_idx = ip.link_lookup(ifname=pf_ifname)[0]
97-
attrs = ip.link('get', index=link_idx, ext_mask=1)[0]
98-
except IndexError:
99-
raise exceptions.NetworkInterfaceNotFound(pf_ifname)
100-
except pyroute2.NetlinkError as e:
101-
raise exceptions.NetlinkRuntimeError(e)
94+
with pyroute2.IPRoute() as ip:
95+
try:
96+
link_idx = ip.link_lookup(ifname=pf_ifname)[0]
97+
attrs = ip.link('get', index=link_idx, ext_mask=1)[0]
98+
except IndexError:
99+
raise exceptions.NetworkInterfaceNotFound(pf_ifname)
100+
except pyroute2.NetlinkError as e:
101+
raise exceptions.NetlinkRuntimeError(e)
102102

103103
vf_attr = (attrs.get_attr('IFLA_VFINFO_LIST').
104104
get_attrs("IFLA_VF_INFO"))[int(vf_idx)]

0 commit comments

Comments
 (0)