Skip to content

The agent that grows with you

fix(gateway): offload remaining atomic_json_write calls in async paths

kshitij48c233d503f1
diff --git a/gateway/run.py b/gateway/run.py
index 11e6fd7f0fb..d9edbae9602 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -10082,10 +10082,12 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
return suspended
- def _clear_restart_failure_count(self, session_key: str) -> None:
+ async def _clear_restart_failure_count(self, session_key: str) -> None:
"""Clear the restart-failure counter for a session that completed OK.
Called after a successful agent turn to signal the loop is broken.
+ Offloaded to a thread because the caller (_handle_message_with_agent)
+ runs on the event loop and atomic_json_write calls os.fsync.
"""
import json
@@ -10097,7 +10099,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
if session_key in counts:
del counts[session_key]
if counts:
- atomic_json_write(path, counts, indent=None)
+ await asyncio.to_thread(atomic_json_write, path, counts, indent=None)
else:
path.unlink(missing_ok=True)
except Exception:
@@ -18339,7 +18341,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
# succeeded and subsequent messages should no longer receive
# the restart-interruption system note.
if session_key and _should_clear_resume_pending_after_turn(agent_result):
- self._clear_restart_failure_count(session_key)
+ await self._clear_restart_failure_count(session_key)
try:
await self.async_session_store.clear_resume_pending(session_key)
except Exception as _e:
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 4533c66befe..dc08a1cd10c 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -1578,7 +1578,8 @@ class GatewaySlashCommandsMixin:
)
except Exception:
self._restart_command_source = event.source
- atomic_json_write(
+ await asyncio.to_thread(
+ atomic_json_write,
_hermes_home / ".restart_notify.json",
notify_data,
indent=None,
@@ -1598,7 +1599,8 @@ class GatewaySlashCommandsMixin:
}
if event.platform_update_id is not None:
dedup_data["update_id"] = event.platform_update_id
- atomic_json_write(
+ await asyncio.to_thread(
+ atomic_json_write,
_hermes_home / ".restart_last_processed.json",
dedup_data,
indent=None,