Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## [3.10.1] - 2026-11-02

- [Fixed] A peer-initiated WSS close that surfaces as a bare `OSError` (`EBADF` / Errno 9, `ENOTCONN`, or a bare `ECONNRESET`) is now a transient reconnect: `_sub_routing_loop` and `_ping_pong` log it at WARNING and set `wss_conn_halted`, instead of ERROR with a traceback. `websocket-client` replies to that close with a close frame on the already-dead socket, and Python does not promote `EBADF`/`ENOTCONN` to a `Connection*` subclass, so they missed `TRANSIENT_WS_ERRORS`. Other `OSError`s still log at ERROR. Reconnect behavior is unchanged. (FEMSA-80)
- [Dependencies] Refreshed dependencies within allowed ranges (`orjson` 3.12.0 → 3.13.0, `tenacity` 9.1.4 → 9.2.1, plus transitive); `uv.lock` updated.

## [3.10.0] - 2026-10-18

- [Added] Operation hooks (`pygqlc.hooks.add_operation_hook` / `remove_operation_hook`): a function that observes every operation the client runs, called once per query or mutation when `execute`/`async_execute` finish (a resend included) and once per subscription message, with an `OperationEvent` (kind, name, document, variables, environment, start, duration, response or error). Consumers can trace or measure their GraphQL traffic (Valiot workers use it to put each call on a timeline next to the screen that triggered the work) without wrapping the client. Hooks only observe: a hook's exception is logged and never reaches the caller, and `query`/`mutate` return exactly what they returned before. The registry is process-wide like `set_logger`, so it survives a reset of the `GraphQLClient` singleton. With no hooks registered the client does no extra work; the POST body, retries and errors are unchanged.
Expand Down
18 changes: 16 additions & 2 deletions pygqlc/GraphQLClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import asyncio
import errno
import traceback
import weakref
import time
Expand Down Expand Up @@ -41,6 +42,19 @@
websocket.WebSocketConnectionClosedException,
)

# Errnos Python leaves as a bare OSError (no Connection* subclass). They mean
# the fd is already gone: the peer closed, or another thread closed it, and
# websocket-client's close-frame reply then fails in sock.send. ECONNRESET is
# usually promoted to ConnectionResetError; a bare OSError with that errno
# (copied or assigned after construction) is the same teardown. (FEMSA-80)
_DEAD_SOCKET_ERRNOS = frozenset({errno.EBADF, errno.ENOTCONN, errno.ECONNRESET})


def _is_transient_ws_error(exc):
if isinstance(exc, TRANSIENT_WS_ERRORS):
return True
return isinstance(exc, OSError) and exc.errno in _DEAD_SOCKET_ERRNOS


class _RequestSentTrace:
"""httpcore `trace` callback that records whether the request was written
Expand Down Expand Up @@ -602,7 +616,7 @@ def _sub_routing_loop(self):
continue
except Exception as e:
if not self.closing:
if isinstance(e, TRANSIENT_WS_ERRORS):
if _is_transient_ws_error(e):
log(LogLevel.WARNING, "WSS connection reset or closed by peer")
else:
log(LogLevel.ERROR, "Some error trying to receive WSS")
Expand Down Expand Up @@ -872,7 +886,7 @@ def _ping_pong(self):
# A failed send on a socket the router already replaced says nothing
# about the new connection; halting would force a needless reconnect.
if not self.closing and conn is self._conn:
if isinstance(e, TRANSIENT_WS_ERRORS):
if _is_transient_ws_error(e):
log(
LogLevel.WARNING,
f"WSS ping send failed, connection closed by peer: {e!r}",
Expand Down
2 changes: 1 addition & 1 deletion pygqlc/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.10.0"
__version__ = "3.10.1"
88 changes: 88 additions & 0 deletions tests/pygqlc/gql_client/test_subscribe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import subscriptions as subs
from . import mutations as muts
import errno
import types
import time
Comment on lines 1 to 5

Expand Down Expand Up @@ -222,6 +223,60 @@ def test_sub_routing_loop_connection_reset_logged_as_warning(routing_client):
)


@pytest.mark.parametrize("errno_code", [errno.EBADF, errno.ENOTCONN])
def test_sub_routing_loop_dead_socket_oserror_logged_as_warning(
routing_client, errno_code
):
"""FEMSA-80: a peer close makes websocket-client's recv reply with a close
frame on an already-dead socket, raising a bare OSError (Errno 9 / EBADF,
or ENOTCONN). Python does not promote those to a Connection* subclass, so
they must be classified as a transient teardown — WARNING and reconnect,
not ERROR with a traceback. ECONNRESET is already ConnectionResetError."""
gql = routing_client
gql._conn.recv.side_effect = OSError(errno_code, "Bad file descriptor")

with (
patch.object(gql, "_new_conn", side_effect=_stop_loop_on(gql)) as new_conn,
_capture_logs() as records,
):
_run_routing_loop(gql)

assert gql.wss_conn_halted is True
assert new_conn.called
assert any(
level == LogLevel.WARNING and "reset or closed by peer" in msg
for level, msg in records
)
assert not any(
level == LogLevel.ERROR and "Some error trying to receive WSS" in msg
for level, msg in records
)


def test_sub_routing_loop_unrelated_oserror_logged_as_error(routing_client):
"""A bare OSError that is not a dead-socket errno must still surface at
ERROR. Blanket-catching OSError would hide real bugs (EACCES, etc.)."""
gql = routing_client
gql._conn.recv.side_effect = OSError(errno.EACCES, "Permission denied")

with (
patch.object(gql, "_new_conn", side_effect=_stop_loop_on(gql)) as new_conn,
_capture_logs() as records,
):
_run_routing_loop(gql)

assert gql.wss_conn_halted is True
assert new_conn.called
assert any(
level == LogLevel.ERROR and "Some error trying to receive WSS" in msg
for level, msg in records
)
assert not any(
level == LogLevel.WARNING and "reset or closed by peer" in msg
for level, msg in records
)


def test_sub_routing_loop_unexpected_error_logged_as_error(routing_client):
"""A non-transient error (not in TRANSIENT_WS_ERRORS) must still surface at ERROR
level and halt for reconnection."""
Expand Down Expand Up @@ -304,6 +359,39 @@ def sleepy(*args):
)


def test_ping_pong_bad_file_descriptor_logged_as_warning(routing_client):
"""FEMSA-80: OSError(EBADF) from a ping send is the same dead socket as on
recv — WARNING and halt, not ERROR."""
gql = routing_client
gql.pingIntervalTime = -1 # ping every iteration, independent of the clock

def send_effect(data):
raise OSError(errno.EBADF, "Bad file descriptor")

gql._conn.send.side_effect = send_effect

sleep_calls = [0]

def sleepy(*args):
sleep_calls[0] += 1
if sleep_calls[0] > 2:
gql.closing = True
return None

with patch("time.sleep", side_effect=sleepy), _capture_logs() as records:
_run_ping_pong(gql)

assert gql.wss_conn_halted is True
assert any(
level == LogLevel.WARNING and "WSS ping send failed" in msg
for level, msg in records
)
assert not any(
level == LogLevel.ERROR and "error trying to send ping" in msg
for level, msg in records
)


def test_ping_pong_unexpected_error_logged_as_error(routing_client):
"""A non-transient error from _ping_pong send must still surface at ERROR
level (and halt)."""
Expand Down
Loading
Loading