Skip to content

Commit

Permalink
race condition-session already returned to pool
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Nov 6, 2023
1 parent 0e04dde commit ee5b3a3
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions google/cloud/spanner_v1/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,32 +259,32 @@ def _close_long_running_transactions(self, session):
:returns: True if transaction is closed else False.
"""
session_recycled = False
session_trace = self._traces[session._session_id]
if self._database.close_inactive_transactions:
if self.logging_enabled:
# Log a warning for a long-running transaction that has been closed
self._database.logger.warning(
LONG_RUNNING_TRANSACTION_ERR_MSG + session_trace
)

# Set the session as None for associated transaction object
if session._transaction is not None:
session._transaction._session = None

# Increment the count of closed transactions and return the session to the pool
session_recycled = True
self.put(session)
elif self.logging_enabled:
# Log a warning for a potentially leaking long-running transaction.
# Only log the warning if it hasn't been logged already.
if not session.transaction_logged:
self._database.logger.warning(
"Transaction has been running for longer than 60 minutes and might be causing a leak. "
+ "Enable closeInactiveTransactions in Session Pool Options to automatically clean such transactions or use batch or partitioned transactions for long running operations."
+ session_trace
)
session.transaction_logged = True

if session._session_id in self._traces:
session_trace = self._traces[session._session_id]
if self._database.close_inactive_transactions:
if self.logging_enabled:
# Log a warning for a long-running transaction that has been closed
self._database.logger.warning(
LONG_RUNNING_TRANSACTION_ERR_MSG + session_trace
)

# Set the session as None for associated transaction object
if session._transaction is not None:
session._transaction._session = None

# Increment the count of closed transactions and return the session to the pool
session_recycled = True
self.put(session)
elif self.logging_enabled:
# Log a warning for a potentially leaking long-running transaction.
# Only log the warning if it hasn't been logged already.
if not session.transaction_logged:
self._database.logger.warning(
"Transaction has been running for longer than 60 minutes and might be causing a leak. "
+ "Enable closeInactiveTransactions in Session Pool Options to automatically clean such transactions or use batch or partitioned transactions for long running operations."
+ session_trace
)
session.transaction_logged = True
return session_recycled


Expand Down

0 comments on commit ee5b3a3

Please sign in to comment.