Skip to content

Commit 038a8ac

Browse files
committed
remote: stop remoted while quic tunnel being established (#571)
1 parent 55d9a5a commit 038a8ac

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

pymobiledevice3/cli/remote.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pymobiledevice3.exceptions import NoDeviceConnectedError
1010
from pymobiledevice3.remote.bonjour import get_remoted_addresses
1111
from pymobiledevice3.remote.remote_service_discovery import RSD_PORT, RemoteServiceDiscoveryService
12-
from pymobiledevice3.remote.utils import stop_remoted
12+
from pymobiledevice3.remote.utils import resume_remoted_if_required, stop_remoted, stop_remoted_if_required
1313

1414
logger = logging.getLogger(__name__)
1515

@@ -70,8 +70,11 @@ def rsd_info(service_provider: RemoteServiceDiscoveryService, color: bool):
7070

7171
async def start_quic_tunnel(service_provider: RemoteServiceDiscoveryService, secrets: TextIO) -> None:
7272
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
73+
74+
stop_remoted_if_required()
7375
with create_core_device_tunnel_service(service_provider, autopair=True) as service:
7476
async with service.start_quic_tunnel(private_key, secrets_log_file=secrets) as tunnel_result:
77+
resume_remoted_if_required()
7578
if secrets is not None:
7679
print(click.style('Secrets: ', bold=True, fg='magenta') +
7780
click.style(secrets.name, bold=True, fg='white'))

pymobiledevice3/remote/utils.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
REMOTED_PATH = '/usr/libexec/remoted'
1010

1111

12-
def _get_remoted_process() -> psutil.Process:
12+
def get_remoted_process() -> psutil.Process:
1313
for process in psutil.process_iter():
1414
if process.pid == 0:
1515
# skip kernel task
@@ -21,24 +21,42 @@ def _get_remoted_process() -> psutil.Process:
2121
continue
2222

2323

24-
@contextlib.contextmanager
25-
def stop_remoted() -> Generator[None, None, None]:
24+
def stop_remoted_if_required() -> None:
2625
if platform.system() != 'Darwin':
2726
# only Darwin systems require it
28-
yield
2927
return
3028

31-
remoted = _get_remoted_process()
29+
remoted = get_remoted_process()
3230
if remoted.status() == 'stopped':
3331
# process already stopped, we don't need to do anything
34-
yield
3532
return
3633

3734
try:
3835
remoted.suspend()
3936
except psutil.AccessDenied:
4037
raise AccessDeniedError()
38+
39+
40+
def resume_remoted_if_required() -> None:
41+
if platform.system() != 'Darwin':
42+
# only Darwin systems require it
43+
return
44+
45+
remoted = get_remoted_process()
46+
if remoted.status() == 'running':
47+
# process already running, we don't need to do anything
48+
return
49+
50+
try:
51+
remoted.resume()
52+
except psutil.AccessDenied:
53+
raise AccessDeniedError()
54+
55+
56+
@contextlib.contextmanager
57+
def stop_remoted() -> Generator[None, None, None]:
58+
stop_remoted_if_required()
4159
try:
4260
yield
4361
finally:
44-
remoted.resume()
62+
resume_remoted_if_required()

0 commit comments

Comments
 (0)