← Commit history

v0.8.29: consent wait no longer spins at 100% CPU when the gateway socket closes

Drew Owens ·ba198ec694 · ·parent 6885464
_minimal_ws_roundtrip appended recv()'s empty EOF bytes forever and never re-checked its deadline, so a dropped gateway socket left 'auth' and 'scopes --auto-widen' busy-looping (one ran 3+ days at ~85% CPU). EOF now raises, which hands control back to the existing reconnect loop, and each recv gets only the time left before the overall deadline. Adds tests/test_ws_eof.py. Mirrors GitHub adom-inc/adom-google a8eabac.
5 files changed +57−14
VERSION+1−1
@@ -1 +1 @@-0.8.28+0.8.29
bin/adom-google+22−11
@@ -23,7 +23,7 @@ See `adom-google help`. """ import sys, os, json, base64, argparse, urllib.parse, ssl, socket, hashlib, secrets, struct, time, subprocess, shutil -ADOM_GOOGLE_VERSION = "0.8.28"+ADOM_GOOGLE_VERSION = "0.8.29" if sys.argv[1:2] == ["--version"]:     print(f"adom-google {ADOM_GOOGLE_VERSION}")     sys.exit(0)@@ -1529,26 +1529,39 @@ def _minimal_ws_roundtrip(ws_url, send_msgs, want_types, timeout=3600):         raw.sendall(header + mask + masked)      inbuf = bytearray(leftover)+    deadline = time.time() + timeout++    def _fill(n):+        """Block until inbuf holds n bytes. recv() returning b"" means the peer closed the+        socket (EOF); it will keep returning b"" instantly forever, so treat it as a drop and+        raise. The old loop appended the empty bytes and spun at ~100% CPU for days (Nov 2026).+        Each recv also gets only the time left, so the overall deadline is real."""+        nonlocal inbuf+        while len(inbuf) < n:+            left = deadline - time.time()+            if left <= 0:+                raise RuntimeError("timed out waiting for the gateway callback")+            raw.settimeout(left)+            chunk = raw.recv(4096)+            if not chunk:+                raise RuntimeError("gateway closed the connection")+            inbuf += chunk      def _recv_frame():         nonlocal inbuf         while True:-            while len(inbuf) < 2:-                inbuf += raw.recv(4096)+            _fill(2)             b0, b1 = inbuf[0], inbuf[1]             opcode = b0 & 0x0F             ln = b1 & 0x7F             idx = 2             if ln == 126:-                while len(inbuf) < 4:-                    inbuf += raw.recv(4096)+                _fill(4)                 ln = struct.unpack("!H", inbuf[2:4])[0]; idx = 4             elif ln == 127:-                while len(inbuf) < 10:-                    inbuf += raw.recv(4096)+                _fill(10)                 ln = struct.unpack("!Q", inbuf[2:10])[0]; idx = 10-            while len(inbuf) < idx + ln:-                inbuf += raw.recv(4096)+            _fill(idx + ln)             data = bytes(inbuf[idx:idx + ln])             del inbuf[:idx + ln]             if opcode == 0x8:  # close@@ -1562,8 +1575,6 @@ def _minimal_ws_roundtrip(ws_url, send_msgs, want_types, timeout=3600):     for m in send_msgs:         _send_text(json.dumps(m)) -    raw.settimeout(timeout)-    deadline = time.time() + timeout     try:         while time.time() < deadline:             data = _recv_frame()
package.json+1−1
@@ -1,6 +1,6 @@ {   "slug": "adom-google",-  "version": "0.8.28",+  "version": "0.8.29",   "type": "app",   "license": "MIT",   "description": "Drive your whole Google Workspace from the terminal: Gmail, Contacts, Drive, Sheets, Docs, Slides, Calendar, Tasks, Google Chat, plus a generic `api` passthrough to hit ANY Google API. Connect a personal @gmail (or another org's Google account) with ZERO setup via Adom's hosted Connector (`adom-google connect-personal`), or bring your own token-broker gateway (`provider set`): Adom staff use the private adom-google-adom provider. One-click OAuth, no client secret on your machine.",
page.json+1−1
@@ -4,7 +4,7 @@   "slug": "adom-google",   "title": "Adom Google",   "brief": "Drive your whole Google Workspace from the terminal, run by your AI. Read Gmail, write Google Docs, build Google Sheets and Google Slides, search Google Drive, manage Google Calendar and Google Tasks, and post to Google Chat, plus any Google API and a zero-setup personal Gmail.",-  "version": "0.8.28",+  "version": "0.8.29",   "visibility": "public",   "install": {     "binary_name": "adom-google"
tests/test_ws_eof.pyadded+32
@@ -0,0 +1,32 @@+"""Regression: the gateway consent wait spun at ~100% CPU for days when the socket hit EOF (2026-11)."""+import socket, threading, time, sys, resource, importlib.machinery, importlib.util+import os+path = sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "bin", "adom-google")+loader = importlib.machinery.SourceFileLoader("ag", path)+spec = importlib.util.spec_from_loader("ag", loader); ag = importlib.util.module_from_spec(spec); loader.exec_module(ag)++def server(mode, port_box):+    s = socket.socket(); s.bind(("127.0.0.1", 0)); s.listen(1); port_box.append(s.getsockname()[1])+    c, _ = s.accept(); c.recv(4096)+    c.sendall(b"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n")+    if mode == "eof":+        c.settimeout(0.5)+        try: c.recv(4096)+        except Exception: pass+        c.shutdown(socket.SHUT_WR); time.sleep(5); c.close()   # FIN, no close frame+    else:+        time.sleep(10); c.close()           # silent++for mode, to in (("eof", 30), ("silent", 3)):+    box = []; t = threading.Thread(target=server, args=(mode, box), daemon=True); t.start()+    while not box: time.sleep(0.01)+    c0 = resource.getrusage(resource.RUSAGE_SELF).ru_utime; w0 = time.time()+    try:+        ag._minimal_ws_roundtrip(f"ws://127.0.0.1:{box[0]}/", [{"type": "hi"}], {"done"}, timeout=to)+        print(mode, "UNEXPECTED return"); sys.exit(1)+    except Exception as e:+        wall = time.time()-w0; cpu = resource.getrusage(resource.RUSAGE_SELF).ru_utime-c0+        print(f"{mode}: raised {e!r} after {wall:.2f}s wall, {cpu:.2f}s cpu")+        assert cpu < 1.0, f"{mode}: busy-looped ({cpu:.1f}s cpu)"+        assert wall < to + 2, f"{mode}: ignored the deadline ({wall:.1f}s)"+print("OK: websocket wait exits on EOF and on deadline without spinning")