sadpandajoe commented on code in PR #45478:
URL: https://github.com/apache/superset/pull/45478#discussion_r4413241524


##########
superset/tasks/manager.py:
##########
@@ -58,6 +58,34 @@ def wrapped() -> T:
     return wrapped
 
 
+def _release_session_after_poll(fn: Callable[[], T]) -> Callable[[], T]:
+    """Wrap ``fn`` so the scoped DB session is discarded after every poll.
+
+    When the listener thread is wrapped in a long-lived context (see
+    ``BACKGROUND_THREAD_CONTEXT_FACTORY``), no per-poll app context is pushed, 
so
+    the usual teardown never runs. Without this, a successful poll would leave 
the
+    listener holding an idle open transaction (and its metastore connection) 
for
+    the task's whole life, and a failed poll would leave the session in a 
failed
+    transaction so every later poll would fail too.
+    """
+
+    def wrapped() -> T:
+        try:
+            return fn()
+        finally:
+            from superset import db
+
+            try:
+                # Not a unit of work: this only discards a read poll's session,
+                # so @transaction (which commits on success) doesn't fit.
+                db.session.rollback()  # pylint: 
disable=consider-using-transaction

Review Comment:
   If `rollback()` itself raises here (the same dropped-connection case this 
wrapper is meant to survive), the `except` skips `db.session.remove()`, so the 
listener thread keeps a session stuck in a failed transaction and every later 
abort poll keeps failing, which is the symptom this change set out to fix. 
Could `remove()` run in its own `finally`, or in a separate `try`, so a 
rollback failure can't skip it? A case where both the poll and `rollback` 
raise, asserting `remove` is still called, would pin it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to