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

Do not error when trying to delete nonexistent temp file #2434

Merged
merged 1 commit into from
Nov 1, 2019
Merged

Do not error when trying to delete nonexistent temp file #2434

merged 1 commit into from
Nov 1, 2019

Conversation

cmorbitzer
Copy link
Contributor

@cmorbitzer cmorbitzer commented Oct 31, 2019

Requirements

Please take note of our contributing guidelines: https://docs.laravel-excel.com/3.1/getting-started/contributing.html
Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.

Mark the following tasks as done:

  • Checked the codebase to ensure that your feature doesn't already exist.
  • Checked the pull requests to ensure that another person hasn't already submitted the feature or fix.
  • Adjusted the Documentation.
  • Added tests to ensure against regression.

Description of the Change

This suppresses the warning No such file or directory when trying to unlink a nonexistent file in LocalTemporaryFile::delete().

Why Should This Be Added?

After migrating our infrastructure to Laravel Vapor, which runs on AWS Lambda, exports are intermittently throwing this exception:

ErrorException unlink(/tmp/storage/laravel-excel-KnoWdWn9XVbOlvBYpIZ928L3CMuOUnrl): No such file or directory 
    /tmp/vendor/maatwebsite/excel/src/Files/LocalTemporaryFile.php:43 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
    [internal] unlink
    /tmp/vendor/maatwebsite/excel/src/Files/LocalTemporaryFile.php:43 Maatwebsite\Excel\Files\LocalTemporaryFile::delete
    /tmp/vendor/maatwebsite/excel/src/Files/RemoteTemporaryFile.php:67 Maatwebsite\Excel\Files\RemoteTemporaryFile::delete
    /tmp/vendor/maatwebsite/excel/src/Jobs/StoreQueuedExport.php:57 Maatwebsite\Excel\Jobs\StoreQueuedExport::handle

It appears that on occasion the Lambda instance is removing the local temp file before StoreQueuedExport finishes. I'm not certain why this is happening; perhaps it has to do with the execution time of the queued job.

We have no reports so far of files being deleted before they are read, so the exports would otherwise finish successfully, except that the Laravel error handler throws an exception when unlink() raises the warning that the file does not exist.

Since the only function of LocalTemporaryFile::delete() is to delete the local temp file, this change suppresses the warning if the file does not exist because then there is no need to delete the file and execution may continue normally.

Benefits

We can reliably export spreadsheets on Laravel Vapor.

Possible Drawbacks

Error suppression is inherently risky. This implementation checks for file existence only after unlink() fails, to prevent a race condition. If the file still exists, the method will call unlink() a second time without suppression so that the error message can bubble up to the handler.

It could also be possible that the file name being passed for deletion is different than the actual or correct file name. This scenario seems unlikely, but probably should be tested against because suppressing this warning will also silence the error in this case.

Verification Process

I first confirmed the issue by duplicating the line $this->localTemporaryFile->delete(); in RemoteTemporaryFile::delete() so that the second call would consistently raise the warning that the file did not exist. Then I made the change, and successfully ran an export.

I prefer automated testing but did not see an obvious place to include one and did not know if this should be unit tested or tested as part of another job. I'm happy to add a test if you give me some direction on where to add it.

Applicable Issues

#1974 #2312

@patrickbrouwers
Copy link
Member

Thanks for this!

@patrickbrouwers patrickbrouwers merged commit 077e845 into SpartnerNL:3.1 Nov 1, 2019
@SchizoDuckie
Copy link

SchizoDuckie commented Dec 2, 2019

Running into this issue as well when queueing a job.
@patrickbrouwers Can you please tag merged as 3.1.18?

Updated composer.json to "maatwebsite/excel": "3.1.*@dev",for now to bypass this.

@georgeboot
Copy link

georgeboot commented Dec 13, 2019

@patrickbrouwers @cmorbitzer I'm now using 3.1 dev on Vapor but are still getting errors:

[2019-12-13 19:04:28] production.ERROR: fopen(/tmp/laravel-excel-jCwBV4ur5tLKjI1bg5eiUh1kZrE5Fxos): 
failed to open stream: No such file or directory {"exception":"[object] (ErrorException(code: 0): 
fopen(/tmp/laravel-excel-jCwBV4ur5tLKjI1bg5eiUh1kZrE5Fxos): failed to open stream: 
No such file or directory at /var/task/vendor/maatwebsite/excel/src/Files/LocalTemporaryFile.php:55) 

It seems that there is another issue here.

Were you able to reliably render excel sheets on Vapor?

@cmorbitzer
Copy link
Contributor Author

@georgeboot We are not experiencing issues right now on 3.1-dev. I will keep an eye out in our bug tracker and report here if we still have errors.

@georgeboot
Copy link

@cmorbitzer I figured it out. I didn't set a custom disk for temp files in the config. With that, it works like a charme 💯

@xlcrr
Copy link

xlcrr commented Mar 23, 2020

We are getting this infinite loop on staging server, using 3.1 when running queue:work

File "/tmp/laravel-excel-459lfzyeQKuX5P7qQJ6F5c0GWhpeuuko" does not exist. {"exception":"[object] (InvalidArgumentException(code: 0): File \"/tmp/laravel-excel-459lfzyeQKuX5P7qQJ6F5c0GWhpeuuko\" does not exist. at /home/forge/test.com/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Shared/File.php:137)
[stacktrace]

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

Successfully merging this pull request may close these issues.

5 participants