sagebind / isahc

The practical HTTP client that is fun to use.
https://docs.rs/isahc
MIT License
711 stars 62 forks source link

Race condition possibly manifesting as missing request #431

Open earce opened 1 year ago

earce commented 1 year ago

Encountered the error when running some code with busy cpu usage:

2023-09-21T21:53:32.563521901Z [WARN] (isahc-agent-0): received unpause request for unknown request token: 0

which implies that either

  1. the request was removed from the map before it should've
  2. the request received a UnpauseRead after it should've
  3. there is a race condition where the lookup occurs before it is available in the map

Looking more closely at this function https://github.com/sagebind/isahc/blob/master/src/agent/mod.rs#L332

it appears like UnpauseRead gets written to a concurrent queue followed by the inner waker having wake_by_ref called on it. It seems like while we are only running a single agent thread which handles both the reading off this queue and writing (barring the outside threads which can write to this queue via send_message).

Reading the wake_by_ref doc it seems like it can cause the current thread to yield and enter the poll cycle? is it possible that this logic gets run again but since requests.insert doesn't happen until like 366 there is a race where that request isn't in there yet?

Should this insertion happen at the top of this function?