Repository: servo/servo
File: components/net/image_cache.rs (Rust)

Code before:
        origin: &ImmutableOrigin,
        cors_setting: &Option<CorsSettings>,
    ) {
        let mut store = self.store.lock();
        store.remove_loaded_image(url, origin, cors_setting);
    }

    fn evict_rasterized_image(&self, svg_id: &str) {
        let mut store = self.store.lock();
        if let Some(mapped_image_id) = self.svg_id_image_id_map.lock().remove(svg_id) {
            store.pending_loads.remove(&mapped_image_id);
            store.vector_images.remove(&mapped_image_id);
            if let Some(requested_sizes) = self.image_id_size_map.lock().remove(&mapped_image_id) {
                for requested_size in requested_sizes.iter() {
                    store.remove_rasterized_vector_image(&mapped_image_id, &requested_size);
                }
            }
        }
    }

    /// Inform the image cache about a response for a pending request.
    fn notify_pending_response(&self, id: PendingImageId, action: FetchResponseMsg) {
        match (action, id) {
            (FetchResponseMsg::ProcessRequestBody(..), _) |
            (FetchResponseMsg::ProcessRequestEOF(..), _) |
            (FetchResponseMsg::ProcessCspViolations(..), _) => (),
            (FetchResponseMsg::ProcessResponse(_, response), _) => {
                debug!("Received {:?} for {:?}", response.as_ref().map(|_| ()), id);
                let mut store = self.store.lock();
                if let Some(pending_load) = store.pending_loads.get_by_key_mut(&id) {
                    let (cors_status, metadata) = match response {
                        Ok(meta) => match meta {
                            FetchMetadata::Unfiltered(m) => (CorsStatus::Safe, Some(m)),
                            FetchMetadata::Filtered { unsafe_, filtered } => (
                                match filtered {
                                    FilteredMetadata::Basic(_) | FilteredMetadata::Cors(_) => {
                                        CorsStatus::Safe
                                    },
                                    FilteredMetadata::Opaque |
                                    FilteredMetadata::OpaqueRedirect(_) => CorsStatus::Unsafe,
                                },
                                Some(unsafe_),
                            ),
                        },
                        Err(_) => (CorsStatus::Unsafe, None),
                    };
                    let final_url = metadata.as_ref().map(|m| m.final_url.clone());
                    pending_load.final_url = final_url;
                    pending_load.cors_status = cors_status;
                    pending_load.content_type = metadata
                        .as_ref()

Review comment (bug):
Let's add a debug here and below in the none case that the pending load has already been evicted from the cache.

Code after:
        store.remove_loaded_image(url, origin, cors_setting);
    }

    fn evict_rasterized_image(&self, svg_id: &str) {
        let mut store = self.store.lock();
        if let Some(mapped_image_id) = self.svg_id_image_id_map.lock().remove(svg_id) {
            store.pending_loads.remove(&mapped_image_id);
            store.vector_images.remove(&mapped_image_id);
            if let Some(requested_sizes) = self.image_id_size_map.lock().remove(&mapped_image_id) {
                for requested_size in requested_sizes.iter() {
                    store.remove_rasterized_vector_image(&mapped_image_id, requested_size);
                }
            }
        }
    }

    /// Inform the image cache about a response for a pending request.
    fn notify_pending_response(&self, id: PendingImageId, action: FetchResponseMsg) {
        match (action, id) {
            (FetchResponseMsg::ProcessRequestBody(..), _) |
            (FetchResponseMsg::ProcessRequestEOF(..), _) |
            (FetchResponseMsg::ProcessCspViolations(..), _) => (),
            (FetchResponseMsg::ProcessResponse(_, response), _) => {
                debug!("Received {:?} for {:?}", response.as_ref().map(|_| ()), id);
                let mut store = self.store.lock();
                if let Some(pending_load) = store.pending_loads.get_by_key_mut(&id) {
                    let (cors_status, metadata) = match response {
                        Ok(meta) => match meta {
                            FetchMetadata::Unfiltered(m) => (CorsStatus::Safe, Some(m)),
                            FetchMetadata::Filtered { unsafe_, filtered } => (
                                match filtered {
                                    FilteredMetadata::Basic(_) | FilteredMetadata::Cors(_) => {
                                        CorsStatus::Safe
                                    },
                                    FilteredMetadata::Opaque |
                                    FilteredMetadata::OpaqueRedirect(_) => CorsStatus::Unsafe,
                                },
                                Some(unsafe_),
                            ),
                        },
                        Err(_) => (CorsStatus::Unsafe, None),
                    };
                    let final_url = metadata.as_ref().map(|m| m.final_url.clone());
                    pending_load.final_url = final_url;
                    pending_load.cors_status = cors_status;
                    pending_load.content_type = metadata
                        .as_ref()
                        .and_then(|metadata| metadata.content_type.clone())
                        .map(|content_type| content_type.into_inner().into());
                } else {
                    debug!("Pending load for id {:?} already evicted from cache", id);