Fix potential race condition when cleaning up abandoned nodes.