fix: server requests must not block reader thread; drop stale bindings on startup

This commit is contained in:
openbot committed 2026-10-06 19:09:12 +08:00
1 parent e36f596255
commit 0e4bcc5a1f
3 files changed
+18 -2

No files matched your search

+9
View File
@@ -226,6 +226,15 @@ class ACPClient:
self._resolve_response(msg)
def _handle_server_request(self, msg: dict[str, Any]) -> None:
# Handlers may BLOCK (e.g. session/request_permission waits minutes for
# the user's IM reply). Run them on a worker thread: blocking here would
# stall the reader loop, and every pending request would time out.
threading.Thread(
target=self._run_server_request, args=(msg,), daemon=True,
name=f"acp-srvreq-{msg.get('method')}-{msg.get('id')}",
).start()
def _run_server_request(self, msg: dict[str, Any]) -> None:
rid = msg["id"]
method = msg["method"]
params = msg.get("params") or {}
+6
View File
@@ -615,6 +615,12 @@ class SessionManager:
def _load_db(self) -> None:
assert self._db is not None
# Persisted bindings reference ACP session ids inside agent subprocesses
# that died with the previous service run. We do not resume them
# (loadSession handling is M2): drop them so the next message creates a
# fresh session instead of failing with "session not found".
self._db.execute("DELETE FROM bindings")
self._db.commit()
for channel, chat_id, agent, sid in self._db.execute(
"SELECT channel, chat_id, agent, acp_session_id FROM bindings"
):
+3 -2
View File
@@ -120,8 +120,9 @@ def test_sqlite_persistence_roundtrip(tmp_path):
m.stop() # closes clients but DB rows persist
m2 = make_manager(tmp_path, db_path=db)
b = m2.get_binding("wecom", "chatP")
assert b is not None and b.agent == "mock"
# bindings reference ACP sessions inside dead subprocesses: NOT restored
# (next message starts a fresh session), but conversation records are.
assert m2.get_binding("wecom", "chatP") is None
rec = m2._records["wecom:chatP"]
assert any(e["text"] == "persist me" for e in rec.text_log)
m2.stop()