Fix a reference counting race condition affecting `PyObject`s cloned in `allow_threads` blocks.