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

Spanner library should have a way to track leaked sessions

Open vkedia opened this issue 9 years ago • 10 comments

Due to a bug in user code or client library code, it is possible that a session is checked out from the pool but never released. We should make it easier for users to be able to detect such leaks and debug where in there code the leak occurred. One way can be to record the stack trace in a list or a map whenever a session is checked out and remove that trace when the session is released back to the pool. User should be able to look at this list and figure out the location in the code where all the unreleased sessions were checked out at. We can also log these stack traces when the pool is closed.

vkedia avatar May 10 '17 17:05 vkedia

@vkedia Isn't this the same issue as #3390?

dhermes avatar May 10 '17 18:05 dhermes

Not really. That issue is about bounding the session pool so that leaked session do not lead to unbounded number of sessions being created. This issue is for users to be able to debug where they are leaking sessions.

vkedia avatar May 10 '17 18:05 vkedia

Any updates on this?

vkedia avatar May 26 '17 23:05 vkedia

Hi Vikas, this seems more like P1. Can you confirm? Thanks.

danoscarmike avatar Jun 27 '17 19:06 danoscarmike

I marked this as P0 to specify that this is Beta blocking. If you think P1 conveys, please downgrade.

vkedia avatar Jun 27 '17 20:06 vkedia

@vkedia Does this really need to be Beta blocking? It sounds like a nice enhancement, but it's neither surface-breaking nor does its absence interfere with someone using the Python Spanner client library. I think we want to tightly manage the remaining work items before we declare Beta on this library.

bjwatson avatar Aug 07 '17 23:08 bjwatson

Also, can I see example problematic code, as well as what you would expect?

lukesneeringer avatar Aug 08 '17 06:08 lukesneeringer

@bjwatson Its ok for this to be done post beta. @lukesneeringer I was going to point to database.read or database.execute_sql as an example where this could happen if one did not completely consume the iterator returned by them. I thought they would hang on to the session till the stream is closed. But I realized those methods are buggy. I filed an issue for them: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3768 Depending on how that issue is resolved, we can revisit this.

vkedia avatar Aug 08 '17 22:08 vkedia

"Proper" / expected usage won't have leaks, because it involves a context manager, which checks out the session from the pool and returns it whatever the user code does:

with database.snapshot() as snapshot:
    snapshot.execute_sql(...)

with database.batch() as batch():
    batch.insert(...)

or has user code running inside a "managed" transaction, likewise managing the session checkout /return.

def unit_of_work(transaction):
      rows = list(transaction.execute_sql(...))
      transaction.update(...)
      return len(rows)

row_count = database.run_in_transaction(unit_of_work)

So, the remaing issue would be with users who are constructing sessions manually.

tseaver avatar Aug 10 '17 18:08 tseaver

I agree now that we have removed the database.execute_sql method this no longer seems to be an issue. If users are creating sessions manually, then really the onus is on them to properly manage them so I am not that worried about them. I will close this.

vkedia avatar Aug 10 '17 18:08 vkedia