Fix deadlock bug when daemonizing