From 935c8cf656accc99982e18a47367e777d10566b1 Mon Sep 17 00:00:00 2001 From: goodboy Date: Wed, 12 Aug 2026 13:48:40 -0400 Subject: [PATCH] Guard receive-path transport rendering Skip raw packet, decoded message, peer, and channel formatting when transport logging is disabled. Keep message processing and wire reads outside the guards so logging controls never affect IPC flow. Also narrow the remaining pretty-struct TODO to require a non-raising formatter with native-repr fallback. (this patch was generated in some part by `opencode` using `gpt-5.6-sol` (`openai`)) --- tractor/ipc/_transport.py | 5 ++++- tractor/runtime/_rpc.py | 35 +++++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/tractor/ipc/_transport.py b/tractor/ipc/_transport.py index 0a38d14d..dd3be179 100644 --- a/tractor/ipc/_transport.py +++ b/tractor/ipc/_transport.py @@ -309,7 +309,10 @@ class MsgpackTransport(MsgTransport): log.transport(f'received header {size}') # type: ignore msg_bytes: bytes = await self.recv_stream.receive_exactly(size) - log.transport(f"received {msg_bytes}") # type: ignore + if log.at_least_level('transport'): + log.transport( # type: ignore + f'received {msg_bytes}' + ) try: # NOTE: lookup the `trio.Task.context`'s var for # the current `MsgCodec`. diff --git a/tractor/runtime/_rpc.py b/tractor/runtime/_rpc.py index 6c0fb32a..d2c9ea30 100644 --- a/tractor/runtime/_rpc.py +++ b/tractor/runtime/_rpc.py @@ -1003,20 +1003,18 @@ async def process_messages( task_status.started(loop_cs) async for msg in chan: - log.transport( # type: ignore - f'IPC msg from peer\n' - f'<= {chan.aid.reprol()}\n\n' + if log.at_least_level('transport'): + log.transport( # type: ignore + f'IPC msg from peer\n' + f'<= {chan.aid.reprol()}\n\n' - # TODO: use of the pprinting of structs is - # FRAGILE and should prolly not be - # - # avoid fmting depending on loglevel for perf? - # -[ ] specifically `pretty_struct.pformat()` sub-call..? - # - how to only log-level-aware actually call this? - # -[ ] use `.msg.pretty_struct` here now instead! - # f'{pretty_struct.pformat(msg)}\n' - f'{msg}\n' - ) + # TODO: pretty-printing structs is FRAGILE; + # -[ ] add a non-raising log formatter with + # native-repr fallback before using + # `.msg.pretty_struct` here. + # f'{pretty_struct.pformat(msg)}\n' + f'{msg}\n' + ) match msg: # msg for an ongoing IPC ctx session, deliver msg to @@ -1262,11 +1260,12 @@ async def process_messages( log.exception(message) raise RuntimeError(message) - log.transport( - 'Waiting on next IPC msg from\n' - f'peer: {chan.aid.reprol()}\n' - f'|_{chan}\n' - ) + if log.at_least_level('transport'): + log.transport( + 'Waiting on next IPC msg from\n' + f'peer: {chan.aid.reprol()}\n' + f'|_{chan}\n' + ) # END-OF `async for`: # IPC disconnected via `trio.EndOfChannel`, likely