Repository: astral-sh/uv
File: crates/uv-windows/src/job.rs (Rust)

Code before:

    /// Returns the raw job handle.
    ///
    /// The returned handle is owned by this `Job` and will be closed when the `Job`
    /// is dropped. The caller must not close or otherwise invalidate this handle.
    #[must_use]
    pub const fn as_raw_handle(&self) -> HANDLE {
        self.handle
    }

    /// Configures the job object limits.
    #[allow(unsafe_code)]
    fn configure_limits(&self) -> Result<(), JobError> {
        let mut info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();
        let info_size = u32::try_from(size_of_val(&info)).expect("job info size fits in u32");
        let mut return_length = 0u32;

        // SAFETY: We pass a valid job handle, the correct information class,
        // a properly sized buffer, and the buffer size.
        unsafe {
            QueryInformationJobObject(
                Some(self.handle),
                JobObjectExtendedLimitInformation,
                (&raw mut info).cast::<c_void>(),
                info_size,
                Some(&raw mut return_length),
            )
        }
        .map_err(|e| JobError::QueryFailed(e.code().0))?;

        // Set the limits we need
        info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
        info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;

        // SAFETY: We pass a valid job handle, the correct information class,
        // a properly initialized info struct, and its size.
        unsafe {
            SetInformationJobObject(
                self.handle,
                JobObjectExtendedLimitInformation,
                (&raw const info).cast::<c_void>(),
                info_size,
            )
        }
        .map_err(|e| JobError::SetFailed(e.code().0))
    }
}

impl Drop for Job {
    #[allow(unsafe_code)]
    fn drop(&mut self) {

Review comment (suggestion):
We can pass `None` here given that this specific query doesn't put anything meaningful here and we're not reading it.

edit: Looks like this is a carry-over from the previous code... Might be a problem for another PR.

Code after:
    /// The returned handle is owned by this `Job` and will be closed when the `Job`
    /// is dropped. The caller must not close or otherwise invalidate this handle.
    #[must_use]
    pub const fn as_raw_handle(&self) -> HANDLE {
        self.handle
    }

    /// Configures the job object limits.
    #[allow(unsafe_code)]
    fn configure_limits(&self) -> Result<(), JobError> {
        let mut info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();
        let info_size = u32::try_from(size_of_val(&info)).expect("job info size fits in u32");

        // SAFETY: We pass a valid job handle, the correct information class,
        // a properly sized buffer, and the buffer size.
        unsafe {
            QueryInformationJobObject(
                Some(self.handle),
                JobObjectExtendedLimitInformation,
                (&raw mut info).cast::<c_void>(),
                info_size,
                None,
            )
        }
        .map_err(|e| JobError::Query(e.code().0))?;

        // Set the limits we need
        info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
        info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;

        // SAFETY: We pass a valid job handle, the correct information class,
        // a properly initialized info struct, and its size.
        unsafe {
            SetInformationJobObject(
                self.handle,
                JobObjectExtendedLimitInformation,
                (&raw const info).cast::<c_void>(),
                info_size,
            )
        }
        .map_err(|e| JobError::Set(e.code().0))
    }
}

impl Drop for Job {
    #[allow(unsafe_code)]
    fn drop(&mut self) {
        // SAFETY: self.handle is valid and we're done using it.
        // Ignoring the result is fine - there's nothing we can do if close fails.
        let _ = unsafe { CloseHandle(self.handle) };
    }