Skip to content

Free, open-source Google Maps scraper for lead generation (Python, no API key, no Docker). Exports name, phone, email, website, rating and Instagram/Facebook/LinkedIn/TikTok/YouTube/X links to CSV. 100 places with emails in 44 s; --grid beats Google's 120 cap. MX email check, ad-pixel detection. Skill for Claude Code, Codex and Cursor.

fix(engine): audit loop 3: broken-socket sends, silent dead tabs

Mahan Jafari1d0205d9d8b8
diff --git a/engine/cdp.py b/engine/cdp.py
index e6c6d82..f11d0c3 100644
--- a/engine/cdp.py
+++ b/engine/cdp.py
@@ -177,7 +177,11 @@ class CDP:
msg = {"id": mid, "method": method, "params": params or {}}
if session_id:
msg["sessionId"] = session_id
- self.ws.send(json.dumps(msg))
+ try:
+ self.ws.send(json.dumps(msg))
+ except OSError as e: # Chrome died and the reader has not noticed yet: same error as a lost reply
+ self._pending.pop(mid, None)
+ raise CDPError(f"{method}: connection lost: {e}") from e
try:
reply = q.get(timeout=timeout or self.timeout)
except queue.Empty:
@@ -260,18 +264,22 @@ class Page:
def wait_for(self, expression, timeout=15, every=0.25):
"""Poll a JavaScript expression until it is truthy; None if it never is. An error while the
- page navigates ("Execution context was destroyed") is retried; a dead connection is not."""
- end = time.time() + timeout
+ page navigates ("Execution context was destroyed") is retried; a dead connection is not, and
+ a tab that never answered at all raises its last error rather than look like an empty page."""
+ end, answered, err = time.time() + timeout, False, None
while time.time() < end:
try:
v = self.eval(expression)
- except CDPError:
+ answered = True
+ except CDPError as e:
if self.cdp.closed:
raise
- v = None
+ v, err = None, e
if v:
return v
time.sleep(every)
+ if err and not answered:
+ raise err
return None
def close(self):
diff --git a/engine/test_cdp.py b/engine/test_cdp.py
index 3074db8..cadea5a 100644
--- a/engine/test_cdp.py
+++ b/engine/test_cdp.py
@@ -127,6 +127,28 @@ class WebSocketAndCDP(unittest.TestCase):
self.c.wait_event("Page.frameNavigated", "S1", timeout=5) # subscribed after the death
self.assertLess(time.time() - t, 2)
+ def test_broken_socket_is_a_cdp_error(self):
+ self.c.ws.sock.close() # as if Chrome died a moment before the reader noticed
+ with self.assertRaisesRegex(cdp.CDPError, "connection lost"):
+ self.c.send("X.y")
+ self.assertEqual(self.c._pending, {})
+
+ def test_wait_for(self):
+ class Tab(cdp.Page):
+ def __init__(self, answers):
+ self.cdp, self.answers = self.c, list(answers)
+ def eval(self, expression, timeout=None):
+ a = self.answers.pop(0) if self.answers else None
+ if isinstance(a, Exception):
+ raise a
+ return a
+ Tab.c = self.c
+ gone = cdp.CDPError("Execution context was destroyed")
+ self.assertEqual(Tab([gone, None, 7]).wait_for("x", timeout=2, every=0.01), 7) # through a navigation
+ with self.assertRaisesRegex(cdp.CDPError, "Session with given id not found"):
+ Tab([cdp.CDPError("Session with given id not found")] * 500).wait_for("x", timeout=0.2, every=0.01)
+ self.assertIsNone(Tab([None] * 500).wait_for("x", timeout=0.2, every=0.01))
+
def test_quiet_browser_keeps_the_reader(self):
c = cdp.CDP(f"ws://127.0.0.1:{FakeDevTools().port}/devtools/browser/y", timeout=0.5)
time.sleep(1) # longer than the command timeout with no frame at all