Catching detached session in the wrapper. And application version download failures

This commit is contained in:
SimplifiedPrivacy 2026-11-16 19:32:07 -05:00
parent 9fdbef91ad
commit 53dc71bf01
7 changed files with 93 additions and 47 deletions

View file

@ -19,6 +19,7 @@ from core.models.orm_calls.application_version_calls import get_application_vers
from core.observers.ApplicationVersionObserver import ApplicationVersionObserver from core.observers.ApplicationVersionObserver import ApplicationVersionObserver
from core.observers.ConnectionObserver import ConnectionObserver from core.observers.ConnectionObserver import ConnectionObserver
from core.services.WebServiceApiService import WebServiceApiService from core.services.WebServiceApiService import WebServiceApiService
from core.models.Result import Result, ResultError
from core.errors.logger import logger from core.errors.logger import logger
# import httpx # import httpx
@ -48,7 +49,12 @@ class ApplicationVersionController:
@staticmethod @staticmethod
def install(application_version: ApplicationVersion, reinstall: bool = False, application_version_observer: Optional[ApplicationVersionObserver] = None, connection_observer: Optional[ConnectionObserver] = None): def install(
application_version: ApplicationVersion,
reinstall: bool = False,
application_version_observer: Optional[ApplicationVersionObserver] = None,
connection_observer: Optional[ConnectionObserver] = None
) -> Result:
if not application_version.is_supported: if not application_version.is_supported:
raise UnsupportedApplicationVersionError('The application version in question is not supported.') raise UnsupportedApplicationVersionError('The application version in question is not supported.')
@ -57,10 +63,12 @@ class ApplicationVersionController:
ApplicationVersionController.uninstall(application_version) ApplicationVersionController.uninstall(application_version)
if application_version.is_installed(): if application_version.is_installed():
raise ApplicationAlreadyInstalledError('The application in question is already installed.') error_msg = 'The application in question is already installed.'
logger.error(error_msg)
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg)
# this used to go through "with_preferred_connection", but now re-uses the same HTTPx client as sync, # this used to go through "with_preferred_connection", but now re-uses the same HTTPx client as sync,
ApplicationVersionController.__install(application_version, application_version_observer, connection_observer) return ApplicationVersionController.__install(application_version, application_version_observer, connection_observer)
# legacy: # legacy:
# from core.controllers.ConnectionController import ConnectionController # from core.controllers.ConnectionController import ConnectionController
@ -87,7 +95,12 @@ class ApplicationVersionController:
ApplicationVersion.save_many(application_versions) ApplicationVersion.save_many(application_versions)
@staticmethod @staticmethod
def __install(application_version: ApplicationVersion, application_version_observer: Optional[ApplicationVersionObserver] = None, connection_observer: Optional[ConnectionObserver] = None): def __install(
application_version: ApplicationVersion,
application_version_observer: Optional[ApplicationVersionObserver] = None,
connection_observer: Optional[ConnectionObserver] = None
) -> Result:
target_app_name = application_version.application_code.capitalize() target_app_name = application_version.application_code.capitalize()
target_app_version = application_version.version_number target_app_version = application_version.version_number
download_path = application_version.download_path download_path = application_version.download_path
@ -105,10 +118,9 @@ class ApplicationVersionController:
connection_observer=connection_observer connection_observer=connection_observer
) )
################################################ if not download_result.valid:
# IT WORKED - SAVE IT return download_result.wrap("failed to download")
################################################
if download_result.valid:
response_buffer = download_result.data response_buffer = download_result.data
file_hash = download_result.message file_hash = download_result.message
@ -118,16 +130,18 @@ class ApplicationVersionController:
with open(f'{application_version.get_installation_path()}/.sha3-512', 'w') as hash_file: with open(f'{application_version.get_installation_path()}/.sha3-512', 'w') as hash_file:
hash_file.write(f'{file_hash}\n') hash_file.write(f'{file_hash}\n')
return Result(valid=True)
################################################ ################################################
# FAILED # FAILED
################################################ ################################################
else: # else:
if download_result.error_type == ResultError.CONNECTION: # if download_result.error_type == ResultError.CONNECTION:
raise ConnectionError(f'Could not connect, to download {target_app_name}.') # # raise ConnectionError(f'Could not connect, to download {target_app_name}.')
elif download_result.error_type == ResultError.INVALID_INPUT: # elif download_result.error_type == ResultError.INVALID_INPUT:
raise FileIntegrityError('Application version file integrity could not be verified.') # raise FileIntegrityError('Application version file integrity could not be verified.')
else: # else:
return None # return None
# legacy: # legacy:

View file

@ -66,8 +66,6 @@ class ThreadPlumbing:
def _on_complete(self, original_result: Result): def _on_complete(self, original_result: Result):
"""Get a potential function prescription and return it to the main thread.""" """Get a potential function prescription and return it to the main thread."""
logger.info(original_result)
if not original_result: if not original_result:
return return

View file

@ -22,6 +22,7 @@ class DBErrorType(Enum):
CORRUPTED_DATABASE = "corrupted_database" CORRUPTED_DATABASE = "corrupted_database"
PYTHON_MODEL_STRUCTURE = "python_model_structure" PYTHON_MODEL_STRUCTURE = "python_model_structure"
SQLALCHEMY_ERROR = "sqlalchemy_error" SQLALCHEMY_ERROR = "sqlalchemy_error"
DETACHED = "detached"
UNKNOWN_MODEL = "unknown_model" UNKNOWN_MODEL = "unknown_model"
UNKNOWN = "unknown" UNKNOWN = "unknown"

View file

@ -0,0 +1,9 @@
from sqlalchemy import inspect
def is_model_stale(obj):
""""checks the internal state of an object, to evaluate if its detatched or expired.""""
ins = inspect(obj)
return ins.detached or ins.expired

View file

@ -58,6 +58,8 @@ def _reinitialize_engine_and_session():
engine = create_engine(f"sqlite:///{database_path}") engine = create_engine(f"sqlite:///{database_path}")
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
# uncomment if going to be destroying SQL threads without clearing cache or DTO:
# Session = sessionmaker(bind=engine, expire_on_commit=False)
@ -83,6 +85,12 @@ def close_session():
_session.close() _session.close()
_session = None _session = None
def create_new_session():
"""Create and return a brand new session instance from the global factory."""
if Session is None:
raise RuntimeError("Session factory not initialized. Call _reinitialize_engine_and_session() first.")
return Session()
# ============================================================================ # ============================================================================
# DATABASE OPERATIONS # DATABASE OPERATIONS
@ -113,21 +121,6 @@ def create_ONLY_db_version_table():
logger.error(f"[DB MANAGEMENT] Failed to create/initialize database_version table: {e}") logger.error(f"[DB MANAGEMENT] Failed to create/initialize database_version table: {e}")
# def create_ONLY_db_version_table():
# """Create only the database_version table using the global engine."""
# if engine is None:
# raise RuntimeError("Engine not initialized. Call _reinitialize_engine_and_session() first.")
# from core.models.orm_models.DatabaseVersion import database_version
# try:
# database_version.create(engine)
# except:
# logger.info("[DB MANAGEMENT] database_version already exists, but was attempted to be made again by calling create_ONLY_db_version_table.")
# # if using without checking or try except blocks:
# # database_version.create(engine, checkfirst=True)
def does_db_version_table_exist(): def does_db_version_table_exist():
if engine is None: if engine is None:
raise RuntimeError("Engine not initialized. Call _reinitialize_engine_and_session() first.") raise RuntimeError("Engine not initialized. Call _reinitialize_engine_and_session() first.")

View file

@ -1,20 +1,21 @@
# custom # custom
from core.errors.logger import logger
from core.models.DatabaseOperation import DatabaseOperation, DBErrorType from core.models.DatabaseOperation import DatabaseOperation, DBErrorType
from core.models.manage.migrations import migrate_sql from core.models.manage.migrations import migrate_sql
from core.models.manage.session_management import get_session from core.models.manage.session_management import get_session, create_new_session
from core.Constants import Constants # from core.Constants import Constants
from core.errors.logger import logger
# generic # generic
from sqlalchemy.orm import Session # from sqlalchemy.orm import Session
from sqlalchemy.exc import ( from sqlalchemy.exc import (
OperationalError, OperationalError,
IntegrityError, IntegrityError,
SQLAlchemyError SQLAlchemyError
) )
from typing import Type, Dict, Any, Callable, TypeVar from sqlalchemy.orm.exc import DetachedInstanceError
from functools import wraps # from typing import Type, Dict, Any, Callable, TypeVar
from enum import Enum # from functools import wraps
# from enum import Enum
import traceback import traceback
@ -47,6 +48,34 @@ def safe_db_operation(func):
# Otherwise, wrap success # Otherwise, wrap success
return DatabaseOperation(valid=True, data=result) return DatabaseOperation(valid=True, data=result)
except DetachedInstanceError as e:
logger.warning(f"DetachedInstanceError: {e}. Retrying with a brand new session.")
# Create a new session on this thread:
temp_session = create_new_session()
try:
# Retry the call with the fresh session
result = func(*args, session=temp_session, **kwargs)
error_msg = "Recovered DetachedInstanceError with new session"
logger.info(error_msg)
return DatabaseOperation(
valid=True,
data=result,
message=error_msg
)
except Exception as retry_error:
logger.error(f"DetachedInstanceError recovery failed with error: {str(retry_error)}", exc_info=True)
session.rollback()
return DatabaseOperation(
valid=False,
error_type=DBErrorType.DETACHED,
message=str(retry_error)
)
finally:
# Close the new session to prevent "Too many connections" errors
temp_session.close()
except OperationalError as e: except OperationalError as e:
logger.error(f"Schema mismatch in {func.__name__}: {e}") logger.error(f"Schema mismatch in {func.__name__}: {e}")
session.rollback() session.rollback()

View file

@ -102,7 +102,9 @@ def enable_session_profile(
application_version = profile.application_version application_version = profile.application_version
if not application_version.is_installed(): if not application_version.is_installed():
ApplicationVersionController.install(application_version, application_version_observer=application_version_observer, connection_observer=connection_observer) install_result = ApplicationVersionController.install(application_version, application_version_observer=application_version_observer, connection_observer=connection_observer)
if not install_result.valid:
return install_result.wrap("Can't enable session profile, because it couldn't get the app")
#----------------------------------- #-----------------------------------
# NETWORK CONFIG # NETWORK CONFIG