Fix forking bug.

The old code caused a race condition in contexts, such as
temporary_dir() etc.