Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PermissionError: [WinError 5] Access is Denied when importing trio #2688

Closed
chrisbeardy opened this issue Jul 6, 2023 · 7 comments
Closed

Comments

@chrisbeardy
Copy link

Trio version 0.22.1
Python version 3.11.4 and 3.10.12 tried
Windows 10

After creating a venv, ttrying to import trio in a python console gives a permission error.

Tthis only happens with 0.22.1, reverting to 0.22.0 works just fine

ttraceback:

Traceback (most recent call last):
  File "C:\Users\chrisb\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\chrisb\GitWS\async_and_thread_test\venv2\lib\site-packages\trio\__init__.py", line 22, in <module>
    from ._core import (
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\chrisb\GitWS\async_and_thread_test\venv2\lib\site-packages\trio\_core\__init__.py", line 27, in <module>
    from ._run import (
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\chrisb\GitWS\async_and_thread_test\venv2\lib\site-packages\trio\_core\_run.py", line 34, in <module>
    from ._thread_cache import start_thread_soon
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\chrisb\GitWS\async_and_thread_test\venv2\lib\site-packages\trio\_core\_thread_cache.py", line 66, in <module>
    set_os_thread_name = get_os_thread_name_func()
  File "C:\Users\chrisb\GitWS\async_and_thread_test\venv2\lib\site-packages\trio\_core\_thread_cache.py", line 42, in get_os_thread_name_func
    libpthread = ctypes.CDLL(libpthread_path)
  File "C:\Users\chrisb\AppData\Local\Programs\Python\Python310\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
PermissionError: [WinError 5] Access is denied
@jakkdl
Copy link
Member

jakkdl commented Jul 6, 2023

This is almost surely caused by #2470, and the particular lines in question are

# find the pthread library
# this will fail on windows
libpthread_path = ctypes.util.find_library("pthread")
if not libpthread_path:
return None
libpthread = ctypes.CDLL(libpthread_path)

The function "should" give up after not finding the pthread library - or at least that's what happens on the test machines. But apparently your system has the pthread library, but you need elevated permission to access it? All the information I found at the time indicated that pthread would never be available on windows, and therefore the feature of renaming threads would not be possible (at least in this way) on windows.

It would be simple to just bail out with a if sys.platform == "win32"; return - but this does make me a bit curious if it's possible to get thread renaming possible, at least on systems like yours, given the correct permissions. I certainly don't have access to such a system though, unless one can modify the CI machines to have it, so I can just push a small PR that bails on any windows system and we can do a quick bugfix release.

@chrisbeardy
Copy link
Author

I wonder if its related to the fact i have WSL installed? If itts nott meant to work on windows anyway then I guess just bailing out is no harm

@jakkdl
Copy link
Member

jakkdl commented Jul 6, 2023

Looking at it a bit more, it looks like you can get pthreads to run on windows:
https://stackoverflow.com/questions/7542286/how-to-run-pthreads-on-windows
https://stackoverflow.com/questions/19467455/how-to-set-up-pthreads-on-windows

So should maybe just guard that line with a try: / except Exception:, but that might just lead to errors later on in case pthread behaves slightly differently - so should make sure to test that it works.

@A5rocks
Copy link
Contributor

A5rocks commented Jul 6, 2023

For what it's worth, this doesn't error on my (Windows 11) machine which has WSL2 installed (and I think I had WSL1 temporarily, if that matters). ctypes.util.find_library("pthread") returns None too, as it should. I think gating it behind a Windows check is a nice quick fix. Looking at pthreads-win32 which seems to be the normal thing to use on Windows, it looks like it matches what we do unless there's some fancy BSD compat things going on. This means just catching the PermissionError should work too.

(sidenote) I just found https://pypi.org/project/pupil-pthreads-win/ which is a pretty funny package, honestly.

@jakkdl
Copy link
Member

jakkdl commented Jul 7, 2023

Oh, if that works we could include that as an optional dependency? Would be lovely if either of you tried it and see if the associated tests in trio/_tests/test_threads.py passes all the way.
But yeah, sounds like catching the perm error is the way to go, and could also catch other esoteric problems on other systems.

@A5rocks
Copy link
Contributor

A5rocks commented Jul 11, 2023

Should be fixed by #2693.

Patch release is going to come out soon-ish!

@A5rocks A5rocks closed this as completed Jul 11, 2023
@A5rocks
Copy link
Contributor

A5rocks commented Jul 12, 2023

Hi, this should be fixed in https://pypi.org/project/trio/0.22.2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants