Skip to content

Automate browser based workflows with AI

Honor CDP proxy denylist env knobs in production; second-signal fast drain exit (SKY-12533) (#7547)

Aaron Perez0373f3426d96
diff --git a/skyvern/proxy/adapters/websocket_server.py b/skyvern/proxy/adapters/websocket_server.py
index d2ee29be2..6e600189e 100644
--- a/skyvern/proxy/adapters/websocket_server.py
+++ b/skyvern/proxy/adapters/websocket_server.py
@@ -853,19 +853,36 @@ class CdpProxyServer:
)
LOG.info("CDP proxy draining", drain_timeout_seconds=drain_timeout)
server.close(close_connections=False)
+ # Re-arm the handlers: a second signal ends the drain early, so a
+ # dev Ctrl-C (or an operator's repeated stop) stays interruptible
+ # instead of sitting out the full drain budget.
+ force = asyncio.Event()
+ for sig in installed:
+ loop.remove_signal_handler(sig)
+ loop.add_signal_handler(sig, force.set)
+ drained = asyncio.create_task(server.wait_closed())
+ forced = asyncio.create_task(force.wait())
try:
- await asyncio.wait_for(server.wait_closed(), timeout=drain_timeout)
- except asyncio.TimeoutError:
- # Server.close() is idempotent — a second call cannot upgrade the
- # drain to a force-close — so stragglers are closed directly. Each
- # close() bounds its own handshake, so this always terminates.
- # `handlers` is an undocumented websockets.asyncio Server attribute
- # (its _close() iterates the same dict); the lifecycle tests here
- # exercise it, so a websockets bump that renames it fails loudly.
- LOG.warning("CDP proxy drain timeout expired, force-closing remaining connections")
- stragglers = [asyncio.create_task(conn.close(1001)) for conn in list(server.handlers)]
- if stragglers:
- await asyncio.wait(stragglers)
+ done, _ = await asyncio.wait(
+ {drained, forced}, timeout=drain_timeout, return_when=asyncio.FIRST_COMPLETED
+ )
+ if drained not in done:
+ # Server.close() is idempotent — a second call cannot upgrade the
+ # drain to a force-close — so stragglers are closed directly. Each
+ # close() bounds its own handshake, so this always terminates.
+ # `handlers` is an undocumented websockets.asyncio Server attribute
+ # (its _close() iterates the same dict); the lifecycle tests here
+ # exercise it, so a websockets bump that renames it fails loudly.
+ reason = "second signal" if forced in done else "drain timeout expired"
+ LOG.warning("CDP proxy force-closing remaining connections", reason=reason)
+ stragglers = [asyncio.create_task(conn.close(1001)) for conn in list(server.handlers)]
+ if stragglers:
+ await asyncio.wait(stragglers)
+ finally:
+ for task in (drained, forced):
+ task.cancel()
+ with contextlib.suppress(asyncio.CancelledError):
+ await task
finally:
for sig in installed:
loop.remove_signal_handler(sig)
diff --git a/tests/unit/proxy/test_websocket_server.py b/tests/unit/proxy/test_websocket_server.py
index 99c4dedd1..5e7cca20a 100644
--- a/tests/unit/proxy/test_websocket_server.py
+++ b/tests/unit/proxy/test_websocket_server.py
@@ -471,3 +471,33 @@ async def test_serve_forever_force_closes_stragglers_after_drain_timeout(
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
+
+
+async def test_second_signal_during_drain_force_closes_immediately(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ # Drain budget is huge on purpose: only the second signal can end the wait,
+ # so a fast completion proves the fast-exit path rather than the timeout.
+ monkeypatch.setenv("CDP_PROXY_DRAIN_TIMEOUT_SECONDS", "3600")
+ port = _free_port()
+ task = asyncio.create_task(_lifecycle_server(port).serve_forever())
+ client = None
+ try:
+ await _wait_for_listener(port, up=True)
+ client = await websockets_client.connect(f"ws://127.0.0.1:{port}/s1")
+ os.kill(os.getpid(), signal.SIGTERM)
+ await _wait_for_listener(port, up=False)
+ os.kill(os.getpid(), signal.SIGTERM)
+
+ await asyncio.wait_for(task, timeout=10)
+ with pytest.raises(websockets_exceptions.ConnectionClosed):
+ await asyncio.wait_for(client.recv(), timeout=5)
+ assert client.protocol.close_code == 1001
+ finally:
+ if client is not None:
+ await client.close()
+ if not task.done():
+ task.cancel()
+ with contextlib.suppress(asyncio.CancelledError):
+ await task