Skip to content

Conversation

@dakkar
Copy link

@dakkar dakkar commented Jan 6, 2026

Summary

Ensure that temporary files created by Mojo::UserAgent are cleaned up as soon as the transaction is destroyed.

Motivation

the $cb that sub start passes to sub _start is:

sub { shift->ioloop->stop; $tx = shift }

which closes over $tx, which owns the response, its content, its asset, and its tempfile (when the asset is a Mojo::Asset::File)

if we leave the callback inside the {connections} hashref, the tempfile will not be cleaned up until the connection is next re-used, which may not happen any time soon, especially for long-lived applications that download data from many sources

deleting the callback just before calling it, ensures a timely cleanup

I've also added a test to prove the problem and the fix. All tests pass.

the `$cb` that `sub start` passes to `sub _start` is:

    sub { shift->ioloop->stop; $tx = shift }

which closes over `$tx`, which owns the response, its content, its
asset, and its tempfile (when the asset is a `Mojo::Asset::File`)

if we leave the callback inside the `{connections}` hashref, the
tempfile will not be cleaned up until the connection is next re-used,
which may not happen any time soon, especially for long-lived
applications that download data from many sources

deleting the callback just before calling it, ensures a timely cleanup

I've also added a test to prove the problem and the fix. All tests
pass.
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.

2 participants