Fix BotSession reaper Race Condition and Bugs, Improve BotSession exceptions and logging
Description
This MR addresses a few related bugs with the BotSession reaper:
- Race condition around "peeking" for the next expiring session in the OrderedDict, where dictionary was non-empty when we checked but then empty when we tried to pop.
- Bug where
next_expire_time
of NoneType was compared with adatetime
. - Bug where instead of keeping the
newest n
evictions, we kept theoldest n
evictions (only used for logging/troubleshooting purposes) - Bug where every time a botsession expired we correctly closed it, but also accidentally lost track of the most recent (read the one farther in the future to expire) bot session (which meant we never expired that one if the relevant bot disappeared since the expiry of the former botsession)
This MR also improves exceptions and logging around BotSessions.
Edited by Marios Hadjimichael