doronz88 / pymobiledevice3

Pure python3 implementation for working with iDevices (iPhone, etc...).
https://discord.gg/52mZGC3JXJ
GNU General Public License v3.0
1.49k stars 207 forks source link

Got `OSError: [WinError 1231]` for developer command #1217

Closed xkww3n closed 1 month ago

xkww3n commented 1 month ago

Test environment

Describe the bug Every developer command will fail with OSError: [WinError 1231] error. I've tried developer dvt sysmon system, developer dvt ls / and developer dvt simulate-location set 0 0, with or without --tunnel '' argument.

To Reproduce Steps to reproduce the behavior:

  1. Connect my iPhone, run pymobiledevice3 usbmux list, seen my device appeared.
  2. Run sudo pymobiledevice3 remote tunneld.
  3. Run pymobiledevice3 developer dvt sysmon system --tunnel '' in a new terminal window.
  4. Got OSError: [WinError 1231] error. Expected behavior Commands should work.

Logs

# pymobiledevice3 usbmux list
[
    {
        "BuildVersion": "22A3354",
        "ConnectionType": "Network",
        "DeviceClass": "iPhone",
        "DeviceName": "iPhone",
        "Identifier": "00008120-00046D523684201E",
        "ProductType": "iPhone15,2",
        "ProductVersion": "18.0",
        "UniqueDeviceID": "00008120-00046D523684201E"
    },
    {
        "BuildVersion": "22A3354",
        "ConnectionType": "USB",
        "DeviceClass": "iPhone",
        "DeviceName": "iPhone",
        "Identifier": "00008120-00046D523684201E",
        "ProductType": "iPhone15,2",
        "ProductVersion": "18.0",
        "UniqueDeviceID": "00008120-00046D523684201E"
    }
]
# sudo pymobiledevice3 remote tunneld
INFO:     Started server process [6560]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:49151 (Press CTRL+C to quit)
2024-09-27 11:05:55 DESKTOP-LN0U7IA pymobiledevice3.tunneld[6560] INFO [start-tunnel-task-usbmux-00008120-00046D523684201E-Network] Created tunnel --rsd fdd3:6894:6554::1 51548
INFO:     127.0.0.1:61184 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:61597 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:61712 - "GET / HTTP/1.1" 200 OK
# pymobiledevice3 developer dvt sysmon system --tunnel ''
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "c:\users\user1\.local\bin\pymobiledevice3.exe\__main__.py", line 7, in <module>
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\__main__.py", line 110, in main
    cli()
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\cli\cli_common.py", line 58, in handle_parse_result
    return super().handle_parse_result(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\click\core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\cli\cli_common.py", line 266, in tunneld
    return asyncio.run(self._tunneld(udid), debug=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\cli\cli_common.py", line 242, in _tunneld
    rsds = await async_get_tunneld_devices((TUNNELD_DEFAULT_ADDRESS[0], port))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\tunneld.py", line 481, in async_get_tunneld_devices
    return await _create_rsds_from_tunnels(tunnels)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\tunneld.py", line 520, in _create_rsds_from_tunnels
    await rsd.connect()
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\remote\remote_service_discovery.py", line 59, in connect
    await self.service.connect()
  File "C:\Users\user1\pipx\venvs\pymobiledevice3\Lib\site-packages\pymobiledevice3\remote\remotexpc.py", line 54, in connect
    self._reader, self._writer = await asyncio.open_connection(self.address[0], self.address[1])
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\streams.py", line 48, in open_connection
    transport, _ = await loop.create_connection(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\base_events.py", line 1086, in create_connection
    raise exceptions[0]
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\base_events.py", line 1070, in create_connection
    sock = await self._connect_sock(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\base_events.py", line 974, in _connect_sock
    await self.sock_connect(sock, address)
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\proactor_events.py", line 726, in sock_connect
    return await self._proactor.connect(sock, address)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\windows_events.py", line 854, in _poll
    value = callback(transferred, key, ov)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\scoop\apps\python311\current\Lib\asyncio\windows_events.py", line 641, in finish_connect
    ov.getresult()
OSError: [WinError 1231] 不能访问网络位置。有关网络排除故障的信息,请参阅 Windows 帮助。

Additional context None

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

doronz88 commented 1 month ago

Wasn't able to reproduce. Try adding debug prints and see why it fails

AnNEDoMini commented 1 month ago

how to fix https://stackoverflow.com/a/78460385

doronz88 commented 1 month ago

@AnNEDoMini I'm not familiar with the issue. If this is something you can reproduce, can you create a PR for it?

AnNEDoMini commented 1 month ago

@AnNEDoMini I'm not familiar with the issue. If this is something you can reproduce, can you create a PR for it?

I did not dig it too far. Mostly because not all of the windows desktops are experiencing this bug. Like half of them, sometimes. But with such fix - works perfectly. Dunno, may be add some sort of a switch/envvar --use_selector_event_loop or USE_SELECTOR_EVENT_LOOP

doronz88 commented 1 month ago

I could just always do that on windows. Is there a downside to this? This sounds more like a Python bug

AnNEDoMini commented 1 month ago

I could just always do that on windows. Is there a downside to this? This sounds more like a Python bug

Do not know any downsides, but as always it may lead to some wired interactions. Anyway, I'll add a PR with fix, add to readme(with clear statement "uses selector event loop..."); if anything goes wrong it is always possible to revert or improve solution.

doronz88 commented 1 month ago

Closing this now as I understand this is fixed. Feel free to reopen if needed