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

lib/client.rb - use Tempfile.create instead of Tempfile.new for request bodies #3296

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

MSP-Greg
Copy link
Member

Description

Currently, Tempfile.new is used in Client when a request contains content (a body) that is either very large or indeterminate (chunked). Tempfile.new creates a finalizer that removes the Tempfile, so its removal is dependent on GC.

Recently, Windows head builds started logging the following in CI, a log is here. Note that Ruby head (3.3) has had many changes re GC, and one might call it more agressive...

D:/a/puma/puma/lib/puma/thread_pool.rb:113: warning: Exception in finalizer #<Tempfile::Remover:0x000001939f62dc50 @pid=2472, @path="D:/a/_temp/puma20231222-2472-94nq5x">
D:/ruby-mswin/lib/ruby/3.3.0+0/tempfile.rb:319:in `unlink': Permission denied @ apply2files - D:/a/_temp/puma20231222-2472-94nq5x (Errno::EACCES)

I doubt many people are using Puma on Windows, but Windows CI may show things that indicate issues. On Windows, one cannot delete a file if it is open, unlike '*nix' platforms.

Also, in general, I think we'd prefer that Puma does its own cleanup, and not depend on GC to do so, at least with system resources like IO (files, pipes, sockets, etc).

So, use Tempfile.create and remove the file when Puma is finished with it.

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added (or updated) appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed.
  • If this PR doesn't need tests (docs change), I added [ci skip] to the title of the PR.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly.
  • All new and existing tests passed, including Rubocop.

@MSP-Greg MSP-Greg added the bug label Dec 23, 2023
@dentarg
Copy link
Member

dentarg commented Feb 18, 2024

Not related, just things I've read on the topic of Tempfile that I'll link from here as I may look for them again in the future:

@elliotcm
Copy link

elliotcm commented Apr 23, 2024

Unrelated to the windows issue, this PR should also fix a problem we've been having on linux where the GC isn't aggressive enough, leading to disk filling up with unlinked-but-held-open tempfiles from Puma.

i.e. this but for another 1200 lines:

COMMAND     PID   TID TASKCMD   USER   FD      TYPE             DEVICE SIZE/OFF    NODE NAME
ruby        520                 root   19u      REG              254,0 68878518   21226 /tmp/puma20240421-520-95cx9t (deleted)
ruby        520                 root   20u      REG              254,0 73373916   21251 /tmp/puma20240421-520-200om9 (deleted)
ruby        520                 root   21u      REG              254,0 69127694   21202 /tmp/puma20240421-520-ag0zhf (deleted)
ruby        520                 root   22u      REG              254,0 37046774   21217 /tmp/puma20240421-520-p208tm (deleted)
ruby        520                 root   23u      REG              254,0 75673550   21204 /tmp/puma20240421-520-a8k1n2 (deleted)
ruby        520                 root   24u      REG              254,0 35001808   21216 /tmp/puma20240421-520-ezlf82 (deleted)
ruby        520                 root   25u      REG              254,0 50871668   21220 /tmp/puma20240421-520-pya8yg (deleted)
ruby        520                 root   26u      REG              254,0 67143412   21214 /tmp/puma20240421-520-wxpvvq (deleted)
ruby        520                 root   27u      REG              254,0 86423491   21207 /tmp/puma20240421-520-yo9urp (deleted)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants