Add a new state of `terminated` to fix the race condition of `errorEmitted`; Add more comments.