|
| 1 | +# Copyright ScyllaDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Tests for the libev close() race condition fix (scylladb/python-driver#614). |
| 17 | +
|
| 18 | +The race: close() sets is_closed=True and calls _socket.close() immediately, |
| 19 | +but libev watchers are stopped asynchronously in _loop_will_run(). Between |
| 20 | +socket close and watcher stop, handle_read()/handle_write() can fire on the |
| 21 | +closed fd, causing EBADF -> ConnectionShutdown. |
| 22 | +
|
| 23 | +The fix adds early-return guards at the top of handle_read() and handle_write() |
| 24 | +that check is_closed/is_defunct before touching the socket. |
| 25 | +""" |
| 26 | + |
| 27 | +import errno |
| 28 | +import socket |
| 29 | +import unittest |
| 30 | +from unittest.mock import patch, MagicMock, PropertyMock |
| 31 | + |
| 32 | +from cassandra import DependencyException |
| 33 | + |
| 34 | +try: |
| 35 | + from cassandra.io.libevreactor import LibevConnection, _PEER_DISCONNECT_ERRNOS |
| 36 | +except (ImportError, DependencyException): |
| 37 | + LibevConnection = None |
| 38 | + _PEER_DISCONNECT_ERRNOS = None |
| 39 | + |
| 40 | +from tests import is_monkey_patched |
| 41 | + |
| 42 | + |
| 43 | +def _skip_if_unavailable(test_case): |
| 44 | + if is_monkey_patched(): |
| 45 | + raise unittest.SkipTest("Can't test libev with monkey patching") |
| 46 | + if LibevConnection is None: |
| 47 | + raise unittest.SkipTest('libev does not appear to be installed correctly') |
| 48 | + |
| 49 | + |
| 50 | +class LibevCloseRaceTest(unittest.TestCase): |
| 51 | + """ |
| 52 | + Tests for the close-race fix in LibevConnection (issue #614). |
| 53 | +
|
| 54 | + Each test simulates the scenario where a watcher fires after close() |
| 55 | + has already been called, verifying that the handler exits gracefully |
| 56 | + without calling defunct() or raising. |
| 57 | + """ |
| 58 | + |
| 59 | + def setUp(self): |
| 60 | + _skip_if_unavailable(self) |
| 61 | + LibevConnection.initialize_reactor() |
| 62 | + |
| 63 | + self.patchers = [ |
| 64 | + patch('socket.socket'), |
| 65 | + patch('cassandra.io.libevwrapper.IO'), |
| 66 | + patch('cassandra.io.libevreactor.LibevLoop.maybe_start'), |
| 67 | + ] |
| 68 | + for p in self.patchers: |
| 69 | + p.start() |
| 70 | + self._connections = [] |
| 71 | + |
| 72 | + def tearDown(self): |
| 73 | + for c in self._connections: |
| 74 | + c.close() |
| 75 | + for p in self.patchers: |
| 76 | + p.stop() |
| 77 | + |
| 78 | + def _make_connection(self): |
| 79 | + from cassandra.connection import DefaultEndPoint |
| 80 | + c = LibevConnection(DefaultEndPoint('1.2.3.4'), cql_version='3.0.1', connect_timeout=5) |
| 81 | + mock_socket = MagicMock() |
| 82 | + mock_socket.send.side_effect = lambda x: len(x) |
| 83 | + mock_socket.recv.return_value = b'' |
| 84 | + c._socket = mock_socket |
| 85 | + self._connections.append(c) |
| 86 | + return c |
| 87 | + |
| 88 | + # ------------------------------------------------------------------ |
| 89 | + # handle_write guards |
| 90 | + # ------------------------------------------------------------------ |
| 91 | + |
| 92 | + def test_handle_write_returns_immediately_when_closed(self): |
| 93 | + """ |
| 94 | + handle_write() must be a no-op if is_closed is already True. |
| 95 | + This prevents EBADF when the watcher fires after close(). |
| 96 | + """ |
| 97 | + c = self._make_connection() |
| 98 | + c.is_closed = True |
| 99 | + c.deque.append(b"data") |
| 100 | + |
| 101 | + # Should not raise and should not call send() |
| 102 | + c.handle_write(None, 0) |
| 103 | + c._socket.send.assert_not_called() |
| 104 | + |
| 105 | + def test_handle_write_returns_immediately_when_defunct(self): |
| 106 | + """ |
| 107 | + handle_write() must be a no-op if is_defunct is already True. |
| 108 | + """ |
| 109 | + c = self._make_connection() |
| 110 | + c.is_defunct = True |
| 111 | + c.deque.append(b"data") |
| 112 | + |
| 113 | + c.handle_write(None, 0) |
| 114 | + c._socket.send.assert_not_called() |
| 115 | + |
| 116 | + def test_handle_write_ebadf_after_close_does_not_defunct(self): |
| 117 | + """ |
| 118 | + If close() races with handle_write() and send() raises EBADF, |
| 119 | + the handler should return without calling defunct(). |
| 120 | +
|
| 121 | + Simulates close() happening between watcher dispatch and the |
| 122 | + error handler check: is_closed is False when the handler starts |
| 123 | + (passes top guard) but becomes True when send() is called. |
| 124 | + """ |
| 125 | + c = self._make_connection() |
| 126 | + c.deque.append(b"data") |
| 127 | + |
| 128 | + def send_sets_closed_and_raises(data): |
| 129 | + c.is_closed = True |
| 130 | + raise socket.error(errno.EBADF, "Bad file descriptor") |
| 131 | + |
| 132 | + c._socket.send.side_effect = send_sets_closed_and_raises |
| 133 | + |
| 134 | + with patch.object(c, 'defunct') as mock_defunct: |
| 135 | + c.handle_write(None, 0) |
| 136 | + mock_defunct.assert_not_called() |
| 137 | + |
| 138 | + def test_handle_write_peer_disconnect_closes_cleanly(self): |
| 139 | + """ |
| 140 | + If send() raises ECONNRESET (peer disconnect), handle_write() |
| 141 | + should call close() instead of defunct(). |
| 142 | + """ |
| 143 | + c = self._make_connection() |
| 144 | + c.deque.append(b"data") |
| 145 | + c._socket.send.side_effect = socket.error(errno.ECONNRESET, "Connection reset by peer") |
| 146 | + |
| 147 | + with patch.object(c, 'defunct') as mock_defunct, \ |
| 148 | + patch.object(c, 'close') as mock_close: |
| 149 | + c.handle_write(None, 0) |
| 150 | + mock_defunct.assert_not_called() |
| 151 | + mock_close.assert_called_once() |
| 152 | + |
| 153 | + # ------------------------------------------------------------------ |
| 154 | + # handle_read guards |
| 155 | + # ------------------------------------------------------------------ |
| 156 | + |
| 157 | + def test_handle_read_returns_immediately_when_closed(self): |
| 158 | + """ |
| 159 | + handle_read() must be a no-op if is_closed is already True. |
| 160 | + This prevents EBADF when the watcher fires after close(). |
| 161 | + """ |
| 162 | + c = self._make_connection() |
| 163 | + c.is_closed = True |
| 164 | + |
| 165 | + # Should not raise and should not call recv() |
| 166 | + c.handle_read(None, 0) |
| 167 | + c._socket.recv.assert_not_called() |
| 168 | + |
| 169 | + def test_handle_read_returns_immediately_when_defunct(self): |
| 170 | + """ |
| 171 | + handle_read() must be a no-op if is_defunct is already True. |
| 172 | + """ |
| 173 | + c = self._make_connection() |
| 174 | + c.is_defunct = True |
| 175 | + |
| 176 | + c.handle_read(None, 0) |
| 177 | + c._socket.recv.assert_not_called() |
| 178 | + |
| 179 | + def test_handle_read_ebadf_after_close_does_not_defunct(self): |
| 180 | + """ |
| 181 | + If close() races with handle_read() and recv() raises EBADF, |
| 182 | + the handler should return without calling defunct(). |
| 183 | +
|
| 184 | + Simulates close() happening between watcher dispatch and the |
| 185 | + error handler check: is_closed is False when the handler starts |
| 186 | + (passes top guard) but becomes True when recv() is called. |
| 187 | + """ |
| 188 | + c = self._make_connection() |
| 189 | + |
| 190 | + def recv_sets_closed_and_raises(bufsize): |
| 191 | + c.is_closed = True |
| 192 | + raise socket.error(errno.EBADF, "Bad file descriptor") |
| 193 | + |
| 194 | + c._socket.recv.side_effect = recv_sets_closed_and_raises |
| 195 | + |
| 196 | + with patch.object(c, 'defunct') as mock_defunct: |
| 197 | + c.handle_read(None, 0) |
| 198 | + mock_defunct.assert_not_called() |
| 199 | + |
| 200 | + def test_handle_read_peer_disconnect_closes_cleanly(self): |
| 201 | + """ |
| 202 | + If recv() raises ECONNRESET (peer disconnect), handle_read() |
| 203 | + should set last_error and call close() instead of defunct(). |
| 204 | + """ |
| 205 | + c = self._make_connection() |
| 206 | + c._socket.recv.side_effect = socket.error(errno.ECONNRESET, "Connection reset by peer") |
| 207 | + |
| 208 | + with patch.object(c, 'defunct') as mock_defunct, \ |
| 209 | + patch.object(c, 'close') as mock_close: |
| 210 | + c.handle_read(None, 0) |
| 211 | + mock_defunct.assert_not_called() |
| 212 | + mock_close.assert_called_once() |
| 213 | + # last_error should be set before close() |
| 214 | + self.assertIsNotNone(c.last_error) |
| 215 | + self.assertIn("closed by peer", str(c.last_error)) |
| 216 | + |
| 217 | + def test_handle_read_eof_sets_last_error_before_close(self): |
| 218 | + """ |
| 219 | + When recv() returns empty bytes (EOF / server closed connection), |
| 220 | + last_error must be set before close() is called so that |
| 221 | + factory() can detect the dead connection. |
| 222 | + """ |
| 223 | + c = self._make_connection() |
| 224 | + c._socket.recv.return_value = b'' |
| 225 | + |
| 226 | + with patch.object(c, 'close') as mock_close: |
| 227 | + c.handle_read(None, 0) |
| 228 | + mock_close.assert_called_once() |
| 229 | + self.assertIsNotNone(c.last_error) |
| 230 | + self.assertIn("closed by server", str(c.last_error)) |
| 231 | + |
| 232 | + # ------------------------------------------------------------------ |
| 233 | + # close() preserves last_error for factory() |
| 234 | + # ------------------------------------------------------------------ |
| 235 | + |
| 236 | + def test_close_sets_last_error_when_connected_event_not_set(self): |
| 237 | + """ |
| 238 | + When close() is called before connected_event is set (i.e. the |
| 239 | + connection was never fully established), last_error must be |
| 240 | + populated so factory() doesn't return a dead connection. |
| 241 | + """ |
| 242 | + c = self._make_connection() |
| 243 | + # connected_event is not set by default in a fresh connection |
| 244 | + c.connected_event.clear() |
| 245 | + |
| 246 | + c.close() |
| 247 | + |
| 248 | + self.assertIsNotNone(c.last_error) |
| 249 | + self.assertIn("was closed", str(c.last_error)) |
| 250 | + |
| 251 | + |
| 252 | +class LibevPeerDisconnectErrnos(unittest.TestCase): |
| 253 | + """Verify _PEER_DISCONNECT_ERRNOS contains expected values.""" |
| 254 | + |
| 255 | + def setUp(self): |
| 256 | + _skip_if_unavailable(self) |
| 257 | + |
| 258 | + def test_contains_ebadf(self): |
| 259 | + self.assertIn(errno.EBADF, _PEER_DISCONNECT_ERRNOS) |
| 260 | + |
| 261 | + def test_contains_econnreset(self): |
| 262 | + self.assertIn(errno.ECONNRESET, _PEER_DISCONNECT_ERRNOS) |
| 263 | + |
| 264 | + def test_contains_econnaborted(self): |
| 265 | + self.assertIn(errno.ECONNABORTED, _PEER_DISCONNECT_ERRNOS) |
| 266 | + |
| 267 | + def test_contains_enotconn(self): |
| 268 | + self.assertIn(errno.ENOTCONN, _PEER_DISCONNECT_ERRNOS) |
| 269 | + |
| 270 | + def test_contains_eshutdown(self): |
| 271 | + self.assertIn(errno.ESHUTDOWN, _PEER_DISCONNECT_ERRNOS) |
| 272 | + |
| 273 | + def test_contains_epipe(self): |
| 274 | + self.assertIn(errno.EPIPE, _PEER_DISCONNECT_ERRNOS) |
0 commit comments