Fix deadlock while deallocating python objects