Add `pending_events` deadlock detection in `handle_error`