'Actually handle pending updates.'

@measure_func('_incoming_device_list_update')
@defer.inlineCallbacks
def _handle_device_updates(self, user_id):

    with (yield self._remote_edu_linearizer.queue(user_id)):
        pending_updates = self._pending_updates.pop(user_id, [])
        if (not pending_updates):
            return
        resync = (yield self._need_to_do_resync(user_id, pending_updates))
        if resync:
            origin = get_domain_from_id(user_id)
            try:
                result = (yield self.federation.query_user_devices(origin, user_id))
            except NotRetryingDestination:
                logger.warn("Failed    to    handle    device    list    update    for    %s,    we're    not    retrying    the    remote", user_id)
                return
            except Exception:
                logger.exception('Failed    to    handle    device    list    update    for    %s', user_id)
                return
            stream_id = result['stream_id']
            devices = result['devices']
            (yield self.store.update_remote_device_list_cache(user_id, devices, stream_id))
            device_ids = [device['device_id'] for device in devices]
            (yield self.device_handler.notify_device_update(user_id, device_ids))
        else:
            for (device_id, stream_id, prev_ids, content) in pending_updates:
                (yield self.store.update_remote_device_list_cache_entry(user_id, device_id, content, stream_id))
            (yield self.device_handler.notify_device_update(user_id, [device_id for (device_id, _, _, _) in pending_updates]))
        self._seen_updates.setdefault(user_id, set()).update((stream_id for (_, stream_id, _, _) in pending_updates))
