diff --git a/rethinkdb/net_asyncio.py b/rethinkdb/net_asyncio.py index 35c891a..1c44121 100644 --- a/rethinkdb/net_asyncio.py +++ b/rethinkdb/net_asyncio.py @@ -42,21 +42,6 @@ __all__ = ["Connection"] -async def _read_until(streamreader, delimiter): - """Naive implementation of reading until a delimiter""" - buffer = bytearray() - - while True: - chunk = await streamreader.read(1) - if not chunk: - break # EOF - buffer.append(chunk[0]) - if chunk == delimiter: - break - - return bytes(buffer) - - def reusable_waiter(loop, timeout): """Wait for something, with a timeout from when the waiter was created. @@ -173,9 +158,8 @@ def _maybe_fetch_batch(self): ): self.outstanding_requests += 1 # Keep a reference so the task is not garbage-collected before it runs. - self._fetch_batch_task = asyncio.ensure_future( - self.conn.parent.resume(self), - loop=self.conn.io_loop, + self._fetch_batch_task = self.conn.io_loop.create_task( + self.conn.parent.resume(self) ) @@ -192,9 +176,7 @@ def __init__(self, parent, io_loop=None): self._user_queries = {} self._cursor_cache = {} self._ready = asyncio.Future() - self._io_loop = io_loop - if self._io_loop is None: - self._io_loop = asyncio.get_event_loop() + self._io_loop = io_loop if io_loop is not None else asyncio.get_running_loop() @property def parent(self): @@ -264,9 +246,10 @@ async def connect(self, timeout): # an optimization, then need to read each separately if request != "": self._streamwriter.write(request) + await self._streamwriter.drain() response = await asyncio.wait_for( - _read_until(self._streamreader, b"\0"), + self._streamreader.readuntil(b"\0"), timeout, ) response = response[:-1] @@ -287,7 +270,7 @@ async def connect(self, timeout): # Start a parallel function to perform reads # store a reference to it so it doesn't get destroyed - self._reader_task = asyncio.ensure_future(self._reader(), loop=self._io_loop) + self._reader_task = self._io_loop.create_task(self._reader()) return self._parent def is_open(self): @@ -327,16 +310,36 @@ async def close(self, noreply_wait=False, token=None, exception=None): return + def _discard_pending_query(self, token): + """Drop a waiter whose write failed before the server could answer.""" + pending = self._user_queries.get(token) + if pending is None: + return + + _, future = pending + if not future.done(): + del self._user_queries[token] + async def run_query(self, query, noreply): """Run a query on the connection.""" - self._streamwriter.write( - query.serialize(self._parent.get_json_encoder(query)) # pylint: disable=protected-access - ) - if noreply: - return None + # Register before writing. drain() yields, and the reader can observe + # the response before this coroutine resumes. + response_future = None + if not noreply: + response_future = self._io_loop.create_future() + self._user_queries[query.token] = (query, response_future) - response_future = asyncio.Future() - self._user_queries[query.token] = (query, response_future) + try: + self._streamwriter.write( + query.serialize(self._parent.get_json_encoder(query)) # pylint: disable=protected-access + ) + await self._streamwriter.drain() + except Exception: + self._discard_pending_query(query.token) + raise + + if response_future is None: + return None return await response_future # The _reader coroutine runs in parallel, reading responses diff --git a/rethinkdb/net_gevent.py b/rethinkdb/net_gevent.py index 8de5bb8..2e54cdd 100644 --- a/rethinkdb/net_gevent.py +++ b/rethinkdb/net_gevent.py @@ -294,20 +294,35 @@ def close(self, noreply_wait=False, token=None, exception=None): except OSError: pass + def _discard_pending_query(self, token): + """Drop a waiter whose write failed before the server could answer.""" + pending = self._user_queries.get(token) + if pending is None: + return + + _, async_res = pending + if not async_res.ready(): + del self._user_queries[token] + def run_query(self, query, noreply): """Run a query on the connection.""" + # Register before sending. sendall can yield, and the reader can + # observe the response before this greenlet resumes. self._write_mutex.acquire() - + async_res = None try: + if not noreply: + async_res = AsyncResult() + self._user_queries[query.token] = (query, async_res) self._socket.sendall(query.serialize(self._parent.get_json_encoder(query))) + except Exception: + self._discard_pending_query(query.token) + raise finally: self._write_mutex.release() - if noreply: + if async_res is None: return None - - async_res = AsyncResult() - self._user_queries[query.token] = (query, async_res) return async_res.get() def _reader(self): diff --git a/rethinkdb/net_tornado.py b/rethinkdb/net_tornado.py index dbdd420..1396982 100644 --- a/rethinkdb/net_tornado.py +++ b/rethinkdb/net_tornado.py @@ -259,7 +259,7 @@ async def _perform_handshake(self, deadline): # This may happen in the `V1_0` protocol where we send two requests as # an optimization, then need to read each separately if request != "": - self._stream.write(request) + await self._stream.write(request) response = await with_absolute_timeout( deadline, @@ -318,6 +318,16 @@ async def close( pass return + def _discard_pending_query(self, token: int) -> None: + """Drop a waiter whose write failed before the server could answer.""" + pending = self._user_queries.get(token) + if pending is None: + return + + _, future = pending + if not future.done(): + del self._user_queries[token] + async def run_query(self, query: Query, noreply: bool) -> Any | None: """ Run a query on the connection. @@ -325,14 +335,24 @@ async def run_query(self, query: Query, noreply: bool) -> Any | None: if self._stream is None: raise ReqlDriverError("Connection is closed.") - await self._stream.write(query.serialize(self._parent.get_json_encoder(query))) - if noreply: - return None + # Register before writing. The write future yields, and the reader can + # observe the response before this coroutine resumes. + response_future: Future[Any] | None = None + if not noreply: + response_future = Future() + self._user_queries[query.token] = (query, response_future) - response_future: Future = Future() - self._user_queries[query.token] = (query, response_future) - res = await response_future - return res + try: + await self._stream.write( + query.serialize(self._parent.get_json_encoder(query)) + ) + except Exception: + self._discard_pending_query(query.token) + raise + + if response_future is None: + return None + return await response_future # The _reader coroutine runs in its own context at the top level of the # Tornado.IOLoop it was created with. It runs in parallel, reading responses diff --git a/rethinkdb/net_trio.py b/rethinkdb/net_trio.py index 97b9c6d..935212d 100644 --- a/rethinkdb/net_trio.py +++ b/rethinkdb/net_trio.py @@ -382,13 +382,32 @@ async def close(self, noreply_wait=False, token=None, exception=None): return + def _discard_pending_query(self, token): + """Drop a waiter whose write failed before the server could answer.""" + pending = self._user_queries.get(token) + if pending is None: + return + + _, future = pending + if not future.done(): + del self._user_queries[token] + async def run_query(self, query, noreply): - await self._send(query.serialize(self._parent.get_json_encoder(query))) - if noreply: - return None + # Register before sending. send_all yields, and the reader can observe + # the response before this coroutine resumes. + response_future = None + if not noreply: + response_future = TrioFuture() + self._user_queries[query.token] = (query, response_future) - response_future = TrioFuture() - self._user_queries[query.token] = (query, response_future) + try: + await self._send(query.serialize(self._parent.get_json_encoder(query))) + except Exception: + self._discard_pending_query(query.token) + raise + + if response_future is None: + return None return await response_future.wait() # The reader task runs in parallel, reading responses diff --git a/tests/test_net_asyncio.py b/tests/test_net_asyncio.py new file mode 100644 index 0000000..52fe44f --- /dev/null +++ b/tests/test_net_asyncio.py @@ -0,0 +1,228 @@ +# Copyright 2016-2026 RethinkDB +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio + +import pytest + +from rethinkdb.encoder import ReqlEncoder +from rethinkdb.errors import ReqlDriverError +from rethinkdb.net import Query +from rethinkdb.net_asyncio import ConnectionInstance + + +class _Socket: + """Socket double that accepts the connect-time options.""" + + def setsockopt(self, level: int, option: int, value: int) -> None: + del level, option, value + + +class _Handshake: + """Handshake that emits one request and then finishes.""" + + def __init__(self) -> None: + self._sent = False + + def reset(self) -> None: + self._sent = False + + def next_message(self, raw_response: bytes | None) -> bytes | None: + del raw_response + if self._sent: + return None + self._sent = True + return b"protocol" + + +class _ConnectParent: + """Parent connection with the attributes ``connect`` reads.""" + + def __init__(self) -> None: + self.host = "localhost" + self.port = 28015 + self.ssl: dict[str, str] = {} + self.handshake = _Handshake() + + +class _QueryParent: + """Parent connection that encodes queries with the stock encoder.""" + + def get_json_encoder(self, query: object) -> ReqlEncoder: + del query + return ReqlEncoder() + + +class _EofReader: + """Reader whose first ``readuntil`` reports EOF.""" + + def __init__(self) -> None: + self.readuntil_calls = 0 + + def at_eof(self) -> bool: + return True + + async def readuntil(self, separator: bytes) -> bytes: + del separator + self.readuntil_calls += 1 + raise asyncio.IncompleteReadError(partial=b"", expected=1) + + +class _ConnectWriter: + """Writer that records handshake writes and drains.""" + + def __init__(self) -> None: + self.writes: list[bytes] = [] + self.drain_calls = 0 + + def write(self, data: bytes) -> None: + self.writes.append(data) + + async def drain(self) -> None: + self.drain_calls += 1 + + def get_extra_info(self, name: str) -> _Socket: + assert name == "socket" + return _Socket() + + def close(self) -> None: + return None + + async def wait_closed(self) -> None: + return None + + +class _QueryWriter: + """Writer that observes query registration from inside ``drain``.""" + + def __init__( + self, + instance: ConnectionInstance, + token: int, + *, + fail: bool = False, + complete_before_failure: bool = False, + ) -> None: + self.instance = instance + self.token = token + self.fail = fail + self.complete_before_failure = complete_before_failure + self.saw_token = False + self.drain_calls = 0 + + def write(self, data: bytes) -> None: + del data + + async def drain(self) -> None: + self.drain_calls += 1 + # pylint: disable=protected-access + self.saw_token = self.token in self.instance._user_queries + if self.saw_token and (not self.fail or self.complete_before_failure): + _, future = self.instance._user_queries[self.token] + future.set_result("ok") + if self.fail: + raise ConnectionResetError("reset") + + +def _query(token: int) -> Query: + return Query(1, token, None) + + +def _query_instance(writer: _QueryWriter) -> ConnectionInstance: + instance = writer.instance + instance._streamwriter = writer # pylint: disable=protected-access + return instance + + +@pytest.mark.asyncio +@pytest.mark.unit +async def test_handshake_eof_is_a_driver_error( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A handshake that ends before the delimiter fails as a driver error.""" + reader = _EofReader() + writer = _ConnectWriter() + + async def open_connection( + *args: object, **kwargs: object + ) -> tuple[_EofReader, _ConnectWriter]: + del args, kwargs + return reader, writer + + monkeypatch.setattr( + "rethinkdb.net_asyncio.asyncio.open_connection", open_connection + ) + instance = ConnectionInstance(_ConnectParent()) + + with pytest.raises(ReqlDriverError, match="Could not connect"): + await instance.connect(1) + + assert writer.writes == [b"protocol"] + assert writer.drain_calls == 1 + assert reader.readuntil_calls == 1 + + +@pytest.mark.asyncio +@pytest.mark.unit +async def test_run_query_registers_before_drain() -> None: + """The response waiter exists before ``drain`` yields to the reader.""" + instance = ConnectionInstance(_QueryParent()) + writer = _QueryWriter(instance, token=7) + _query_instance(writer) + + assert await instance.run_query(_query(7), noreply=False) == "ok" + assert writer.drain_calls == 1 + assert writer.saw_token is True + + +@pytest.mark.asyncio +@pytest.mark.unit +async def test_noreply_query_drains_without_a_waiter() -> None: + """A noreply write is drained and does not occupy the response table.""" + instance = ConnectionInstance(_QueryParent()) + writer = _QueryWriter(instance, token=7) + _query_instance(writer) + + assert await instance.run_query(_query(7), noreply=True) is None + assert writer.drain_calls == 1 + assert writer.saw_token is False + + +@pytest.mark.asyncio +@pytest.mark.unit +async def test_drain_failure_drops_pending_waiter() -> None: + """A failed drain removes the waiter the caller will not await.""" + instance = ConnectionInstance(_QueryParent()) + writer = _QueryWriter(instance, token=7, fail=True) + _query_instance(writer) + + with pytest.raises(ConnectionResetError, match="reset"): + await instance.run_query(_query(7), noreply=False) + + assert writer.saw_token is True + assert 7 not in instance._user_queries # pylint: disable=protected-access + + +@pytest.mark.asyncio +@pytest.mark.unit +async def test_drain_failure_keeps_a_completed_waiter() -> None: + """A response delivered during ``drain`` is left in place when the write fails.""" + instance = ConnectionInstance(_QueryParent()) + writer = _QueryWriter(instance, token=7, fail=True, complete_before_failure=True) + _query_instance(writer) + + with pytest.raises(ConnectionResetError, match="reset"): + await instance.run_query(_query(7), noreply=False) + + assert 7 in instance._user_queries # pylint: disable=protected-access diff --git a/tests/test_net_gevent.py b/tests/test_net_gevent.py new file mode 100644 index 0000000..c3e58e0 --- /dev/null +++ b/tests/test_net_gevent.py @@ -0,0 +1,89 @@ +# Copyright 2016-2026 RethinkDB +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +from rethinkdb.encoder import ReqlEncoder +from rethinkdb.net import Query +from rethinkdb.net_gevent import ConnectionInstance + + +class _QueryParent: + """Parent connection that encodes queries with the stock encoder.""" + + def get_json_encoder(self, query: object) -> ReqlEncoder: + del query + return ReqlEncoder() + + +class _Socket: + """Socket that reports whether the response waiter exists during ``sendall``.""" + + def __init__( + self, instance: ConnectionInstance, token: int, *, fail: bool = False + ) -> None: + self.instance = instance + self.token = token + self.fail = fail + self.saw_token = False + + def sendall(self, data: bytes) -> None: + del data + # pylint: disable=protected-access + self.saw_token = self.token in self.instance._user_queries + if self.fail: + raise OSError("reset") + if self.saw_token: + _, result = self.instance._user_queries[self.token] + result.set("ok") + + +def _instance(token: int, *, fail: bool = False) -> tuple[ConnectionInstance, _Socket]: + instance = ConnectionInstance(_QueryParent()) + socket = _Socket(instance, token, fail=fail) + instance._socket = socket # type: ignore[assignment] # pylint: disable=protected-access + return instance, socket + + +@pytest.mark.gevent +@pytest.mark.unit +def test_run_query_registers_before_send() -> None: + """The response waiter exists before ``sendall`` can yield to the reader.""" + instance, socket = _instance(3) + + assert instance.run_query(Query(1, 3, None), noreply=False) == "ok" + assert socket.saw_token is True + + +@pytest.mark.gevent +@pytest.mark.unit +def test_noreply_query_sends_without_a_waiter() -> None: + """A noreply write does not occupy the response table.""" + instance, socket = _instance(3) + + assert instance.run_query(Query(1, 3, None), noreply=True) is None + assert socket.saw_token is False + + +@pytest.mark.gevent +@pytest.mark.unit +def test_send_failure_drops_pending_waiter() -> None: + """A failed send removes the waiter the caller will not wait on.""" + instance, socket = _instance(3, fail=True) + + with pytest.raises(OSError, match="reset"): + instance.run_query(Query(1, 3, None), noreply=False) + + assert socket.saw_token is True + assert 3 not in instance._user_queries # pylint: disable=protected-access diff --git a/tests/test_net_tornado.py b/tests/test_net_tornado.py new file mode 100644 index 0000000..cf835a0 --- /dev/null +++ b/tests/test_net_tornado.py @@ -0,0 +1,155 @@ +# Copyright 2016-2026 RethinkDB +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio + +import pytest + +from rethinkdb.encoder import ReqlEncoder +from rethinkdb.net import Query +from rethinkdb.net_tornado import ConnectionInstance + + +class _QueryParent: + """Parent connection that encodes queries with the stock encoder.""" + + def __init__(self) -> None: + self.handshake = _Handshake() + + def get_json_encoder(self, query: object) -> ReqlEncoder: + del query + return ReqlEncoder() + + +class _Handshake: + """Handshake that emits one request and then finishes.""" + + def __init__(self) -> None: + self._sent = False + + def reset(self) -> None: + self._sent = False + + def next_message(self, raw_response: bytes | None) -> bytes | None: + del raw_response + if self._sent: + return None + self._sent = True + return b"protocol" + + +class _HandshakeStream: + """Stream that finishes its write before the following read can proceed.""" + + def __init__(self) -> None: + self.write_finished = False + self.read_after_write = False + + async def write(self, data: bytes) -> None: + del data + await asyncio.sleep(0) + self.write_finished = True + + async def read_until(self, separator: bytes) -> bytes: + del separator + self.read_after_write = self.write_finished + return b"ok\0" + + +class _QueryStream: + """Stream that observes query registration from inside ``write``.""" + + def __init__( + self, instance: ConnectionInstance, token: int, *, fail: bool = False + ) -> None: + self.instance = instance + self.token = token + self.fail = fail + self.saw_token = False + + async def write(self, data: bytes) -> None: + del data + # pylint: disable=protected-access + self.saw_token = self.token in self.instance._user_queries + if self.fail: + raise ConnectionResetError("reset") + if self.saw_token: + _, future = self.instance._user_queries[self.token] + future.set_result("ok") + + +def _instance(stream: object) -> ConnectionInstance: + instance = ConnectionInstance(_QueryParent()) + instance._socket.close() # pylint: disable=protected-access + instance._stream = stream # type: ignore[assignment] # pylint: disable=protected-access + return instance + + +@pytest.mark.asyncio +@pytest.mark.tornado +@pytest.mark.unit +async def test_handshake_awaits_write_before_read() -> None: + """The handshake does not read until the preceding write has finished.""" + stream = _HandshakeStream() + instance = _instance(stream) + + await instance._perform_handshake(None) # pylint: disable=protected-access + + assert stream.write_finished is True + assert stream.read_after_write is True + + +@pytest.mark.asyncio +@pytest.mark.tornado +@pytest.mark.unit +async def test_run_query_registers_before_write() -> None: + """The response waiter exists before ``write`` yields to the reader.""" + instance = ConnectionInstance(_QueryParent()) + stream = _QueryStream(instance, token=4) + instance._socket.close() # pylint: disable=protected-access + instance._stream = stream # type: ignore[assignment] # pylint: disable=protected-access + + assert await instance.run_query(Query(1, 4, None), noreply=False) == "ok" + assert stream.saw_token is True + + +@pytest.mark.asyncio +@pytest.mark.tornado +@pytest.mark.unit +async def test_noreply_query_writes_without_a_waiter() -> None: + """A noreply write does not occupy the response table.""" + instance = ConnectionInstance(_QueryParent()) + stream = _QueryStream(instance, token=4) + instance._socket.close() # pylint: disable=protected-access + instance._stream = stream # type: ignore[assignment] # pylint: disable=protected-access + + assert await instance.run_query(Query(1, 4, None), noreply=True) is None + assert stream.saw_token is False + + +@pytest.mark.asyncio +@pytest.mark.tornado +@pytest.mark.unit +async def test_write_failure_drops_pending_waiter() -> None: + """A failed write removes the waiter the caller will not await.""" + instance = ConnectionInstance(_QueryParent()) + stream = _QueryStream(instance, token=4, fail=True) + instance._socket.close() # pylint: disable=protected-access + instance._stream = stream # type: ignore[assignment] # pylint: disable=protected-access + + with pytest.raises(ConnectionResetError, match="reset"): + await instance.run_query(Query(1, 4, None), noreply=False) + + assert stream.saw_token is True + assert 4 not in instance._user_queries # pylint: disable=protected-access diff --git a/tests/test_net_trio.py b/tests/test_net_trio.py index 43131da..2b6d117 100644 --- a/tests/test_net_trio.py +++ b/tests/test_net_trio.py @@ -15,7 +15,9 @@ import pytest import trio +from rethinkdb.encoder import ReqlEncoder from rethinkdb.errors import ReqlDriverError +from rethinkdb.net import Query from rethinkdb.net_trio import ConnectionInstance, TrioCursor @@ -145,3 +147,73 @@ async def test_parent_continue_swallows_closed_connection() -> None: cursor.conn = _CursorConn() # type: ignore[assignment] assert await cursor._parent_continue() is None # pylint: disable=protected-access + + +class _QueryParent: + """Parent connection that encodes queries with the stock encoder.""" + + def get_json_encoder(self, query: object) -> ReqlEncoder: + del query + return ReqlEncoder() + + +class _RecordingStream: + """Stream that reports whether the response waiter exists during ``send_all``.""" + + def __init__(self, instance: ConnectionInstance, token: int, *, fail: bool) -> None: + self.instance = instance + self.token = token + self.fail = fail + self.saw_token = False + + async def send_all(self, data: bytes) -> None: + del data + # pylint: disable=protected-access + self.saw_token = self.token in self.instance._user_queries + if self.fail: + raise trio.BrokenResourceError("reset") + if self.saw_token: + _, future = self.instance._user_queries[self.token] + future.set_result("ok") + + +def _query_instance( + token: int, *, fail: bool = False +) -> tuple[ConnectionInstance, _RecordingStream]: + instance = ConnectionInstance(_QueryParent()) + stream = _RecordingStream(instance, token, fail=fail) + instance._stream = stream # pylint: disable=protected-access + return instance, stream + + +@pytest.mark.trio +@pytest.mark.unit +async def test_run_query_registers_before_send() -> None: + """The response waiter exists before ``send_all`` yields to the reader.""" + instance, stream = _query_instance(9) + + assert await instance.run_query(Query(1, 9, None), noreply=False) == "ok" + assert stream.saw_token is True + + +@pytest.mark.trio +@pytest.mark.unit +async def test_noreply_query_sends_without_a_waiter() -> None: + """A noreply write does not occupy the response table.""" + instance, stream = _query_instance(9) + + assert await instance.run_query(Query(1, 9, None), noreply=True) is None + assert stream.saw_token is False + + +@pytest.mark.trio +@pytest.mark.unit +async def test_send_failure_drops_pending_waiter() -> None: + """A failed send removes the waiter the caller will not await.""" + instance, stream = _query_instance(9, fail=True) + + with pytest.raises(ReqlDriverError, match="Connection is closed"): + await instance.run_query(Query(1, 9, None), noreply=False) + + assert stream.saw_token is True + assert 9 not in instance._user_queries # pylint: disable=protected-access