Repository: servo/servo
File: components/script/dom/globalscope.rs (Rust)

Code before:

    pub(crate) fn in_flight_keep_alive_records(&self) -> Vec<InFlightKeepAliveRecord> {
        self.fetch_group
            .borrow()
            .in_flight_keep_alive_records
            .lock()
            .clone()
    }

    /// Part of <https://fetch.spec.whatwg.org/#populate-request-from-client>
    pub(crate) fn request_client(&self) -> RequestClient {
        // Step 1.2.2. If global is a Window object and global’s navigable is not null,
        // then set request’s traversable for user prompts to global’s navigable’s traversable navigable.
        let preloaded_resources = self
            .downcast::<Window>()
            .map(|window: &Window| window.Document().preloaded_resources())
            .unwrap_or_default();
        RequestClient {
            preloaded_resources,
            policy_container: RequestPolicyContainer::PolicyContainer(self.policy_container()),
            origin: RequestOrigin::Origin(self.origin().immutable().clone()),
            in_flight_keep_alive_records: self
                .fetch_group
                .borrow()
                .in_flight_keep_alive_records
                .clone(),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-policy-container>
    pub(crate) fn policy_container(&self) -> PolicyContainer {
        if let Some(window) = self.downcast::<Window>() {
            return window.Document().policy_container().to_owned();
        }
        if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
            return worker.policy_container().to_owned();
        }
        unreachable!();
    }

    /// Get the [base url](https://html.spec.whatwg.org/multipage/#api-base-url)
    /// for this global scope.
    pub(crate) fn api_base_url(&self) -> ServoUrl {
        if let Some(window) = self.downcast::<Window>() {
            // https://html.spec.whatwg.org/multipage/#script-settings-for-browsing-contexts:api-base-url
            return window.Document().base_url();
        }
        if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
            // https://html.spec.whatwg.org/multipage/#script-settings-for-workers:api-base-url
            return worker.get_url().clone();
        }

Review comment (bug):
@gterzian Can you help me out here? I thought that this would allow me to send the `Mutex` to the fetch thread, so that any change made to the vec would be reflected back to the script thread. However, based on my debugging, that's not the case:

```
pid:120547 65536 <-- size of total_bytes as u64 + in_flight_keep_alive_bytes in SendBeacon
 0:03.85 pid:120547 When sending: 0 <-- number of items in vec in SendBeacon
 0:03.85 pid:120547 Adding to records RequestId(1198164b-c21d-4fcb-b9bf-9e988dcf0661)
 0:03.85 pid:120547 Vec size 1 <-- Added it in the loader
 0:03.85 pid:120547 In loader: 65536 <-- confirmed the size in the loader
 0:03.86 pid:120547 0 <-- second request in SendBeacon (which indeed adds 0 bytes)
 0:03.86 pid:120547 When sending: 0 <-- WRONG: It should have seen the "Adding to records" that happened in the fetch thread in the script thread and thus this should have printed "When sending: 1"
 0:03.86 pid:120547 Adding to records RequestId(72eefc2a-4120-4c8e-a8ab-2f7370caf7cf)
 0:03.86 pid:120547 Vec size 1
 0:03.87 pid:120547 1
 0:03.87 pid:120547 When sending: 0
 0:03.87 pid:120547 Adding to records RequestId(9d179d5a-4984-4b33-9f04-80a6344c65cd)
 0:03.87 pid:120547 Vec size 1
 0:03.89 pid:120547 In loader: 0
 0:03.89 pid:120547 In loader: 1
```

I thought that maybe the `RefCell` makes this impossible, but I don't think that's the case since we clone the `Arc<Mutex`.

I am at a loss here and don't know what part is missing here. I have read several guides on `Arc<Mutex` and there the code works (it always does, doesn't it 😂).

Code after:
            _ => with_script_thread(|script_thread| Some(script_thread.schedule_timer(request))),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#nested-browsing-context>
    pub(crate) fn is_nested_browsing_context(&self) -> bool {
        self.downcast::<Window>()
            .is_some_and(|window| !window.is_top_level())
    }

    /// Obtain the size of in flight keep alive records from the resource thread.
    /// If we can't communicate with the thread, we return u64::MAX to ensure
    /// the limit is higher than what is allowed. This ensures that whenever
    /// we want to initiate a keep alive request and the thread doesn't communicate,
    /// we block additional keep alive requests.
    pub(crate) fn total_size_of_in_flight_keep_alive_records(&self) -> u64 {
        let (sender, receiver) = generic_channel::channel().unwrap();
        if self
            .core_resource_thread()
            .send(CoreResourceMsg::TotalSizeOfInFlightKeepAliveRecords(
                self.pipeline_id(),
                sender,
            ))
            .is_err()
        {
            return u64::MAX;
        }
        receiver.recv().unwrap_or(u64::MAX)
    }

    /// Part of <https://fetch.spec.whatwg.org/#populate-request-from-client>
    pub(crate) fn request_client(&self) -> RequestClient {
        // Step 1.2.2. If global is a Window object and global’s navigable is not null,
        // then set request’s traversable for user prompts to global’s navigable’s traversable navigable.
        let window = self.downcast::<Window>();
        let preloaded_resources = window
            .map(|window: &Window| window.Document().preloaded_resources().clone())
            .unwrap_or_default();
        let is_nested_browsing_context = window.is_some_and(|window| !window.is_top_level());
        RequestClient {
            preloaded_resources,
            policy_container: RequestPolicyContainer::PolicyContainer(self.policy_container()),
            origin: RequestOrigin::Origin(self.origin().immutable().clone()),
            is_nested_browsing_context,
            insecure_requests_policy: self.insecure_requests_policy(),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-policy-container>
    pub(crate) fn policy_container(&self) -> PolicyContainer {
        if let Some(window) = self.downcast::<Window>() {