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

Fix apt_key tempfile race condition #571

Closed
wants to merge 1 commit into from

Conversation

claytono
Copy link

The Ruby Tempfile class has a finalizer that removes the file when the
GC runs. It's not predictible when the GC will run, so you have to
ensure that the instance of the class stays in scope for as long as you
need it.

Unfortunately the tempfile method is returning just the filename of the
temporary file, which means it goes out of scope when that method
returns. This allows the GC to reap it at any time after return.

In both CI and production environments we've seen this race fail,
causing apt-key add to fail a small (2-3%) amount of the time.

This changes the tempfile and source_to_file methods to return the
underlying Tempfile object, pushing it up into the caller's scope. Both
of the callers immediately use the object to get its filename and then
open the file, eliminating the race.

@claytono
Copy link
Author

This improves the situation, but is still missing something, don't merge.

@claytono
Copy link
Author

Ok, this should be good now. Tested by forcing GC right before running the apt-key command and cannot reproduce now.

@daenney
Copy link

daenney commented Nov 12, 2015

Could you squash this down into one commit please?

The Ruby Tempfile class has a finalizer that removes the file when the
GC runs.  It's not predictible when the GC will run, so you have to
ensure that the instance of the class stays in scope for as long as you
need it.

Unfortunately the tempfile method is returning just the filename of the
temporary file, which means it goes out of scope when that method
returns.  This allows the GC to reap it at any time after return.

In both CI and production environments we've seen this race fail,
causing apt-key add to fail a small (2-3%) amount of the time.

This changes the tempfile and source_to_file methods to return the
underlying Tempfile object, pushing it up into the caller's scope.  Both
of the callers immediately use the object to get its filename and then
open the file, eliminating the race.

Tested this by adding 'GC.start; sleep(1)' immediately before the
command is run, to give the GC plenty of time to remove the tempfile if
it was going to.
@claytono
Copy link
Author

Done

@daenney daenney added the bugfix label Nov 12, 2015
@daenney
Copy link

daenney commented Nov 12, 2015

We have a triage tonight, I'll raise it there. There's a chance we won't actually ever release another 1.8.x version of the module though, we've essentially discontinued that line.

@tphoney
Copy link

tphoney commented Dec 14, 2015

@Dvorak @daenney quite correct. APT is at 2.2.1 on the forge. we will only merge into master.

@tphoney tphoney closed this Dec 14, 2015
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.

3 participants