Add response_finished support to TempfileReaper by skipkayhil · Pull Request #2363 · rack/rack · GitHub
Skip to content

Add response_finished support to TempfileReaper - #2363

Merged
ioquatix merged 2 commits into
rack:mainfrom
skipkayhil:hm-krqswoqmtrytrnxp
Aug 21, 2025
Merged

ioquatix merged 2 commits into
rack:mainfrom
skipkayhil:hm-krqswoqmtrytrnxp

Conversation

@skipkayhil

@skipkayhil skipkayhil commented Aug 1, 2025 •

Copy link
Copy Markdown
Contributor

When supported by the server, this delays closing any tempfiles until after the server socket has been closed (assuming it will be), and additionally removes an allocation (the BodyProxy).

(hiding whitespace from the diff makes the changes more clear)

@skipkayhil
skipkayhil force-pushed the hm-krqswoqmtrytrnxp branch 2 times, most recently from 88e791f to 13fd68a Compare August 1, 2025 06:58

@ioquatix ioquatix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @jeremyevans can you merge if you also agree.

@jeremyevans jeremyevans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to add support for this. I have some suggestions for minor improvements, but they are optional, no objections to merging as-is.

Comment thread lib/rack/tempfile_reaper.rb Outdated
Comment thread lib/rack/tempfile_reaper.rb

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the constant is private, this is only called when env[RACK_TEMPFILES] is set, so I don't think we need the &. here, unless you are trying to handle a case where the key is deleted by the app or other middleware:

Suggested change
env[RACK_TEMPFILES]&.each(&:close!)
env[RACK_TEMPFILES].each(&:close!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way because there are existing tests for the BodyProxy implementation that TempfileReaper should be resilient to other middleware messing with the array

it 'handle missing rack.tempfiles on normal response' do

Since this implementation is new, I suppose we could deviate, but I don't have a strong opinion either way

Comment on lines 21 to +25
env[RACK_TEMPFILES] ||= []

begin
_, _, body = response = @app.call(env)
rescue Exception
env[RACK_TEMPFILES]&.each(&:close!)
raise
end
if response_finished = env[RACK_RESPONSE_FINISHED]
response_finished << RESPONSE_FINISHED_HANDLER

@ioquatix ioquatix Aug 1, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
env[RACK_TEMPFILES] ||= []
begin
_, _, body = response = @app.call(env)
rescue Exception
env[RACK_TEMPFILES]&.each(&:close!)
raise
end
if response_finished = env[RACK_RESPONSE_FINISHED]
response_finished << RESPONSE_FINISHED_HANDLER
tempfiles = env[RACK_TEMPFILES] ||= []
if response_finished = env[RACK_RESPONSE_FINISHED]
response_finished << proc do
tempfiles.each(&:close!)
end

I'm not in favour of my proposed approach, but I thought I'd mention it. Also the diff is messed up in this comment but it's the idea that I'm trying to share.

@ioquatix

ioquatix commented Aug 1, 2025

Copy link
Copy Markdown
Member

I was also wondering if for whatever reason, we had multiple TempfileReaper middleware how would they interact.

For example would close be better off like this:

while io = tempfiles.pop
  io.close!
end

Otherwise, the array is never cleared. Does it matter?

When supported by the server, this delays closing any tempfiles until
after the server socket has been closed (assuming it will be), and
additionally removes an allocation (the BodyProxy).
@skipkayhil

Copy link
Copy Markdown
Contributor Author

@skipkayhil
skipkayhil force-pushed the hm-krqswoqmtrytrnxp branch from 13fd68a to 01c14b8 Compare August 1, 2025 21:40
@ioquatix
ioquatix merged commit 3a95e2f into rack:main Aug 21, 2025
@skipkayhil
skipkayhil deleted the hm-krqswoqmtrytrnxp branch August 21, 2025 21:53
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.

3 participants