Fix race condition in signal handling.