Improved initial load state management for evaluation on background, but kill sessions on mainthread

This commit is contained in:
SimplifiedPrivacy 2026-10-29 19:49:47 -04:00
parent 0b065fb025
commit 9948ac0454
3 changed files with 28 additions and 15 deletions

View file

@ -20,7 +20,7 @@ from core.errors.logger import logger
# generic # generic
from functools import partial from functools import partial
import time
SINGBOX_PROTOCOLS = (ProtocolChoice.HYSTERIA2, ProtocolChoice.VLESS) SINGBOX_PROTOCOLS = (ProtocolChoice.HYSTERIA2, ProtocolChoice.VLESS)
@ -215,7 +215,7 @@ def evaluate_state_on_startup() -> Result:
if systemwide_state: if systemwide_state:
error_msg = "The previous system-wide connection was not correctly turned off before the app was killed. Do you want to evaluate it? (no blindly kills it)" error_msg = "The previous system-wide connection was not correctly turned off before the app was killed. Do you want to evaluate it? (no blindly kills it)"
no_option = partial(kill_any_protocol, current_state=systemwide_state, active_protocol=systemwide_state.protocol) no_option = partial(kill_any_protocol, current_state=systemwide_state, active_protocol=systemwide_state.protocol)
return Result(valid=False, no_option=no_option, error_type=ResultError.ZOMBIE_CONNECTION, message=error_msg) return Result(valid=False, no_option=no_option, error_type=ResultError.ZOMBIE_SYSTEM, message=error_msg)
#----------------------- #-----------------------
@ -223,8 +223,9 @@ def evaluate_state_on_startup() -> Result:
#----------------------- #-----------------------
active_session_profiles = SessionStateController.all() active_session_profiles = SessionStateController.all()
if active_session_profiles: if active_session_profiles:
for each_profile in active_session_profiles: for each_session in active_session_profiles:
logger.info(f"Removing this profile {active_session_profiles} from session since it's a fresh restart.") logger.info(f"Killing profile {each_session}")
SessionStateController.remove(each_profile.id) Notifications.emit(EventType.SESSION_PROFILE_KILL, each_session)
time.sleep(4)
return Result(valid=True) return Result(valid=True)

View file

@ -66,13 +66,18 @@ 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."""
#---------------------------- if not original_result:
return
if isinstance(original_result, Result):
# Wrapper: # Wrapper:
#----------------------------
if not original_result.valid: if not original_result.valid:
logger.error(audit(original_result)) logger.error(audit(original_result))
result = unwrap(original_result) result = unwrap(original_result)
else:
# Convert:
result = convert_to_result(original_result)
#---------------------------- #----------------------------
# Went Right with a New Function Prescription to Run Right Now: # Went Right with a New Function Prescription to Run Right Now:
@ -229,13 +234,19 @@ def convert_to_result(result: Any) -> Result:
logger.error(error_msg) logger.error(error_msg)
if isinstance(result, bool): if isinstance(result, bool):
if result:
return Result(valid=True)
else:
return Result(valid=result, error_type=ResultError.INVALID_INPUT, message=error_msg) return Result(valid=result, error_type=ResultError.INVALID_INPUT, message=error_msg)
elif isinstance(result, dict): elif isinstance(result, dict):
valid = result.get('valid', False) valid = result.get('valid', False)
message = result.get('message', False) message = result.get('message', False)
if not message: if valid:
message = error_msg return Result(valid=True)
if message:
error_msg = message
return Result(valid=valid, error_type=ResultError.INVALID_INPUT, message=error_msg) return Result(valid=valid, error_type=ResultError.INVALID_INPUT, message=error_msg)
else: else:

View file

@ -29,7 +29,8 @@ class ResultError(Enum):
BILLING_CONFLICT = "billing_conflict" BILLING_CONFLICT = "billing_conflict"
STATE_CONFLICT = "state_conflict" # with actions to kill STATE_CONFLICT = "state_conflict" # with actions to kill
STATE = "state" STATE = "state"
ZOMBIE_CONNECTION = "zombie_connection" ZOMBIE_SYSTEM = "zombie_system"
ZOMBIE_SESSION = "zombie_session"
TICKET = "ticket" TICKET = "ticket"
NO_TICKET = "no_ticket" NO_TICKET = "no_ticket"
RANDOM_TICKETS_OFF = "random_tickets_off" RANDOM_TICKETS_OFF = "random_tickets_off"