Removing Addon with compiled modules(e.g. cython) on Windows will cause PermissionError: [WinError 5] Access is denied #77837

Closed
opened 2020-06-13 17:36:16 +02:00 by MACHIN3 · 25 comments

System Information
Operating system: Linux-4.15.0-101-generic-x86_64-with-debian-buster-sid 64 Bits
Graphics card: GeForce GTX 1050/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 435.21

Blender Version
Broken: version: 2.83.0, branch: master, commit date: 2020-06-03 14:38, hash: 211b6c29f7

Short description of error
Any addon with complied modules can't be properly removed on Windows using bpy.ops.preferences.addon_remove()

This error will pop up:

Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\startup\bl_operators\userpref.py", line 775, in execute
    shutil.rmtree(path)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 516, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 395, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 400, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 398, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\x\\AppData\\Roaming\\Blender Foundation\\Blender\\2.83\\scripts\\addons\\RemoveAddonException\

Exact steps for others to reproduce the error

  • install the RemoveAddonException addon from the zip (tiny addon to test that has a single operator called "MACHIN3: Printer", which prints using a custom print function in utils/print.py[d])
  • optionally confirm its working by running MACHIN3: Printer from the operator search menu, then take look at the terminal
  • remove the addon from the addon prefs
    RemoveAddonException.zip

Thoughts

Addon removal is done in /opt/blender 2.83/2.83/scripts/startup/bl_operators/userpref.py, specifically in class PREFERENCES_OT_addon_remove().

in line 776, it's doing this:
shutil.rmtree(path)

shutil.rmtree() takes two optional arguments:

  1. ignore_errors
  2. onerror

If ignore_errors were to be set to True, the addon removal would finish without the exception, which in my view would be preferable to the current implementation.
It won't completely remove the addon folder however, as the compiled module will remain in the addon's folder.

Using onerror a custom function can be passed in to deal with the problem. It turns out, that while python doesn't like the module to be removed on Windows, it doesn't mind it being moved elsewhere. So what could be done is this:

def move_file(func, path, err):
     os.rename(path, os.path.join(bpy.utils.resources('SCRIPTS'), 'trash', os.path.basename(path)))
...
shutil.rmtree(path, onerror=move_file)

In this example. the file is moved to a trash folder in the scripts users resources, so parallel to the addons folder.
Doing this Blender is able to completely remove the addon folder.
Perhaps, Blender could then also remove that trash folder when closing, or the next time it starts and before any addons are registered?

IMO, at the very least, shutil.rmtree() should use the ignore_errors argument.
The best solution would be to have python release the module somehow, but based on my research, that's not possible? Any input is appreciated.

References (aka other people having this issue)

**System Information** Operating system: Linux-4.15.0-101-generic-x86_64-with-debian-buster-sid 64 Bits Graphics card: GeForce GTX 1050/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 435.21 **Blender Version** Broken: version: 2.83.0, branch: master, commit date: 2020-06-03 14:38, hash: `211b6c29f7` **Short description of error** Any addon with complied modules can't be properly removed on Windows using `bpy.ops.preferences.addon_remove()` This error will pop up: ``` Error: Traceback (most recent call last): File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\startup\bl_operators\userpref.py", line 775, in execute shutil.rmtree(path) File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 516, in rmtree return _rmtree_unsafe(path, onerror) File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 395, in _rmtree_unsafe _rmtree_unsafe(fullname, onerror) File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 400, in _rmtree_unsafe onerror(os.unlink, fullname, sys.exc_info()) File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\shutil.py", line 398, in _rmtree_unsafe os.unlink(fullname) PermissionError: [WinError 5] Access is denied: 'C:\\Users\\x\\AppData\\Roaming\\Blender Foundation\\Blender\\2.83\\scripts\\addons\\RemoveAddonException\ ``` **Exact steps for others to reproduce the error** * install the RemoveAddonException addon from the zip (tiny addon to test that has a single operator called "MACHIN3: Printer", which prints using a custom print function in `utils/print.py[d]`) * optionally confirm its working by running `MACHIN3: Printer` from the operator search menu, then take look at the terminal * remove the addon from the addon prefs [RemoveAddonException.zip](https://archive.blender.org/developer/F8615784/RemoveAddonException.zip) ---- **Thoughts** Addon removal is done in `/opt/blender 2.83/2.83/scripts/startup/bl_operators/userpref.py`, specifically in `class PREFERENCES_OT_addon_remove()`. in line 776, it's doing this: `shutil.rmtree(path)` shutil.rmtree() takes two optional arguments: 1. `ignore_errors` 2. `onerror` If `ignore_errors` were to be set to `True`, the addon removal would finish without the exception, which in my view would be preferable to the current implementation. It won't completely remove the addon folder however, as the compiled module will remain in the addon's folder. Using `onerror` a custom function can be passed in to deal with the problem. It turns out, that while python doesn't like the module to be removed on Windows, it doesn't mind it being moved elsewhere. So what could be done is this: ``` def move_file(func, path, err): os.rename(path, os.path.join(bpy.utils.resources('SCRIPTS'), 'trash', os.path.basename(path))) ... shutil.rmtree(path, onerror=move_file) ``` In this example. the file is moved to a trash folder in the scripts users resources, so parallel to the addons folder. Doing this Blender is able to completely remove the addon folder. Perhaps, Blender could then also remove that trash folder when closing, or the next time it starts and before any addons are registered? IMO, at the very least, `shutil.rmtree()` should use the `ignore_errors` argument. The best solution would be to have python release the module somehow, but based on my research, that's not possible? Any input is appreciated. **References (aka other people having this issue)** * https://discuss.python.org/t/how-to-unload-native-extensions-pyd-on-windows/3749 * https://python-forum.io/Thread-How-To-Unload-Windows-Native-Extension-pyd * https://github.com/rlguy/Blender-FLIP-Fluids/wiki/Addon-Installation-and-Uninstallation
Author

Added subscriber: @MACHIN3

Added subscriber: @MACHIN3
MACHIN3 changed title from Removing Addon with compiled modules(ex. cython) on WIndows will cause PermissionError: [WinError 5] Access is denied: to Removing Addon with compiled modules(e.g. cython) on WIndows will cause PermissionError: [WinError 5] Access is denied: 2020-06-13 17:36:52 +02:00
MACHIN3 changed title from Removing Addon with compiled modules(e.g. cython) on WIndows will cause PermissionError: [WinError 5] Access is denied: to Removing Addon with compiled modules(e.g. cython) on Windows will cause PermissionError: [WinError 5] Access is denied 2020-06-13 17:48:30 +02:00

Added subscriber: @rlguy

Added subscriber: @rlguy

This issue also affects the FLIP Fluids addon and is a common error when our users try to uninstall our addon. If there is a solution or workaround to to this issue, it would be greatly appreciated!

This issue also affects the FLIP Fluids addon and is a common error when our users try to uninstall our addon. If there is a solution or workaround to to this issue, it would be greatly appreciated!
Member

Added subscriber: @LazyDodo

Added subscriber: @LazyDodo
Member

Changed status from 'Needs Triage' to: 'Archived'

Changed status from 'Needs Triage' to: 'Archived'
Ray molenkamp self-assigned this 2020-06-13 20:09:09 +02:00
Member

There's not a whole lot we can do about this from the blender side, i took a quick peek at the python source and they just seem to lack any kind of management that would allow you to do this.

you may be able to forcefully unload the library with ctypes and forcefully calling FreeLibrary a few times in a row, but that'll be pretty far off the supported path for both blender and python and could blow up in your face given python may not enjoy the dll being gone at shutdown time, you may have more control over this in a pure ctypes based solution (rather than a python module solution)

either way, not a bug we can fix in blender, so by the tracker rules i'll have to close this ticket.

There's not a whole lot we can do about this from the blender side, i took a quick peek at the python source and they just seem to lack any kind of management that would allow you to do this. you may be able to forcefully unload the library with ctypes and forcefully calling FreeLibrary a few times in a row, but that'll be pretty far off the supported path for both blender and python and could blow up in your face given python may not enjoy the dll being gone at shutdown time, you may have more control over this in a pure ctypes based solution (rather than a python module solution) either way, not a bug we can fix in blender, so by the tracker rules i'll have to close this ticket.
Author

Why not at least prevent the error using the ignore_errors arg?

Why not at least prevent the error using the `ignore_errors` arg?
Member

Changed status from 'Archived' to: 'Needs Triage'

Changed status from 'Archived' to: 'Needs Triage'
Member

Sorry, overlooked that bit, i'll leave that for the python guys to decide, for unloading the library there is really nothing we can do.

Sorry, overlooked that bit, i'll leave that for the python guys to decide, for unloading the library there is really nothing we can do.

Added subscriber: @bent

Added subscriber: @bent
Member

Added subscriber: @EAW

Added subscriber: @EAW
Member

In #77837#953299, @rlguy wrote:
This issue also affects the FLIP Fluids addon and is a common error when our users try to uninstall our addon. If there is a solution or workaround to to this issue, it would be greatly appreciated!

I notice that your uninstall instructions don't tell users to first disable the addon, restart, and then click remove. That is how I workaround this issue myself.

> In #77837#953299, @rlguy wrote: > This issue also affects the FLIP Fluids addon and is a common error when our users try to uninstall our addon. If there is a solution or workaround to to this issue, it would be greatly appreciated! I notice that your [uninstall instructions](https://github.com/rlguy/Blender-FLIP-Fluids/wiki/Addon-Installation-and-Uninstallation#uninstalling-the-addon) don't tell users to first disable the addon, restart, and then click remove. That is how I workaround this issue myself.

In #77837#953659, @EAW wrote:

In #77837#953299, @rlguy wrote:
This issue also affects the FLIP Fluids addon and is a common error when our users try to uninstall our addon. If there is a solution or workaround to to this issue, it would be greatly appreciated!

I notice that your uninstall instructions don't tell users to first disable the addon, restart, and then click remove. That is how I workaround this issue myself.

This is what Animation Nodes tells the user aswell. Not really fond of it though, I wish Blender had a more user friendly and / or unified way of doing it.

> In #77837#953659, @EAW wrote: >> In #77837#953299, @rlguy wrote: >> This issue also affects the FLIP Fluids addon and is a common error when our users try to uninstall our addon. If there is a solution or workaround to to this issue, it would be greatly appreciated! > > I notice that your [uninstall instructions](https://github.com/rlguy/Blender-FLIP-Fluids/wiki/Addon-Installation-and-Uninstallation#uninstalling-the-addon) don't tell users to first disable the addon, restart, and then click remove. That is how I workaround this issue myself. This is what Animation Nodes tells the user aswell. Not really fond of it though, I wish Blender had a more user friendly and / or unified way of doing it.

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

Added subscriber: @mano-wii

Added subscriber: @mano-wii
Ray molenkamp removed their assignment 2020-07-03 22:23:14 +02:00

Added subscriber: @piiichan

Added subscriber: @piiichan

Added subscriber: @BrianSavery

Added subscriber: @BrianSavery

Also adding this being an issue with Radeon ProRender addon (only windows). I've tried forcefully unloading DLL but so far unsuccessfully.

Also adding this being an issue with Radeon ProRender addon (only windows). I've tried forcefully unloading DLL but so far unsuccessfully.

Added subscriber: @timodriaan

Added subscriber: @timodriaan

Added subscriber: @regcs

Added subscriber: @regcs

This comment was removed by @regcs

*This comment was removed by @regcs*

I submitted a patch (D13685), which follows the suggestion by @MACHIN3:

  • move the files that can't be deleted to a new folder /scripts/addons_trash/
  • every time Blender is initialized it deletes the trash folder (if it exists)

NOTE:
Some specific python modules still may need to be unloaded manually in the add-on's unregister() call. I noticed that, e.g., the logging module demands a logging.shutdown().

I submitted a patch ([D13685](https://archive.blender.org/developer/D13685)), which follows the suggestion by @MACHIN3: - move the files that can't be deleted to a new folder `/scripts/addons_trash/` - every time Blender is initialized it deletes the trash folder (if it exists) **NOTE:** Some specific python modules still may need to be unloaded manually in the add-on's `unregister()` call. I noticed that, e.g., the `logging` module demands a `logging.shutdown()`.

Added subscriber: @Zeastin

Added subscriber: @Zeastin

Added subscriber: @ArmoredWolf

Added subscriber: @ArmoredWolf
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:42 +01:00

Adding a ping here as D13685 was archived. @regcs were you able to or anyone else able to get a fix in?

Adding a ping here as [D13685](https://archive.blender.org/developer/D13685) was archived. @regcs were you able to or anyone else able to get a fix in?
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2024-08-05 01:49:41 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
12 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#77837
No description provided.