axeldelafosse/stemgen

[Bug] Windows cleaning step failing

Closed this issue · 5 comments

I always get this error on different windows install:

[Done 6/6]

creating E:\Users\Rouniald\Music\stemgen-master\.\output/Olmatri - Kaspar/Olmatri - Kaspar.stem.m4a was successful!
Done.
Cleaning...
Traceback (most recent call last):
  File "E:\Users\Rouniald\Music\stemgen-master\stemgen.py", line 427, in <module>
    main()
  File "E:\Users\Rouniald\Music\stemgen-master\stemgen.py", line 422, in main
    run()
  File "E:\Users\Rouniald\Music\stemgen-master\stemgen.py", line 302, in run
    clean_dir()
  File "E:\Users\Rouniald\Music\stemgen-master\stemgen.py", line 415, in clean_dir
    shutil.rmtree(os.path.join(OUTPUT_PATH + "/" + FILE_NAME))
  File "C:\Users\rouniald\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 759, in rmtree
    return _rmtree_unsafe(path, onerror)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\rouniald\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 626, in _rmtree_unsafe
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Users\rouniald\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 624, in _rmtree_unsafe
    os.rmdir(path)
PermissionError: [WinError 32] Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus: 'E:\\Users\\Rouniald\\Music\\stemgen-master\\.\\output/Olmatri - Kaspar'

Thank you for the report! It is a known (and annoying) issue.

I'm not familiar enough with Windows to understand and fix this quickly, I need to dig. PRs are welcomed! This is a good first issue, if you ever want to try to fix it :)

The error message: (PermissionError: [WinError 32] The process cannot access the file because it is being used by another process) indicates that some process is still using a file or directory in the folder you're trying to remove with shutil.rmtree().

This is because stemgen is using os.chdir(os.path.join(OUTPUT_PATH, FILE_NAME)) to change the current working directory. If the Python process (or any other running process) is in the directory you're trying to delete, you'll get a permission error. Before removing the directory, you should change back to some other directory.

I made a pull request (#39) with the following changes to the cleanup process:
Bug Fix:

  • Move out of the Target Directory to be cleaned: Before attempting to remove the directory, I changed the current working directory to OUTPUT_PATH to ensure we are not inside the directory we're trying to delete.
    Minor Improvements
  • Path Handling: Improved the way paths are constructed using os.path.join() consistently instead of a mix of string concatenation and joining. This ensures paths are constructed in a platform-independent manner.
  • Error Handling: Added a try-except block around the shutil.rmtree() call. If a PermissionError occurs, a message is printed indicating the directory might still be in use. This provides a clearer error message and ensures the script doesn't crash due to this specific error.
  • Checking for Stem File: Modified the path for checking and renaming the stem file to ensure it's constructed correctly.

I also added the following improvement for windows users with a GPU:

  • Automatic GPU Detection: Script now does a check to see if CUDA is available. If so, it will use the GPU by default (can be overridden by using the -d/--device flag).
    If no CUDA support is detected, the script will use the CPU instead.

FWIW, Demucs without any -d flag defaults to CUDA-if-available-otherwise-CPU.

@awesomer, yes but there isn't a way of doing that without changing the code because "cpu" is set as the default value in argparse. So if I omit the -d flag, stemgen defaults back to -d cpu

Fixed by #39. Closing this!