Fixed crash on exit due to race condition of exit code closing db