vipyrsec/dragonfly-client-rs

Reauthentication Race Condition

Closed this issue · 1 comments

The Issue

Consider: there are two threads of execution, ready to send a request that requires authentication. The access token is expired. Both threads send the request simultaneously and both threads receive a 401. Both threads will then attempt to reauthenticate. This results in the client attempting to login more times than necessary.

Fix Attempts

  • Use RwLock::try_write instead of write in reauthenticate in order to return early if the lock is held already. This does not work because the lock can't tell the difference between locking for updating the rules and updating the access token

Potential Fix

Separate the (hash, rules) pair and access token with 2 RwLocks. Add a CondVar to make sure only one thread is reauthenticateing at once. Use a helper function to access the access token.

Blockers

Blocked by #23

#23 merged, going to work on this now