Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(sandbox): roll back partially opened tunnels (#5174)
injaneityca6a0e210509
diff --git a/libs/python/cua-sandbox/cua_sandbox/interfaces/tunnel.py b/libs/python/cua-sandbox/cua_sandbox/interfaces/tunnel.pyindex e3f66b8b5d..d6da76629d 100644--- a/libs/python/cua-sandbox/cua_sandbox/interfaces/tunnel.py+++ b/libs/python/cua-sandbox/cua_sandbox/interfaces/tunnel.py@@ -65,10 +65,16 @@ class _TunnelContext:return self._open().__await__()async def _open(self) -> Union[TunnelInfo, Dict[int | str, TunnelInfo]]:- for p in self._ports:- info = await self._t.forward_tunnel(p)- info._closer = self._close_one- self._infos.append(info)+ opened: List[TunnelInfo] = []+ try:+ for p in self._ports:+ info = await self._t.forward_tunnel(p)+ info._closer = self._close_one+ self._infos.append(info)+ opened.append(info)+ except BaseException:+ await asyncio.gather(*(info.close() for info in opened), return_exceptions=True)+ raisereturn self._result()def _result(self) -> Union[TunnelInfo, Dict[int | str, TunnelInfo]]:diff --git a/libs/python/cua-sandbox/tests/test_tunnel.py b/libs/python/cua-sandbox/tests/test_tunnel.pynew file mode 100644index 0000000000..5fd9e10016--- /dev/null+++ b/libs/python/cua-sandbox/tests/test_tunnel.py@@ -0,0 +1,91 @@+"""Tunnel acquisition rollback and normal cleanup."""++import asyncio+from unittest.mock import AsyncMock, call++import pytest+from cua_sandbox.interfaces.tunnel import Tunnel, TunnelInfo+from cua_sandbox.transport.base import Transport++pytestmark = pytest.mark.asyncio++[email protected]("use_context", [False, True])+async def test_partial_acquisition_closes_opened_tunnel(use_context):+ transport = AsyncMock(spec=Transport)+ info = TunnelInfo("localhost", 45001, 9222)+ error = RuntimeError("second forward failed")+ transport.forward_tunnel.side_effect = [info, error]+ context = Tunnel(transport).forward(9222, 8080)++ with pytest.raises(RuntimeError) as caught:+ if use_context:+ async with context:+ pytest.fail("context body must not run after failed acquisition")+ else:+ await context++ assert caught.value is error+ transport.close_tunnel.assert_awaited_once_with(info)+ await info.close()+ transport.close_tunnel.assert_awaited_once_with(info)+++async def test_cancelled_acquisition_closes_opened_tunnel():+ transport = AsyncMock(spec=Transport)+ info = TunnelInfo("localhost", 45001, 9222)+ second_started = asyncio.Event()++ async def forward(port):+ if port == 9222:+ return info+ second_started.set()+ await asyncio.Future()++ transport.forward_tunnel.side_effect = forward+ task = asyncio.ensure_future(Tunnel(transport).forward(9222, 8080))+ await second_started.wait()+ task.cancel()++ with pytest.raises(asyncio.CancelledError):+ await task++ transport.close_tunnel.assert_awaited_once_with(info)++[email protected]("close_error", [RuntimeError("close failed"), asyncio.CancelledError()])+async def test_rollback_attempts_all_closers_and_preserves_original_error(close_error):+ transport = AsyncMock(spec=Transport)+ first = TunnelInfo("localhost", 45001, 9222)+ second = TunnelInfo("localhost", 45002, 8080)+ error = RuntimeError("third forward failed")+ transport.forward_tunnel.side_effect = [first, second, error]+ transport.close_tunnel.side_effect = [close_error, None]++ with pytest.raises(RuntimeError) as caught:+ await Tunnel(transport).forward(9222, 8080, 3000)++ assert caught.value is error+ assert transport.close_tunnel.await_args_list == [call(first), call(second)]+ transport.close_tunnel.side_effect = None+ await first.close()+ await second.close()+ assert transport.close_tunnel.await_args_list == [call(first), call(second), call(first)]+++async def test_failed_attempt_preserves_previously_opened_tunnels():+ transport = AsyncMock(spec=Transport)+ first = TunnelInfo("localhost", 45001, 9222)+ second = TunnelInfo("localhost", 45002, 8080)+ third = TunnelInfo("localhost", 45003, 9222)+ transport.forward_tunnel.side_effect = [first, second, third, RuntimeError("forward failed")]+ context = Tunnel(transport).forward(9222, 8080)+ assert await context == {9222: first, 8080: second}++ with pytest.raises(RuntimeError, match="forward failed"):+ await context++ transport.close_tunnel.assert_awaited_once_with(third)+ await first.close()+ await second.close()+ assert transport.close_tunnel.await_args_list == [call(third), call(first), call(second)]