]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commit - lib/puppet/provider/apt_key/apt_key.rb
Fix apt_key tempfile race condition
authorClayton O'Neill <clayton.oneill@twcable.com>
Wed, 11 Nov 2015 20:11:24 +0000 (20:11 +0000)
committerClayton O'Neill <clayton.oneill@twcable.com>
Thu, 12 Nov 2015 14:09:52 +0000 (14:09 +0000)
commitea6a84f336d5bb7e2ccab04cc6b2a4b2722b503d
tree9bbfb05208d12b92e94c463d171bb3754da493a0
parent99ea865f43fe22c4828dea2457aeda9dbf909e7f
Fix apt_key tempfile race condition

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.
lib/puppet/provider/apt_key/apt_key.rb