BurstyPool in spanner can lead to unbounded number of sessions to be created
BurstyPool takes a parameter which is supposed to bound the maximum number of sessions that will be maintained in the pool. But this invariant if maintained when the session is put back in the pool. In case there is a bug (either in the client or in the user code) which leads to session leaking and never being released back into the pool, this can cause an unbounded number of sessions to be created. That can be dangerous since sessions expire only after an hour on the backend so this can cause a user to quickly hit the session limit on the backend and then in effect be locked out of the database for upto an hour.
Instead the behavior should be changed to make the user wait in get if the existing number of sessions that have been created is equal to the max size.
As per discussion with @bjwatson making this P0.
Any updates on this?
@tseaver Can you work on this next? @vkedia wants this to be done before we declare Beta.
All the pools are designed to be used via a Python context manager, which prevents any possibility of leaked sessions (that is the point of the context manager). E.g.:
with pool.session() as session:
session.read(...)
session.run_in_transaction(func)
No matter what, the session gets returned to the pool at the end of the with block. In the case of BurstyPool, if the pool is over its target size at that point, the session is just discarded (after a call to its delete API).
If we make BurstyPool have a hard cap on its size, then we might as well just delete it: the FixedSizePool fills that niche already: when empty, its get() blocks for a configurable timeout, and raises an exception if no session can be acquired.
Thanks for the clarification @tseaver.
@vkedia Given this explanation, are you okay with the the behavior of BurstyPool? It sounds like there should be no leaks, but it can temporarily exceed the configured session pool size in the interest of not blocking on an empty pool.
Having not heard anything on this, and also having looked at the code, I am going to declare this working and close the issue. (@tseaver is correct about how context managers work; solving this kind of problem is exactly why they exist.)
I think this is fine.