fohrloop / dash-uploader

The alternative upload component for python Dash applications.
MIT License
144 stars 30 forks source link

Fix random "PermissionError: [WinError 32] The process cannot access the file because it is being used by another process" #71

Closed fohrloop closed 2 years ago

fohrloop commented 2 years ago

Sometimes when uploading files, the upload process is intermitted. Amongst the python logs, there is text

Traceback (most recent call last):
  File "C:\repos\dash-uploader\dash_uploader\httprequesthandler.py", line 42, in post
    return self._post()
  File "C:\repos\dash-uploader\dash_uploader\httprequesthandler.py", line 114, in _post
    os.unlink(target_file_name)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\tmp\\Uploads\\dd620eac-934d-11ec-9bf6-2016b9d15494\\somefile.csv'

This will cause the upload of the file to fail.

dash-uploader version: 944204a (and most probably all versions before that, and <= 0.6.0)

fohrloop commented 2 years ago

Found out a way to reproduce the error. The lines around C:\repos\dash-uploader\dash_uploader\httprequesthandler.py, line 114 are:

            # Make sure some other chunk didn't trigger file reconstruction
            target_file_name = os.path.join(temp_root, flowFilename)
            if os.path.exists(target_file_name):
                logger.info("File %s exists already. Overwriting..", target_file_name)
                os.unlink(target_file_name)

Therefore, to reproduce the error, one can

  1. Upload file somefile.csv
  2. Open the file somefile.csv in another process
  3. Reupload file somefile.csv (same upload_id)

I will write tests for this and create a fix.

fohrloop commented 2 years ago

Closed via https://github.com/np-8/dash-uploader/commit/f033683771e5c807915257250a7344e00d91b42c