Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NullPointer in HTTPClient.addBackTCPClient
A race condition could result in the ArrayDeque being removed right after addBackTCPClient added it. Resulting in a NPE when we try to synchronize on it after doing the .get(). The fix is to change how we add back. We already had good locks in place when removing (though checkIdleSockets forgot to check when all sockets were closed). When adding we may loop mutliple times if the state changes from what we expect, because it can be hard to have the lock before we need to add it. If the ArrayDeque already exists, the solution is easy, we just need to be sure it still exists once we actually are able to synchronize (otherwise it may have been removed while waiting to synchronize on it) If it does not exist we will optimistically construct the ArrayDeque, and add the element to it (so that if another thread sees the new queue, it wont be removed due to being empty). Then finally try to do an atomic `putIfAbsent`, if this fails, we will loop and retry.
- Loading branch information