Add @kwmsmith's fix for atexit cleanup errors.

As discussed.