bito-code-review[bot] commented on code in PR #44653:
URL: https://github.com/apache/superset/pull/44653#discussion_r4123290134


##########
tests/unit_tests/extensions/metastore_cache_test.py:
##########
@@ -0,0 +1,87 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Session hygiene for ``SupersetMetastoreCache``.
+
+Cache reads and writes are best-effort: every caller swallows the failure and
+carries on, treating it as a miss. When the backend is the metadata database
+that leaves ``db.session`` in "pending rollback" state, so the work the caller
+carries on to do fails on this cache call rather than on its own merits. These
+methods therefore repair the session before letting the error propagate.
+"""
+
+from uuid import UUID
+
+import pytest
+from pytest_mock import MockerFixture
+from sqlalchemy.exc import SQLAlchemyError
+
+from superset.extensions.metastore_cache import SupersetMetastoreCache
+from superset.key_value.types import JsonKeyValueCodec
+
+NAMESPACE = UUID("ee173d1b-ccf3-40aa-a3b1-e2e73e56d3c1")
+
+
[email protected]
+def cache() -> SupersetMetastoreCache:
+    return SupersetMetastoreCache(namespace=NAMESPACE, 
codec=JsonKeyValueCodec())
+
+
+def test_get_rolls_back_and_reraises_on_db_error(
+    cache: SupersetMetastoreCache,
+    mocker: MockerFixture,
+) -> None:
+    mocker.patch(
+        "superset.daos.key_value.KeyValueDAO.get_value",
+        side_effect=SQLAlchemyError("connection lost"),
+    )
+    session = mocker.patch("superset.extensions.metastore_cache.db").session
+
+    with pytest.raises(SQLAlchemyError):
+        cache.get("some-key")
+
+    session.rollback.assert_called_once()

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing reraise assertion</b></div>
   <div id="fix">
   
   The test name promises "rolls back and reraises", but only the rollback half 
is asserted: `session.rollback.assert_called_once()` passes even if 
`SupersetMetastoreCache.get` swallowed the `SQLAlchemyError` after rolling back 
(the `pytest.raises` block would then fail, but a swallowed-error regression in 
a future refactor that also drops `pytest.raises` would go unnoticed). Capture 
`exc_info` and assert the raised exception is the mocked `get_value` error, per 
the repo rule on verifying state changes rather than mock return values.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #7d49e8</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
tests/unit_tests/extensions/metastore_cache_test.py:
##########
@@ -0,0 +1,87 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Session hygiene for ``SupersetMetastoreCache``.
+
+Cache reads and writes are best-effort: every caller swallows the failure and
+carries on, treating it as a miss. When the backend is the metadata database
+that leaves ``db.session`` in "pending rollback" state, so the work the caller
+carries on to do fails on this cache call rather than on its own merits. These
+methods therefore repair the session before letting the error propagate.
+"""
+
+from uuid import UUID
+
+import pytest
+from pytest_mock import MockerFixture
+from sqlalchemy.exc import SQLAlchemyError
+
+from superset.extensions.metastore_cache import SupersetMetastoreCache
+from superset.key_value.types import JsonKeyValueCodec
+
+NAMESPACE = UUID("ee173d1b-ccf3-40aa-a3b1-e2e73e56d3c1")
+
+
[email protected]
+def cache() -> SupersetMetastoreCache:
+    return SupersetMetastoreCache(namespace=NAMESPACE, 
codec=JsonKeyValueCodec())
+
+
+def test_get_rolls_back_and_reraises_on_db_error(
+    cache: SupersetMetastoreCache,
+    mocker: MockerFixture,
+) -> None:
+    mocker.patch(
+        "superset.daos.key_value.KeyValueDAO.get_value",
+        side_effect=SQLAlchemyError("connection lost"),
+    )
+    session = mocker.patch("superset.extensions.metastore_cache.db").session
+
+    with pytest.raises(SQLAlchemyError):
+        cache.get("some-key")
+
+    session.rollback.assert_called_once()
+
+
+def test_set_rolls_back_and_reraises_on_db_error(
+    cache: SupersetMetastoreCache,
+    mocker: MockerFixture,
+) -> None:
+    mocker.patch(
+        "superset.daos.key_value.KeyValueDAO.upsert_entry",
+        side_effect=SQLAlchemyError("connection lost"),
+    )
+    session = mocker.patch("superset.extensions.metastore_cache.db").session
+
+    with pytest.raises(SQLAlchemyError):
+        cache.set("some-key", {"a": 1})
+
+    session.rollback.assert_called_once()

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing reraise assertion</b></div>
   <div id="fix">
   
   Same gap as the `get` test: `test_set_rolls_back_and_reraises_on_db_error` 
asserts only `session.rollback.assert_called_once()` and never checks that the 
`SQLAlchemyError` raised by the mocked `KeyValueDAO.upsert_entry` propagates 
unchanged out of `SupersetMetastoreCache.set`. A regression that swallows or 
wraps the error after rollback would still satisfy 
`pytest.raises(SQLAlchemyError)`. Capture `exc_info` and assert exception 
identity, matching the repo rule on verifying state changes.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #7d49e8</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset/models/helpers.py:
##########
@@ -3810,7 +3810,15 @@ def get_from_clause(
                         for statement in parsed_script.statements
                         for table in statement.tables
                     )
-                except Exception:  # pylint: disable=broad-except
+                except Exception as retry_ex:  # pylint: disable=broad-except
+                    # This retry queries db.session again and rebuilds an
+                    # engine, so it can re-poison the session the outer handler
+                    # just rolled back. Roll back again: failing closed below
+                    # raises QueryObjectValidationError, which callers catch
+                    # and carry on from, and the continue-path keeps running
+                    # this query outright.
+                    if isinstance(retry_ex, SQLAlchemyError):
+                        db.session.rollback()  # pylint: 
disable=consider-using-transaction  # noqa: E501

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Conditional rollback poisons session</b></div>
   <div id="fix">
   
   The outer handler (line 3788) rolls back for any exception because a caught 
DB error leaves `db.session` in pending-rollback state. The retry handler rolls 
back only for `SQLAlchemyError`, but `get_predicates_for_table` -> 
`_find_datasets`/`get_sqla_row_level_filters` (superset/utils/rls.py:211, 281) 
can raise arbitrary exceptions mid-query, leaving the session poisoned on both 
the fail-closed and continue paths. Roll back unconditionally.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #7d49e8</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



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