When a file is uploaded, I receive a ActionDispatch::Http::UploadedFile, which contains a tempfile, that will get cleaned up when the gc cleans it up.
Now I would like to create a job that runs in the background and processes the uploaded file, so that the request can finish quickly. If I kept a reference to the tempfile for the background job, it wouldn't get deleted, but only as long as the Ruby process does not restart.
I found that I can do ObjectSpace.undefine_finalizer(tempfile), and do the deletion myself, at a time of my choosing. Great! I just wonder if there are any risks to undefining the finalizer of a Tempfile?
N.B. An alternative would be to "just" copy the tempfile and work on the copy. But that's wasteful. In my case we are talking up to a few GBs in size total for one upload session.