···111111 }
112112 };
113113114114+ // check before trying to acquire a permit so we dont acquire a permit
115115+ // for no reason, the read will be cheap anyhow
114116 if self.in_flight.contains_sync(&did) {
115117 continue;
116118 }
117117- let _ = self.in_flight.insert_sync(did.clone().into_static());
118119119120 let permit = match self.semaphore.clone().try_acquire_owned() {
120121 Ok(p) => p,
121121- Err(_) => {
122122- // remove before breaking so the DID isn't permanently stuck in
123123- // in_flight with no task to clean it up
124124- self.in_flight.remove_sync(&did);
125125- break;
126126- }
122122+ Err(_) => break,
127123 };
124124+125125+ // only mark as in flight if we can acquire a permit
126126+ if self
127127+ .in_flight
128128+ .insert_sync(did.clone().into_static())
129129+ .is_err()
130130+ {
131131+ // a task is already running, weh
132132+ // so we don't need this one anymore...
133133+ break;
134134+ }
128135129136 let guard = InFlightGuard {
130137 did: did.clone().into_static(),