| 1 |
import errno |
| 2 |
import sys |
| 3 |
import tempfile |
| 4 |
from unittest.mock import Mock, call, patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from billiard import compat |
| 9 |
from billiard.compat import close_open_fds |
| 10 |
|
| 11 |
|
| 12 |
class test_close_open_fds: |
| 13 |
|
| 14 |
def test_closes_only_listed_descriptors(self): |
| 15 |
with patch('billiard.compat._open_fds', |
| 16 |
return_value=[0, 1, 2, 7, 9]), \ |
| 17 |
patch('os.close') as close, \ |
| 18 |
patch('os.closerange') as closerange: |
| 19 |
close_open_fds([1, 2]) |
| 20 |
assert close.call_args_list == [call(0), call(7), call(9)] |
| 21 |
closerange.assert_not_called() |
| 22 |
|
| 23 |
def test_accepts_file_objects_and_ignores_none(self): |
| 24 |
fh = Mock(name='fh') |
| 25 |
fh.fileno.return_value = 3 |
| 26 |
with patch('billiard.compat._open_fds', return_value=[3, 4]), \ |
| 27 |
patch('os.close') as close, \ |
| 28 |
patch('os.closerange'): |
| 29 |
close_open_fds([None, fh]) |
| 30 |
close.assert_called_once_with(4) |
| 31 |
|
| 32 |
@pytest.mark.parametrize('code', [errno.EBADF, errno.EINTR, errno.EIO]) |
| 33 |
def test_ignores_close_errors_like_closerange(self, code): |
| 34 |
exc = OSError() |
| 35 |
exc.errno = code |
| 36 |
with patch('billiard.compat._open_fds', return_value=[5, 6]), \ |
| 37 |
patch('os.close', side_effect=exc) as close, \ |
| 38 |
patch('os.closerange'): |
| 39 |
close_open_fds() |
| 40 |
assert close.call_args_list == [call(5), call(6)] |
| 41 |
|
| 42 |
def test_never_calls_get_fdmax_with_fd_dir(self): |
| 43 |
"""The limit is irrelevant when the open descriptors can be listed.""" |
| 44 |
with patch('billiard.compat._open_fds', return_value=[0, 1, 2]), \ |
| 45 |
patch('billiard.compat.get_fdmax') as get_fdmax, \ |
| 46 |
patch('os.close'), \ |
| 47 |
patch('os.closerange') as closerange: |
| 48 |
close_open_fds([0, 1, 2]) |
| 49 |
get_fdmax.assert_not_called() |
| 50 |
closerange.assert_not_called() |
| 51 |
|
| 52 |
|
| 53 |
class test_close_open_fds_fallback: |
| 54 |
"""Without an fd directory the gaps between kept descriptors are closed.""" |
| 55 |
|
| 56 |
def test_closes_ranges_between_kept_descriptors(self): |
| 57 |
with patch('billiard.compat._open_fds', return_value=None), \ |
| 58 |
patch('billiard.compat.get_fdmax', return_value=10), \ |
| 59 |
patch('os.closerange') as closerange, \ |
| 60 |
patch('os.close') as close: |
| 61 |
close_open_fds([1, 2, 5]) |
| 62 |
assert closerange.call_args_list == [ |
| 63 |
call(0, 1), call(3, 5), call(6, 10), |
| 64 |
] |
| 65 |
close.assert_not_called() |
| 66 |
|
| 67 |
def test_accepts_file_objects_and_ignores_none(self): |
| 68 |
fh = Mock(name='fh') |
| 69 |
fh.fileno.return_value = 4 |
| 70 |
with patch('billiard.compat._open_fds', return_value=None), \ |
| 71 |
patch('billiard.compat.get_fdmax', return_value=6), \ |
| 72 |
patch('os.closerange') as closerange, \ |
| 73 |
patch('os.close'): |
| 74 |
close_open_fds([None, fh, 4, 0]) |
| 75 |
assert closerange.call_args_list == [call(1, 4), call(5, 6)] |
| 76 |
|
| 77 |
def test_without_keep_closes_whole_range(self): |
| 78 |
with patch('billiard.compat._open_fds', return_value=None), \ |
| 79 |
patch('billiard.compat.get_fdmax', return_value=3), \ |
| 80 |
patch('os.closerange') as closerange, \ |
| 81 |
patch('os.close'): |
| 82 |
close_open_fds() |
| 83 |
closerange.assert_called_once_with(0, 3) |
| 84 |
|
| 85 |
def test_cost_does_not_grow_with_fdmax(self): |
| 86 |
"""One closerange() call per gap, however large the limit is. |
| 87 |
|
| 88 |
The body before celery/billiard#455 called os.close() once per |
| 89 |
descriptor up to get_fdmax(), which stalls for minutes in containers |
| 90 |
where the limit is ~1e9 (celery/celery#9886). |
| 91 |
""" |
| 92 |
with patch('billiard.compat._open_fds', return_value=None), \ |
| 93 |
patch('billiard.compat.get_fdmax', return_value=2 ** 20), \ |
| 94 |
patch('os.closerange') as closerange, \ |
| 95 |
patch('os.close') as close: |
| 96 |
close_open_fds([0, 1, 2]) |
| 97 |
closerange.assert_called_once_with(3, 2 ** 20) |
| 98 |
close.assert_not_called() |
| 99 |
|
| 100 |
|
| 101 |
class test_open_fds: |
| 102 |
|
| 103 |
def test_fd_dir_matches_this_platform(self): |
| 104 |
if sys.platform == 'darwin': |
| 105 |
assert compat._FD_DIR == '/dev/fd' |
| 106 |
elif sys.platform.startswith(('cygwin', 'freebsd', 'dragonfly')): |
| 107 |
assert compat._FD_DIR == '/dev/fd' |
| 108 |
else: |
| 109 |
assert compat._FD_DIR == '/proc/self/fd' |
| 110 |
|
| 111 |
@pytest.mark.skipif(sys.platform == 'win32', reason='no fd directory') |
| 112 |
def test_lists_a_descriptor_this_process_opened(self): |
| 113 |
with tempfile.TemporaryFile() as fh: |
| 114 |
fds = compat._open_fds() |
| 115 |
if fds is None: |
| 116 |
pytest.skip( |
| 117 |
f'fd directory {compat._FD_DIR!r} not available here') |
| 118 |
assert fh.fileno() in fds |
| 119 |
|
| 120 |
def test_returns_none_when_fd_dir_is_missing(self): |
| 121 |
with patch('os.listdir', side_effect=OSError()): |
| 122 |
assert compat._open_fds() is None |
| 123 |
|
| 124 |
def test_returns_none_on_freebsd_without_fdescfs(self): |
| 125 |
with patch.object(sys, 'platform', 'freebsd14'), \ |
| 126 |
patch('billiard.compat._dev_fd_is_fdescfs', |
| 127 |
return_value=False), \ |
| 128 |
patch('os.listdir') as listdir: |
| 129 |
assert compat._open_fds() is None |
| 130 |
listdir.assert_not_called() |
| 131 |
|
| 132 |
def test_dev_fd_is_fdescfs(self): |
| 133 |
with patch('os.stat', side_effect=[Mock(st_dev=1), Mock(st_dev=2)]): |
| 134 |
assert compat._dev_fd_is_fdescfs() |
| 135 |
with patch('os.stat', side_effect=[Mock(st_dev=1), Mock(st_dev=1)]): |
| 136 |
assert not compat._dev_fd_is_fdescfs() |
| 137 |
with patch('os.stat', side_effect=OSError()): |
| 138 |
assert not compat._dev_fd_is_fdescfs() |