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

[HttpFoundation] Fix deleteFileAfterSend on client abortion #42033

Closed

Conversation

nerg4l
Copy link
Contributor

@nerg4l nerg4l commented Jul 8, 2021

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #27538
License MIT
Doc PR

Description

If someone sets deleteFileAfterSend to true for a BinaryFileResponse instance they expect the file to be deleted when the response finished. This is not the case when the response is cancelled because PHP won't continue running the code. ignore_user_abort can be used to allow it to finish but this means stream_copy_to_stream will also continue to run regardless of the abort. To fix this later problem it can be a solution to run the copy in a loop and detect the connect abort manually with connection_aborted.

Tests

I don't know how can I simulate connection abort in PHPUnit. I would appreciate if I could get some guidance if it is possible.

Todo

  • add tests if connection abort can be simulated
  • gather feedback for my changes

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title [WIP] [HttpFoundation] Fix deleteFileAfterSend on client abortion [HttpFoundation] [WIP] Fix deleteFileAfterSend on client abortion Jul 8, 2021
@nerg4l nerg4l force-pushed the delete-file-after-send-on-client-abort branch from 4a4c935 to a579bbe Compare July 8, 2021 12:19
@nerg4l nerg4l changed the title [HttpFoundation] [WIP] Fix deleteFileAfterSend on client abortion [HttpFoundation] Fix deleteFileAfterSend on client abortion Aug 16, 2021
@fabpot
Copy link
Member

fabpot commented Jul 15, 2022

Thank you @nerg4l.

fabpot added a commit that referenced this pull request Jul 15, 2022
…n (nerg4l)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpFoundation] Fix deleteFileAfterSend on client abortion

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #27538 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch 5.x.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
-->

**Description**

If someone sets `deleteFileAfterSend` to `true` for a `BinaryFileResponse` instance they expect the file to be deleted when the response finished. This is not the case when the response is cancelled because PHP won't continue running the code. `ignore_user_abort` can be used to allow it to finish but this means `stream_copy_to_stream` will also continue to run regardless of the abort. To fix this later problem it can be a solution to run the copy in a loop and detect the connect abort manually with `connection_aborted`.

**Tests**

I don't know how can I simulate connection abort in PHPUnit. I would appreciate if I could get some guidance if it is possible.

**Todo**

- [ ] add tests if connection abort can be simulated
- [ ] gather feedback for my changes

Commits
-------

f097bef [HttpFoundation] Fix deleteFileAfterSend on client abortion
@fabpot fabpot closed this Jul 15, 2022
@fabpot fabpot force-pushed the delete-file-after-send-on-client-abort branch from deaff2c to f097bef Compare July 15, 2022 08:59
@nerg4l
Copy link
Contributor Author

nerg4l commented Jul 15, 2022

@fabpot I'm glad I was able help.

Additional info: The chunk size in this PR is 8 KiB (8 * 1024 byte) at the moment which is the same as the default stream chunk size in PHP. As far as I know, the only way to read the default chunk size is to call stream_set_chunk_size and see the previous value of it. However, a year ago when I opened the PR I set it to 1 MiB (1024 * 1024 byte) and I don't remember why I did that.

@nerg4l nerg4l deleted the delete-file-after-send-on-client-abort branch August 21, 2022 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants