浏览代码

fix potential race issue

tuanchris 3 月之前
父节点
当前提交
6a730f3189
共有 3 个文件被更改,包括 12 次插入8 次删除
  1. 5 3
      main.py
  2. 3 3
      modules/core/pattern_manager.py
  3. 4 2
      modules/core/playlist_manager.py

+ 5 - 3
main.py

@@ -674,10 +674,12 @@ async def run_theta_rho(request: ThetaRhoRequest, background_tasks: BackgroundTa
             logger.warning("Attempted to run a pattern without a connection")
             raise HTTPException(status_code=400, detail="Connection not established")
         
+        # If another pattern is running, stop it first
         if pattern_manager.pattern_lock.locked():
-            logger.warning("Attempted to run a pattern while another is already running")
-            raise HTTPException(status_code=409, detail="Another pattern is already running")
-            
+            logger.info("Another pattern is running - stopping it before starting new pattern")
+            await pattern_manager.stop_actions(clear_playlist=True, wait_for_lock=True)
+            logger.info("Previous pattern stopped successfully")
+
         files_to_run = [file_path]
         logger.info(f'Running theta-rho file: {request.file_name} with pre_execution={request.pre_execution}')
         

+ 3 - 3
modules/core/pattern_manager.py

@@ -592,9 +592,9 @@ def is_clear_pattern(file_path):
 
 async def run_theta_rho_file(file_path, is_playlist=False):
     """Run a theta-rho file by sending data in optimized batches with tqdm ETA tracking."""
-    if pattern_lock.locked():
-        logger.warning("Another pattern is already running. Cannot start a new one.")
-        return
+    # if pattern_lock.locked():
+    #     logger.warning("Another pattern is already running. Cannot start a new one.")
+    #     return
 
     async with pattern_lock:  # This ensures only one pattern can run at a time
         # Start progress update task only if not part of a playlist

+ 4 - 2
modules/core/playlist_manager.py

@@ -88,9 +88,11 @@ def add_to_playlist(playlist_name, pattern):
 
 async def run_playlist(playlist_name, pause_time=0, clear_pattern=None, run_mode="single", shuffle=False):
     """Run a playlist with the given options."""
+    # If another pattern is running, stop it first
     if pattern_manager.pattern_lock.locked():
-        logger.warning("Cannot start playlist: Another pattern is already running")
-        return False, "Cannot start playlist: Another pattern is already running"
+        logger.info("Another pattern is running - stopping it before starting new playlist")
+        await pattern_manager.stop_actions(clear_playlist=True, wait_for_lock=True)
+        logger.info("Previous pattern stopped successfully")
 
     playlists = load_playlists()
     if playlist_name not in playlists: