laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.79k stars 296 forks source link

Storage::download throws memory exhausted error when using Swoole #933

Open NiroDeveloper opened 4 months ago

NiroDeveloper commented 4 months ago

Octane Version

2.6.2

Laravel Version

11.16.0

PHP Version

8.3.6

What server type are you using?

Swoole

Server Version

5.1.2

Database Driver & Version

No response

Description

Before i start, this is not a duplicate to https://github.com/laravel/octane/issues/903, this is about Swoole. Also large stream responses are fixed with https://github.com/laravel/octane/issues/636, but not this case.

While using Storage::download or Storage::response, laravel is creating a stream response that uses php fpassthru($stream). Somehow the buffering between fpassthru and ob_start does not work, instead it tries to load the full file into the memory. This leads to out of memory errors while downloading very big files.

If i replace the fpassthru with a fread-while-loop it is working as expected.

I can basically make a pull request, but need help with the solution... Is this more a problem in the laravel framework or PHP itself?

Steps To Reproduce

  1. Write a laravel controller that uses Storage::download() to download file with e.g. 1 GB
  2. Call the controller and see the out of memory error
github-actions[bot] commented 4 months ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

francoism90 commented 3 months ago

I have the same issue, large file downloads aren't always correctly downloaded.

Any workarounds?

Shkeats commented 3 months ago

@francoism90 @NiroDeveloper Are you running octane in HTTP/2 mode? https://github.com/laravel/octane/issues/873 and the PR https://github.com/laravel/octane/pull/875 could be related if so.

francoism90 commented 3 months ago

@Shkeats I'm running Octane on a nginx proxy.