Fix race at exit.

This fixes the issue #2.