From 2008156e2030b71a54650ce4939868f9568cf104 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Fri, 2 May 2025 13:26:57 +0330 Subject: [PATCH] Fix: Remove misuse of asyncio.to_thread with async context manager in remove_user.py --- core/scripts/hysteria2/remove_user.py | 37 ++++++++++----------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/core/scripts/hysteria2/remove_user.py b/core/scripts/hysteria2/remove_user.py index 6538eb8..0663eb4 100644 --- a/core/scripts/hysteria2/remove_user.py +++ b/core/scripts/hysteria2/remove_user.py @@ -7,42 +7,30 @@ import asyncio from init_paths import * from paths import * -async def remove_user(username): - """ - Remove a user from the USERS_FILE asynchronously. - - Args: - username (str): The username to remove - - Returns: - int: 0 on success, 1 on failure - """ +def sync_remove_user(username): if not os.path.isfile(USERS_FILE): - print(f"Error: Config file {USERS_FILE} not found.") - return 1 + return 1, f"Error: Config file {USERS_FILE} not found." try: - async with asyncio.to_thread(open, USERS_FILE, 'r') as f: + with open(USERS_FILE, 'r') as f: try: users_data = json.load(f) except json.JSONDecodeError: - print(f"Error: {USERS_FILE} contains invalid JSON.") - return 1 + return 1, f"Error: {USERS_FILE} contains invalid JSON." if username in users_data: del users_data[username] - async with asyncio.to_thread(open, USERS_FILE, 'w') as f: + with open(USERS_FILE, 'w') as f: json.dump(users_data, f, indent=4) - print(f"User {username} removed successfully.") + return 0, f"User {username} removed successfully." else: - print(f"Error: User {username} not found.") - return 1 + return 1, f"Error: User {username} not found." except Exception as e: - print(f"Error: {str(e)}") - return 1 + return 1, f"Error: {str(e)}" - return 0 +async def remove_user(username): + return await asyncio.to_thread(sync_remove_user, username) async def main(): if len(sys.argv) != 2: @@ -50,8 +38,9 @@ async def main(): sys.exit(1) username = sys.argv[1] - exit_code = await remove_user(username) + exit_code, message = await remove_user(username) + print(message) sys.exit(exit_code) if __name__ == "__main__": - asyncio.run(main()) \ No newline at end of file + asyncio.run(main())