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 #572

Merged

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 claytono force-pushed the master-tempfile-race-condition branch from 95ff7f9 to ea6a84f Compare November 12, 2015 14:10
@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.

@tphoney
Copy link

tphoney commented Dec 11, 2015

closing this as it is a duplicate of #571

@tphoney tphoney closed this Dec 11, 2015
@claytono
Copy link
Author

This isn't a duplicate, it's on a different branch.

@tphoney
Copy link

tphoney commented Dec 11, 2015

both are be merged into master though. or is that incorrect ?

@claytono
Copy link
Author

No, one is against master, the other is against the 1.8.x branch.

@tphoney tphoney reopened this Dec 11, 2015
@tphoney
Copy link

tphoney commented Dec 11, 2015

Generally we would apply into master first, then all the changes would be merged into the release branch, prior to the new release going out.

@daenney
Copy link

daenney commented Dec 12, 2015

The other PR is against 1.8, the old release of APT. It's unlikely we'll ever release that.

@daenney
Copy link

daenney commented Jan 3, 2016

@tphoney Can someone at PL run the integration tests for this and come back to us with the result?

@tphoney
Copy link

tphoney commented Jan 4, 2016

@daenney i will get this into our sprint

@bmjen
Copy link

bmjen commented Jan 6, 2016

@daenney
Copy link

daenney commented Jan 6, 2016

Alright, so everything appears to work. Someone care to merge?

@bmjen
Copy link

bmjen commented Jan 6, 2016

bmjen added a commit that referenced this pull request Jan 6, 2016
…tion

Fix apt_key tempfile race condition
@bmjen bmjen merged commit 7aa9778 into puppetlabs:master Jan 6, 2016
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.

4 participants