|
| 1 | +"""Pin: ``DqliteConnection`` and ``ConnectionPool`` raise |
| 2 | +``InterfaceError`` if used from a child process after ``os.fork``. |
| 3 | +
|
| 4 | +Fork-after-init is unsupported: the inherited TCP socket would be |
| 5 | +shared with the parent (writes interleaving on the wire), and asyncio |
| 6 | +primitives bound to the parent's loop are unusable in the child. |
| 7 | +
|
| 8 | +The fix records ``os.getpid()`` in ``__init__`` (both classes) and |
| 9 | +``_check_in_use`` / ``acquire`` raise a clear ``InterfaceError`` |
| 10 | +("reconstruct from configuration in the target process") on pid |
| 11 | +mismatch — symmetric with the existing ``__reduce__`` pickle guards. |
| 12 | +
|
| 13 | +The test does not need a live server: the pid check fires before any |
| 14 | +network work; an unconnected instance is sufficient. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import asyncio |
| 20 | +import os |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from dqliteclient import DqliteConnection |
| 25 | +from dqliteclient.exceptions import InterfaceError |
| 26 | +from dqliteclient.pool import ConnectionPool |
| 27 | + |
| 28 | + |
| 29 | +def _run_in_child(check) -> bytes: |
| 30 | + r, w = os.pipe() |
| 31 | + pid = os.fork() |
| 32 | + if pid == 0: |
| 33 | + try: |
| 34 | + os.close(r) |
| 35 | + try: |
| 36 | + check() |
| 37 | + os.write(w, b"NO_RAISE") |
| 38 | + except InterfaceError as e: |
| 39 | + msg = str(e) |
| 40 | + if "fork" in msg and "reconstruct from configuration" in msg: |
| 41 | + os.write(w, b"OK") |
| 42 | + else: |
| 43 | + os.write(w, f"WRONG_MSG:{msg}".encode()) |
| 44 | + except Exception as e: # noqa: BLE001 |
| 45 | + os.write(w, f"WRONG_TYPE:{type(e).__name__}:{e}".encode()) |
| 46 | + finally: |
| 47 | + os.close(w) |
| 48 | + finally: |
| 49 | + os._exit(0) |
| 50 | + os.close(w) |
| 51 | + result = b"" |
| 52 | + while True: |
| 53 | + chunk = os.read(r, 4096) |
| 54 | + if not chunk: |
| 55 | + break |
| 56 | + result += chunk |
| 57 | + os.close(r) |
| 58 | + os.waitpid(pid, 0) |
| 59 | + return result |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.skipif(not hasattr(os, "fork"), reason="requires os.fork") |
| 63 | +def test_dqlite_connection_used_after_fork_raises_interface_error() -> None: |
| 64 | + conn = DqliteConnection("127.0.0.1:9999") |
| 65 | + assert conn._creator_pid == os.getpid() |
| 66 | + |
| 67 | + def child_check() -> None: |
| 68 | + # ``_check_in_use`` runs ``asyncio.get_running_loop`` after |
| 69 | + # the pid check. Drive a loop so the pid mismatch is the |
| 70 | + # raised error, not the "must be used from async context" one. |
| 71 | + async def run() -> None: |
| 72 | + conn._check_in_use() |
| 73 | + |
| 74 | + asyncio.run(run()) |
| 75 | + |
| 76 | + result = _run_in_child(child_check) |
| 77 | + assert result == b"OK", f"child reported: {result!r}" |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.skipif(not hasattr(os, "fork"), reason="requires os.fork") |
| 81 | +def test_connection_pool_acquire_after_fork_raises_interface_error() -> None: |
| 82 | + pool = ConnectionPool(addresses=["127.0.0.1:9999"]) |
| 83 | + assert pool._creator_pid == os.getpid() |
| 84 | + |
| 85 | + def child_check() -> None: |
| 86 | + async def run() -> None: |
| 87 | + async with pool.acquire(): |
| 88 | + pass |
| 89 | + |
| 90 | + asyncio.run(run()) |
| 91 | + |
| 92 | + result = _run_in_child(child_check) |
| 93 | + assert result == b"OK", f"child reported: {result!r}" |
0 commit comments