google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

BurstyPool in spanner can lead to unbounded number of sessions to be created

Open vkedia opened this issue 9 years ago • 7 comments

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.

vkedia avatar May 09 '17 19:05 vkedia

As per discussion with @bjwatson making this P0.

vkedia avatar May 09 '17 22:05 vkedia

Any updates on this?

vkedia avatar May 26 '17 23:05 vkedia

@tseaver Can you work on this next? @vkedia wants this to be done before we declare Beta.

bjwatson avatar Jul 25 '17 22:07 bjwatson

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.

tseaver avatar Jul 25 '17 23:07 tseaver

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.

bjwatson avatar Jul 25 '17 23:07 bjwatson

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.)

lukesneeringer avatar Aug 07 '17 18:08 lukesneeringer

I think this is fine.

vkedia avatar Aug 07 '17 18:08 vkedia