doronz88 / pymobiledevice3

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

RuntimeError: Task RemoteServiceDiscoveryService.close() got Future attached to a different loop #1031

Open HuaSheng2000 opened 1 month ago

HuaSheng2000 commented 1 month ago

Test environment

Describe the bug Cannot connect to device through tunnel

To Reproduce Steps to reproduce the behavior:

  1. Start the server by pymobiledevice3 remote tunneld.
  2. use this command to connect device pymobiledevice3 developer dvt ls / --tunnel 'xxxxxx'.

Expected behavior get files in root folder

Logs

Traceback (most recent call last):
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "D:\projects\dev-tools\.conda\envs\common-py310\Scripts\pymobiledevice3.exe\__main__.py", line 7, in <module>
    sys.exit(main())
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\__main__.py", line 100, in main
    cli()
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\cli\cli_common.py", line 58, in handle_parse_result
    return super().handle_parse_result(
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\click\core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\cli\cli_common.py", line 250, in tunneld
    get_asyncio_loop().run_until_complete(rsd.close())
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\remote\remote_service_discovery.py", line 116, in close
    await self.service.close()
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\remote\remotexpc.py", line 61, in close
    await self.service_connection.aio_close()
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\service_connection.py", line 108, in aio_close
    await self.writer.wait_closed()
  File "D:\projects\dev-tools\.conda\envs\common-py310\lib\asyncio\streams.py", line 343, in wait_closed
    await self._protocol._get_close_waiter(self)
RuntimeError: Task <Task pending name='Task-2' coro=<RemoteServiceDiscoveryService.close() running at D:\projects\dev-tools\.conda\envs\common-py310\lib\site-packages\pymobiledevice3\remote\remote_service_discovery.py:116> cb=[_run_until_complete_cb() at D:\projects\dev-tools\.conda\envs\common-py310\lib\asyncio\base_events.py:184]> got Future <Future pending> attached to a different loop

Additional context In version 4.6.0, I could connect to the device via tunnel, but in version 4.7.0, I can no longer connect to the device via tunnel. According to the update log, it might be due to this commit because it is related to the loop. https://github.com/doronz88/pymobiledevice3/commit/2f39345137c94f5d5b13665ed4660496e69faf9d When I reverted the changes, the error disappeared.

doronz88 commented 1 month ago

I wasn't able to reproduce the issue and this change was added because the old function is deprecated should not be used. Feel free to submit a PR that resolves this issue without using a deprecated API

doronz88 commented 1 month ago

Actually - I strongly believe this bug has something to do with this "conda-env" - try to execute it globally and verify

doronz88 commented 1 month ago

Please verify if #1032 solves your issue. This removes all usages of this API entirely since it shouldn't really exist in the first place.

doronz88 commented 1 month ago

Feel free to reopen this if the issue persists

HuaSheng2000 commented 1 month ago

I recreated a clean python10 environment for pymobiledevice3 by conda create -n pymobiledevice3-py310 python=3.10

install the pymobiledevice3 by pip install pymobiledevice3

here is the pip list output

Package               Version
--------------------- --------------
aiofiles              23.2.1
annotated-types       0.7.0
ansicon               1.89.0
anyio                 4.3.0
arrow                 1.3.0
asn1                  2.7.0
asttokens             2.4.1
async-timeout         4.0.3
blessed               1.20.0
bpylist2              4.1.1
cached-property       1.5.2
certifi               2024.2.2
cffi                  1.16.0
charset-normalizer    3.3.2
click                 8.1.7
colorama              0.4.6
coloredlogs           15.0.1
construct             2.10.70
cryptography          42.0.7
daemonize             2.5.0
decorator             5.1.1
developer-disk-image  0.0.2
dnspython             2.6.1
editor                1.6.6
email_validator       2.1.1
enum-compat           0.0.3
exceptiongroup        1.2.1
executing             2.0.1
fastapi               0.111.0
fastapi-cli           0.0.4
gpxpy                 1.5.0
h11                   0.14.0
hexdump               3.3
httpcore              1.0.5
httptools             0.6.1
httpx                 0.27.0
humanfriendly         10.0
hyperframe            6.0.1
idna                  3.7
ifaddr                0.2.0
inquirer3             0.6.0
ipsw-parser           1.2.3
ipython               8.24.0
itsdangerous          2.2.0
jedi                  0.19.1
Jinja2                3.1.4
jinxed                1.2.1
la-panic              0.4.9
lzfse                 0.4.2
markdown-it-py        3.0.0
MarkupSafe            2.1.5
matplotlib-inline     0.1.7
mdurl                 0.1.2
nest-asyncio          1.6.0
opack                 0.1.0
orjson                3.10.3
packaging             24.0
parameter-decorators  0.0.2
parso                 0.8.4
pillow                10.3.0
pip                   24.0
plumbum               1.8.3
prompt-toolkit        3.0.43
psutil                5.9.8
pure-eval             0.2.2
pycparser             2.22
pycrashreport         1.2.3
pycryptodome          3.20.0
pydantic              2.7.1
pydantic_core         2.18.2
pydantic-extra-types  2.7.0
pydantic-settings     2.2.1
Pygments              2.18.0
pygnuutils            0.1.1
pyimg4                0.8.5
pykdebugparser        1.2.4
pylzss                0.3.8
pymobiledevice3       4.8.5
pyreadline3           3.4.1
python-dateutil       2.9.0.post0
python-dotenv         1.0.1
python-multipart      0.0.9
python-pcapng         2.1.1
pytun-pmd3            2.0.7
pyusb                 1.2.1
pywin32               306
PyYAML                6.0.1
qh3                   1.0.7
readchar              4.1.0
remotezip             0.12.3
requests              2.32.2
rich                  13.7.1
runs                  1.2.2
setuptools            69.5.1
shellingham           1.5.4
six                   1.16.0
sniffio               1.3.1
srptools              1.0.1
sslpsk-pmd3           1.0.3
stack-data            0.6.3
starlette             0.37.2
termcolor             2.4.0
tqdm                  4.66.4
traitlets             5.14.3
typer                 0.12.3
types-python-dateutil 2.9.0.20240316
typing_extensions     4.12.0
ujson                 5.10.0
urllib3               2.2.1
uvicorn               0.29.0
watchfiles            0.21.0
wcwidth               0.2.13
websockets            12.0
wheel                 0.43.0
wsproto               1.2.0
xmod                  1.8.1
xonsh                 0.16.0
zeroconf              0.132.2

here is the error.

Traceback (most recent call last):
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\Scripts\pymobiledevice3.exe\__main__.py", line 7, in <module>
    sys.exit(main())
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\pymobiledevice3\__main__.py", line 100, in main
    cli()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\pymobiledevice3\cli\cli_common.py", line 58, in handle_parse_result
    return super().handle_parse_result(
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\click\core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\pymobiledevice3\cli\cli_common.py", line 250, in tunneld
    asyncio.run(rsd.close(), debug=True)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\pymobiledevice3\remote\remote_service_discovery.py", line 121, in close
    await self.service.close()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\pymobiledevice3\remote\remotexpc.py", line 61, in close
    await self.service_connection.aio_close()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\site-packages\pymobiledevice3\service_connection.py", line 106, in aio_close
    self.writer.close()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\asyncio\streams.py", line 337, in close
    return self._transport.close()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\asyncio\proactor_events.py", line 109, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\asyncio\base_events.py", line 753, in call_soon
    self._check_closed()
  File "D:\projects\dev-tools\.conda\envs\pymobiledevice3-py310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

What information do you need to reproduce this issue?

HuaSheng2000 commented 1 month ago

I can't reopen this issue. https://github.com/orgs/community/discussions/41926#discussioncomment-9125706

Do I need to new an issue?

doronz88 commented 1 month ago

Reopened myself - but as I'm not able to reproduce your issue using my own venv (not using coda) I pretty certain its conda specific. Please verify

HuaSheng2000 commented 1 month ago

I tried not using conda, downloading python from python official website. Still the same error.

  1. downloaded python3.10.11 from here. https://www.python.org/downloads/release/python-31011/

  2. Create venv environment

    python -m venv ./venv
  3. install pymobiledevice3(4.8.5). (The output of pip list is the same as in conda, except for pip, setuptool, wheel)

  4. activate venv and start server.

  5. connect device by pymobiledevice3 developer dvt ls / --tunnel 'xxxxxx'.

  6. Same error

    Traceback (most recent call last):
    File "D:\projects\dev-tools\python3.10\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "D:\projects\dev-tools\python3.10\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
    File "D:\projects-path\venv\Scripts\pymobiledevice3.exe\__main__.py", line 7, in <module>
    sys.exit(main())
    File "D:\projects-path\venv\lib\site-packages\pymobiledevice3\__main__.py", line 100, in main
    cli()
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 943, in make_context
    self.parse_args(ctx, args)
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
    File "D:\projects-path\venv\lib\site-packages\pymobiledevice3\cli\cli_common.py", line 58, in handle_parse_result
    return super().handle_parse_result(
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
    File "D:\projects-path\venv\lib\site-packages\click\core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
    File "D:\projects-path\venv\lib\site-packages\pymobiledevice3\cli\cli_common.py", line 250, in tunneld
    asyncio.run(rsd.close(), debug=True)
    File "D:\projects\dev-tools\python3.10\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
    File "D:\projects\dev-tools\python3.10\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
    File "D:\projects-path\venv\lib\site-packages\pymobiledevice3\remote\remote_service_discovery.py", line 121, in close
    await self.service.close()
    File "D:\projects-path\venv\lib\site-packages\pymobiledevice3\remote\remotexpc.py", line 61, in close
    await self.service_connection.aio_close()
    File "D:\projects-path\venv\lib\site-packages\pymobiledevice3\service_connection.py", line 106, in aio_close
    self.writer.close()
    File "D:\projects\dev-tools\python3.10\lib\asyncio\streams.py", line 337, in close
    return self._transport.close()
    File "D:\projects\dev-tools\python3.10\lib\asyncio\proactor_events.py", line 109, in close
    self._loop.call_soon(self._call_connection_lost, None)
    File "D:\projects\dev-tools\python3.10\lib\asyncio\base_events.py", line 753, in call_soon
    self._check_closed()
    File "D:\projects\dev-tools\python3.10\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
    RuntimeError: Event loop is closed

If there is no problem with the code, it may be related to the system. follow this link. https://stackoverflow.com/a/70758881

HuaSheng2000 commented 1 month ago

I will try to reproduce this issue on manjaro later, please wait.

doronz88 commented 1 month ago

I wasn't able to reproduce on two different win11 systems

HuaSheng2000 commented 1 month ago

Sorry I left out an important detail, I'm using wifi. usbmux doesn't have this issue.