02 nov 2025//tech//1 min read

rust threadpool shutdown safety

when implementing the drop trait for a threadpool, iterating with `vec::drain(..)` is tempting but dangerous. it destructively empties the vector immediately. if a thread panics mid-loop, you lose track of the remaining workers in the vector, making a graceful shutdown impossible. the safer approach is iterating mutably and using `option::take()`. this allows you to extract the thread handle while leaving `none` in its place, keeping the vector structure intact. this prevents double joins and ensures that even if one thread panics, the others remain accessible for cleanup.