Repository: tokio-rs/tokio
File: tokio/src/runtime/time/mod.rs (Rust)

Code before:
            // This happens only on the highest level, when an entry is inserted
            // more than MAX_DURATION into the future. When this happens, we wrap
            // around, and process some entries a multiple of MAX_DURATION before
            // they actually need to be dropped down a level. We then reinsert them
            // back into the same position; we must make sure we don't then process
            // those entries again or we'll end up in an infinite loop.
            let unguarded_list = lock.take_entries(&expiration);
            // It is critical for `GuardedLinkedList` safety that the guard node is
            // pinned in memory and is not dropped until the guarded list is dropped.
            let guard = TimerShared::new(id);
            pin!(guard);
            let guard_ptr = NonNull::from(guard.as_ref().get_ref());
            // GuardedLinkedList ensures that the concurrent drop of Entry in this slot is safe.
            let mut guarded_list = unguarded_list.into_guarded(guard_ptr);

            while let Some(entry) = guarded_list.pop_back() {
                let entry = unsafe { entry.as_ref().handle() };
                let deadline = expiration.deadline;
                // Try to expire the entry; this is cheap (doesn't synchronize) if
                // the timer is not expired, and updates cached_when.
                match unsafe { entry.mark_firing(deadline) } {
                    Ok(()) => {
                        // Entry was expired.
                        // SAFETY: We hold the driver lock, and just removed the entry from any linked lists.
                        if let Some(waker) = unsafe { entry.fire(Ok(())) } {
                            waker_list.push(waker);

                            if !waker_list.can_push() {
                                // Wake a batch of wakers. To avoid deadlock,
                                // we must do this with the lock temporarily dropped.
                                drop(lock);
                                waker_list.wake_all();

                                lock = self.inner.lock_sharded_wheel(id);
                            }
                        }
                    }
                    Err(state) if state == STATE_DEREGISTERED => {}
                    Err(state) => {
                        // Safety: This Entry has not expired.
                        unsafe { lock.reinsert_entry(entry, deadline, state) };
                    }
                }
            }
            lock.occupied_bit_maintain(&expiration);
        }

        let next_wake_up = lock.poll_at();
        drop(lock);

        waker_list.wake_all();

Review comment (suggestion):
I'm worried about panics here. What happens to entries still in the guarded list?

I think some other examples of this wrap the guarded list in something that removes all entries from the list without waking their wakers.

Code after:
            let guard_handle = guard.as_ref().get_ref().handle();

            // * This list will be still guarded by the lock of the Wheel with the specefied id.
            //   `EntryWaitersList` wrapper makes sure we hold the lock to modify it.
            // * This wrapper will empty the list on drop. It is critical for safety
            //   that we will not leave any list entry with a pointer to the local
            //   guard node after this function returns / panics.
            // Safety: The `TimerShared` inside this `TimerHandle` is pinned in the memory.
            let mut list = unsafe { lock.get_waiters_list(&expiration, guard_handle, id, self) };

            while let Some(entry) = list.pop_back_locked(&mut lock) {
                let deadline = expiration.deadline;
                // Try to expire the entry; this is cheap (doesn't synchronize) if
                // the timer is not expired, and updates cached_when.
                match unsafe { entry.mark_firing(deadline) } {
                    Ok(()) => {
                        // Entry was expired.
                        // SAFETY: We hold the driver lock, and just removed the entry from any linked lists.
                        if let Some(waker) = unsafe { entry.fire(Ok(())) } {
                            waker_list.push(waker);

                            if !waker_list.can_push() {
                                // Wake a batch of wakers. To avoid deadlock,
                                // we must do this with the lock temporarily dropped.
                                drop(lock);
                                waker_list.wake_all();

                                lock = self.inner.lock_sharded_wheel(id);
                            }
                        }
                    }
                    Err(state) if state == STATE_DEREGISTERED => {}
                    Err(state) => {
                        // Safety: This Entry has not expired.
                        unsafe { lock.reinsert_entry(entry, deadline, state) };
                    }
                }
            }
            lock.occupied_bit_maintain(&expiration);
        }

        let next_wake_up = lock.poll_at();
        drop(lock);

        waker_list.wake_all();
        next_wake_up
    }

    /// Removes a registered timer from the driver.
    ///
    /// The timer will be moved to the cancelled state. Wakers will _not_ be