From bf5a3271560c3acf517e64e9e1ce27a08cb97b85 Mon Sep 17 00:00:00 2001 From: L1nSn0w Date: Wed, 4 Mar 2026 12:13:31 +0800 Subject: [PATCH] fix(api): ensure enterprise workspace join occurs on account registration failure --- api/services/account_service.py | 20 +++++++------------ .../services/test_account_service.py | 2 ++ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/api/services/account_service.py b/api/services/account_service.py index f9393ff1c0..c52a88d5c8 100644 --- a/api/services/account_service.py +++ b/api/services/account_service.py @@ -297,18 +297,15 @@ class AccountService: email=email, name=name, interface_language=interface_language, password=password ) - workspace_creation_error: Exception | None = None try: TenantService.create_owner_tenant_if_not_exist(account=account) - except Exception as e: # noqa: BLE001 - workspace_creation_error = e + except Exception: + # Enterprise-only side-effect should run independently from personal workspace creation. + _try_join_enterprise_default_workspace(str(account.id)) + raise - # Enterprise-only side-effect should run independently from personal workspace creation. _try_join_enterprise_default_workspace(str(account.id)) - if workspace_creation_error is not None: - raise workspace_creation_error - return account @staticmethod @@ -1363,7 +1360,6 @@ class RegisterService: db.session.begin_nested() """Register account""" try: - workspace_creation_error: Exception | None = None account = AccountService.create_account( email=email, name=name, @@ -1387,15 +1383,13 @@ class RegisterService: TenantService.create_tenant_member(tenant, account, role="owner") account.current_tenant = tenant tenant_was_created.send(tenant) - except Exception as e: # noqa: BLE001 - workspace_creation_error = e + except Exception: + _try_join_enterprise_default_workspace(str(account.id)) + raise db.session.commit() _try_join_enterprise_default_workspace(str(account.id)) - - if workspace_creation_error is not None: - raise workspace_creation_error except WorkSpaceNotAllowedCreateError: db.session.rollback() logger.exception("Register failed") diff --git a/api/tests/unit_tests/services/test_account_service.py b/api/tests/unit_tests/services/test_account_service.py index d35f407874..2f73097293 100644 --- a/api/tests/unit_tests/services/test_account_service.py +++ b/api/tests/unit_tests/services/test_account_service.py @@ -1178,6 +1178,7 @@ class TestRegisterService: ) mock_join_default_workspace.assert_called_once_with(str(mock_account.id)) + mock_db_dependencies["db"].session.commit.assert_not_called() def test_register_still_calls_default_workspace_join_when_workspace_limit_exceeded( self, mock_db_dependencies, mock_external_service_dependencies, monkeypatch @@ -1216,6 +1217,7 @@ class TestRegisterService: ) mock_join_default_workspace.assert_called_once_with(str(mock_account.id)) + mock_db_dependencies["db"].session.commit.assert_not_called() def test_register_with_oauth(self, mock_db_dependencies, mock_external_service_dependencies): """Test account registration with OAuth integration."""