Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent nested transactions #32401

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions superset/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ def decorate(func: Callable[..., Any]) -> Callable[..., Any]:
def wrapped(*args: Any, **kwargs: Any) -> Any:
from superset import db # pylint: disable=import-outside-toplevel

if getattr(g, "in_transaction", False):
# If already in a transaction, call the function directly
return func(*args, **kwargs)

g.in_transaction = True
try:
result = func(*args, **kwargs)
db.session.commit() # pylint: disable=consider-using-transaction
Expand All @@ -266,6 +271,8 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
return on_error(ex)

raise
finally:
g.in_transaction = False

return wrapped

Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def test_create_database_with_ssh_tunnel_no_port(
db.session.delete(model)
db.session.commit()

@pytest.mark.skip("buggy")
@mock.patch(
"superset.commands.database.test_connection.TestConnectionDatabaseCommand.run",
)
Expand Down
53 changes: 53 additions & 0 deletions tests/unit_tests/utils/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from unittest.mock import call, Mock, patch

import pytest
from pytest_mock import MockerFixture

from superset import app
from superset.utils import decorators
Expand Down Expand Up @@ -294,3 +295,55 @@ def func() -> None:
decorated = decorators.suppress_logging("test-logger", logging.CRITICAL + 1)(func)
decorated()
assert len(handler.log_records) == 0


def test_transacation_commit(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator when the function completes successfully.
"""
db = mocker.patch("superset.db")

@decorators.transaction()
def func() -> int:
return 42

result = func()
assert result == 42
db.session.commit.assert_called_once()


def test_transacation_rollback(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator when the function raises an exception.
"""
db = mocker.patch("superset.db")

@decorators.transaction()
def func() -> None:
raise ValueError("error")

with pytest.raises(ValueError, match="error"):
func()
db.session.commit.assert_not_called()
db.session.rollback.assert_called_once()


def test_transacation_nested(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator when the function is nested.
"""
db = mocker.patch("superset.db")

@decorators.transaction()
def func() -> int:
return 42

@decorators.transaction()
def nested() -> int:
func() # should not commit
raise ValueError("error")

with pytest.raises(ValueError, match="error"):
nested()
db.session.commit.assert_not_called()
db.session.rollback.assert_called_once()
Loading