Backport fix for potential bad_weak_ptr exception from 2.0.