docker/libcontainer — pull request #17 by alexlarsson (2014-06-13 14:01:34+00:00)
Title: Fix invalid fd race

Sometimes I was getting:

2014/06/13 13:47:24 finalize namespace drop bounding set read /proc/1/status: bad file descriptor

This happens when applying the capabilities, and the code that
reads the current caps opens /proc/1/status and then reads some data from it.
But during this it gets a EBADFD error.

The problem is that FinalizeNamespace() closes all FDs before applying
the caps, and if a GC then happens after /proc/1/status is opened but
before reading from the fd, then an old os.File finalizer may close the
already closed-and-reused fd, wreaking havoc.

We fix this by running the GC before closing all the FDs to avoid
the risk of this happening to later os.File users, as well as
moving the fd closing to the after setting the capabilities.

Docker-DCO-1.1-Signed-off-by: Alexander Larsson (github: alexlarsson)