LmeSzinc / AzurLaneAutoScript

Azur Lane bot (CN/EN/JP/TW) 碧蓝航线脚本 | 无缝委托科研,全自动大世界
https://alas.azurlane.cloud
GNU General Public License v3.0
6.96k stars 833 forks source link

ALAS 发现更新的时候会停止执行任务 #1241

Open yzbyzz opened 2 years ago

yzbyzz commented 2 years ago

在提问之前...

描述你的问题

当 ALAS 发现有更新的时候,会停止执行任务。 但是没有执行更新操作,也没有继续执行任务。

如何复现

  1. 正常运行 ALAS
  2. ALAS 发现有更新,尝试进行更新。此时会停止执行任务

预期行为

希望在发现更新后

相关 Logs

═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
                                                       TACTICAL                                                        
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
2022-06-21 03:49:46.246 | INFO | <<< UI ENSURE >>>                                                                     
2022-06-21 03:49:46.253 | INFO | UI get current page                                                                   
2022-06-21 03:49:46.281 | INFO | [UI] page_main                                                                        
2022-06-21 03:49:46.299 | INFO | Goto page_reward                                                                      
2022-06-21 03:49:46.306 | INFO | <<< UI GOTO PAGE_REWARD >>>                                                           
2022-06-21 03:49:46.931 | INFO | Page switch: page_main -> page_reward                                                 
2022-06-21 03:49:46.944 | INFO | Click (  21,  170) @ MAIN_GOTO_REWARD                                                 
2022-06-21 03:49:52.063 | INFO | Page arrive: page_reward                                                              
═══════════════════════════════════════════════ TACTICAL CLASS RECEIVE ════════════════════════════════════════════════
2022-06-21 03:49:52.104 | INFO | TACTICAL CLASS RECEIVE                                                                
2022-06-21 03:49:52.136 | INFO | Click ( 432,  419) @ REWARD_2                                                         
2022-06-21 03:49:55.683 | INFO | Click ( 769,  517) @ POPUP_CONFIRM_TACTICAL                                           
──────────────────────────────────────────────── TACTICAL BOOKS CHOOSE ────────────────────────────────────────────────
2022-06-21 03:49:58.841 | INFO | TACTICAL BOOKS CHOOSE                                                                 
2022-06-21 03:49:58.859 | INFO | [Book_count] 11                                                                       
2022-06-21 03:49:58.863 | INFO | [Books] [Red_T4, Blue_T4_Exp, Red_T3, Blue_T3_Exp, Yellow_T3, Red_T2, Blue_T2_Exp,    
Yellow_T2, Red_T1, Blue_T1_Exp, Yellow_T1]                                                                             
2022-06-21 03:50:00.390 | INFO | [Book_count] 11                                                                       
2022-06-21 03:50:00.406 | INFO | [Books] [Red_T4, Blue_T4_Exp, Red_T3, Blue_T3_Exp, Yellow_T3, Red_T2, Blue_T2_Exp,    
Yellow_T2, Red_T1, Blue_T1_Exp, Yellow_T1]                                                                             
2022-06-21 03:50:00.485 | INFO | Loading OCR model: ./bin/cnocr_models/azur_lane                                       
2022-06-21 03:50:01.114 | INFO | [OCR_SKILL_EXP 0.689s] 1150/4400                                                      
2022-06-21 03:50:01.121 | INFO | [Book_sort] Blue_T4_Exp > Blue_T3_Exp > Blue_T2_Exp > Blue_T1_Exp > Yellow_T2 > Red_T2
> Yellow_T3 > Red_T3 > Yellow_T1 > Red_T4 > Red_T1 > first                                                             
2022-06-21 03:50:01.130 | INFO | Click ( 403,  307) @ BOOKS_GRID_1_0                                                   
2022-06-21 03:50:02.871 | INFO | Click (1094,  632) @ TACTICAL_CLASS_START                                             
2022-06-21 03:50:05.645 | INFO | Click ( 767,  505) @ POPUP_CONFIRM_TACTICAL                                           
2022-06-21 03:50:08.671 | INFO | <<< TACTICAL GET FINISH >>>                                                           
2022-06-21 03:50:08.697 | INFO | Tactical status: ['running', 'running', 'empty', 'empty']                             
2022-06-21 03:50:09.340 | INFO | [TACTICAL_REMAIN 0.631s] ['11:38:19', '12:00:00']                                     
2022-06-21 03:50:09.354 | INFO | Tactical finish: ['2022-06-21 15:28:28', '2022-06-21 15:50:09']                       
2022-06-21 03:50:09.372 | INFO | Click (  55,   44) @ BACK_ARROW                                                       
2022-06-21 03:50:10.811 | INFO | Click ( 795,  512) @ POPUP_CONFIRM_TACTICAL                                           
──────────────────────────────────────────────── TACTICAL BOOKS CHOOSE ────────────────────────────────────────────────
2022-06-21 03:50:13.663 | INFO | TACTICAL BOOKS CHOOSE                                                                 
2022-06-21 03:50:13.688 | INFO | [Book_count] 10                                                                       
2022-06-21 03:50:13.705 | INFO | [Books] [Red_T4, Red_T3, Blue_T3, Yellow_T3_Exp, Red_T2, Blue_T2, Yellow_T2_Exp,      
Red_T1, Blue_T1, Yellow_T1_Exp]                                                                                        
2022-06-21 03:50:15.125 | INFO | [Book_count] 10                                                                       
2022-06-21 03:50:15.133 | INFO | [Books] [Red_T4, Red_T3, Blue_T3, Yellow_T3_Exp, Red_T2, Blue_T2, Yellow_T2_Exp,      
Red_T1, Blue_T1, Yellow_T1_Exp]                                                                                        
2022-06-21 03:50:15.597 | INFO | [OCR_SKILL_EXP 0.437s] 1900/4400                                                      
2022-06-21 03:50:15.610 | INFO | [Book_sort] Yellow_T3_Exp > Yellow_T2_Exp > Yellow_T1_Exp > Blue_T2 > Red_T2 > Blue_T3
> Red_T3 > Blue_T1 > Red_T4 > Red_T1 > first                                                                           
2022-06-21 03:50:15.625 | INFO | Click ( 711,  348) @ BOOKS_GRID_3_0                                                   
2022-06-21 03:50:17.507 | INFO | Click (1114,  628) @ TACTICAL_CLASS_START                                             
2022-06-21 03:50:20.428 | INFO | Click ( 750,  509) @ POPUP_CONFIRM_TACTICAL                                           
2022-06-21 03:50:23.163 | INFO | <<< TACTICAL GET FINISH >>>                                                           
2022-06-21 03:50:23.176 | INFO | Tactical status: ['running', 'running', 'running', 'empty']                           
2022-06-21 03:50:23.746 | INFO | [TACTICAL_REMAIN 0.557s] ['11:38:04', '11:59:45', '08:00:00']                         
2022-06-21 03:50:23.759 | INFO | Tactical finish: ['2022-06-21 15:28:27', '2022-06-21 15:50:08', '2022-06-21 11:50:23']
2022-06-21 03:50:23.769 | INFO | Click (  59,   57) @ BACK_ARROW                                                       
2022-06-21 03:50:27.444 | INFO | Delay task `Tactical` to 2022-06-21 11:50:23 (target=datetime.datetime(2022, 6, 21,   
11, 50, 23))                                                                                                           
2022-06-21 03:50:27.491 | INFO | Bind task {'General', 'Alas', 'Tactical'}                                             
2022-06-21 03:50:27.507 | INFO | Save config ./config\alas_冲绳岛.json,                                                
Tactical.Scheduler.NextRun=datetime.datetime(2022, 6, 21, 11, 50, 23)                                                  
2022-06-21 03:50:27.636 | INFO | Scheduler: End task `Tactical`                                                        
2022-06-21 03:50:27.647 | INFO | [Server] cn                                                                           
2022-06-21 03:50:27.714 | INFO | Bind task {'General', 'Alas'}                                                         
2022-06-21 03:50:27.727 | INFO | No task pending                                                                       
2022-06-21 03:50:27.738 | INFO | [Task] Commission (Enable, 2022-06-21 03:56:23)                                       
2022-06-21 03:50:27.756 | INFO | Bind task {'General', 'Alas', 'Commission'}                                           
2022-06-21 03:50:27.788 | INFO | Wait until 2022-06-21 03:56:23 for task `Commission`                                  
2022-06-21 03:50:27.811 | INFO | Goto main page during wait                                                            
2022-06-21 03:50:30.006 | INFO | [Package_name] com.bilibili.azurlane                                                  
2022-06-21 03:50:30.012 | INFO | App is already running, goto main page                                                
2022-06-21 03:50:30.022 | INFO | <<< UI ENSURE >>>                                                                     
2022-06-21 03:50:30.037 | INFO | UI get current page                                                                   
2022-06-21 03:50:30.094 | INFO | [UI] page_reward                                                                      
2022-06-21 03:50:30.105 | INFO | Goto page_main                                                                        
2022-06-21 03:50:30.118 | INFO | <<< UI GOTO PAGE_MAIN >>>                                                             
2022-06-21 03:50:30.179 | INFO | Page switch: page_reward -> page_main                                                 
2022-06-21 03:50:30.186 | INFO | Click (1072,  631) @ REWARD_GOTO_MAIN                                                 
2022-06-21 03:50:34.295 | INFO | Page arrive: page_main                                                                
2022-06-21 03:51:09.330 | INFO | Update event detected                                                                 
2022-06-21 03:51:09.347 | INFO | [alas_冲绳岛] exited. Reason: Update

截图

No response

还有别的吗?

一些猜测: (日志里没有看到相关的信息加以验证,仅做参考)

LmeSzinc commented 2 years ago

还需要当天的gui.txt

yzbyzz commented 2 years ago

还需要当天的gui.txt

FYI

═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
                                                         START                                                         
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
2022-06-21 00:00:07.857 | INFO | No update                                                                             
2022-06-21 00:04:15.140 | INFO | End of log queue handler loop                                                         
2022-06-21 00:04:15.153 | INFO | [alas_冲绳岛] exited                                                                  
2022-06-21 00:05:09.356 | INFO | No update                                                                             
2022-06-21 00:08:25.282 | INFO | Task <Switch_scheduler_btn_refresh (delay=1)> removed.                                
2022-06-21 00:08:25.289 | INFO | Task <Switch_log_scroll_btn_refresh (delay=1)> removed.                               
2022-06-21 00:08:25.303 | INFO | Task <alas_update_overiew_task (delay=10)> removed.                                   
2022-06-21 00:08:25.319 | INFO | Task <put_log (delay=0.25)> removed.                                                  
2022-06-21 00:08:43.312 | INFO | Add task <Switch_scheduler_btn_refresh (delay=1)>                                     
2022-06-21 00:08:43.345 | INFO | Add task <Switch_log_scroll_btn_refresh (delay=1)>                                    
2022-06-21 00:08:43.363 | INFO | Add task <alas_update_overiew_task (delay=10)>                                        
2022-06-21 00:08:43.374 | INFO | Add task <put_log (delay=0.25)>                                                       
2022-06-21 00:10:11.946 | INFO | No update                                                                             
2022-06-21 00:15:14.680 | INFO | No update                                                                             
2022-06-21 00:20:16.842 | INFO | No update                                                                             
2022-06-21 00:25:18.233 | INFO | No update                                                                             
2022-06-21 00:30:19.528 | INFO | No update                                                                             
2022-06-21 00:35:20.831 | INFO | No update                                                                             
2022-06-21 00:40:22.105 | INFO | No update                                                                             
2022-06-21 00:45:23.299 | INFO | No update                                                                             
2022-06-21 00:50:24.453 | INFO | No update                                                                             
2022-06-21 00:55:25.691 | INFO | No update                                                                             
2022-06-21 01:00:26.882 | INFO | No update                                                                             
2022-06-21 01:05:28.144 | INFO | No update                                                                             
2022-06-21 01:10:29.972 | INFO | No update                                                                             
2022-06-21 01:15:31.214 | INFO | No update                                                                             
2022-06-21 01:20:32.494 | INFO | No update                                                                             
2022-06-21 01:25:33.719 | INFO | No update                                                                             
2022-06-21 01:30:34.984 | INFO | No update                                                                             
2022-06-21 01:35:36.231 | INFO | No update                                                                             
2022-06-21 01:40:37.922 | INFO | No update                                                                             
2022-06-21 01:45:39.184 | INFO | No update                                                                             
2022-06-21 01:50:40.554 | INFO | No update                                                                             
2022-06-21 01:55:41.779 | INFO | No update                                                                             
2022-06-21 02:00:43.183 | INFO | No update                                                                             
2022-06-21 02:05:44.442 | INFO | No update                                                                             
2022-06-21 02:10:45.778 | INFO | No update                                                                             
2022-06-21 02:15:46.998 | INFO | No update                                                                             
2022-06-21 02:20:48.251 | INFO | No update                                                                             
2022-06-21 02:25:49.566 | INFO | No update                                                                             
2022-06-21 02:30:50.777 | INFO | No update                                                                             
2022-06-21 02:35:52.021 | INFO | No update                                                                             
2022-06-21 02:41:05.585 | INFO | New update avaliable                                                                  
2022-06-21 02:41:05.589 | INFO | b1ab1ce0 - Merge pull request #1239 from LmeSzinc/dev                                 
2022-06-21 03:51:07.639 | INFO | Waiting all running alas finish.                                                      
2022-06-21 07:49:44.112 | INFO | Task <Switch_scheduler_btn_refresh (delay=1)> removed.                                
2022-06-21 07:49:44.180 | INFO | Task <Switch_log_scroll_btn_refresh (delay=1)> removed.                               
2022-06-21 07:49:44.205 | INFO | Task <alas_update_overiew_task (delay=10)> removed.                                   
2022-06-21 07:49:44.209 | INFO | Task <put_log (delay=0.25)> removed.                                                  
2022-06-21 07:49:45.173 | INFO | Add task <Switch_updater_refresh (delay=0.5)>                                         
2022-06-21 07:50:21.791 | INFO | Task <Switch_updater_refresh (delay=0.5)> removed.                                    
2022-06-21 07:50:21.799 | INFO | [Server] cn                                                                           
2022-06-21 07:50:22.016 | INFO | Bind task {'', 'Alas', 'General'}                                                     
2022-06-21 07:50:22.675 | INFO | Add task <Switch_scheduler_btn_refresh (delay=1)>                                     
2022-06-21 07:50:22.684 | INFO | Add task <Switch_log_scroll_btn_refresh (delay=1)>                                    
2022-06-21 07:50:22.690 | INFO | Add task <alas_update_overiew_task (delay=10)>                                        
2022-06-21 07:50:22.743 | INFO | Add task <put_log (delay=0.25)>                                                       
2022-06-21 07:50:24.316 | INFO | Alas [alas_冲绳岛] stopped                                                            
2022-06-21 07:50:24.349 | INFO | Remains: []                                                                           
2022-06-21 07:50:24.694 | INFO | All alas stopped, start updating                                                      
2022-06-21 07:50:24.747 | INFO | <<< RUN UPDATE >>>                                                                    
2022-06-21 07:50:24.800 | INFO | +---------------------------------------------------+                                 
2022-06-21 07:50:24.850 | INFO | |                    Update Alas                    |                                 
2022-06-21 07:50:24.874 | INFO | End of log queue handler loop                                                         
2022-06-21 07:50:24.939 | WARNING | Log queue handler thread does not stop within 1 seconds                            
2022-06-21 07:50:24.949 | INFO | +---------------------------------------------------+                                 
2022-06-21 07:50:25.093 | INFO | [alas_冲绳岛] exited                                                                  
2022-06-21 07:50:25.115 | INFO | ==================== Git Init ====================                                    
2022-06-21 07:50:25.189 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" init        
2022-06-21 07:50:25.547 | INFO | [ success ]                                                                           
2022-06-21 07:50:25.598 | INFO | ==================== Set Git Proxy ====================                               
2022-06-21 07:50:25.674 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" config      
--local --unset http.proxy                                                                                             
2022-06-21 07:50:25.971 | INFO | [ allowed failure ], error_code: 5                                                    
2022-06-21 07:50:26.034 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" config      
--local --unset https.proxy                                                                                            
2022-06-21 07:50:26.281 | INFO | [ allowed failure ], error_code: 5                                                    
2022-06-21 07:50:26.377 | INFO | ==================== Set Git Repository ====================                          
2022-06-21 07:50:26.423 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" remote      
set-url origin https://gitee.com/LmeSzinc/AzurLaneAutoScript                                                           
2022-06-21 07:50:26.695 | INFO | [ success ]                                                                           
2022-06-21 07:50:26.717 | INFO | ==================== Fetch Repository Branch ====================                     
2022-06-21 07:50:26.784 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" fetch origin
master                                                                                                                 
2022-06-21 07:50:27.905 | INFO | [alas_冲绳岛] exited                                                                  
2022-06-21 07:50:28.323 | INFO | [ success ]                                                                           
2022-06-21 07:50:28.409 | INFO | ==================== Pull Repository Branch ====================                      
2022-06-21 07:50:28.447 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" reset --hard
origin/master                                                                                                          
2022-06-21 07:50:33.039 | INFO | [ success ]                                                                           
2022-06-21 07:50:33.054 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" pull        
--ff-only origin master                                                                                                
2022-06-21 07:50:33.817 | INFO | Task <Switch_scheduler_btn_refresh (delay=1)> removed.                                
2022-06-21 07:50:33.844 | INFO | Task <Switch_log_scroll_btn_refresh (delay=1)> removed.                               
2022-06-21 07:50:33.852 | INFO | Task <alas_update_overiew_task (delay=10)> removed.                                   
2022-06-21 07:50:33.859 | INFO | Task <put_log (delay=0.25)> removed.                                                  
2022-06-21 07:50:34.444 | INFO | [ success ]                                                                           
2022-06-21 07:50:34.463 | INFO | ==================== Show Version ====================                                
2022-06-21 07:50:34.467 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/Git/mingw64/bin/git.exe" log         
--no-merges -1                                                                                                         
2022-06-21 07:50:34.647 | INFO | [ success ]                                                                           
2022-06-21 07:50:34.653 | INFO | +-----------------------------------------------------------+                         
2022-06-21 07:50:34.659 | INFO | |                    Update Dependencies                    |                         
2022-06-21 07:50:34.673 | INFO | +-----------------------------------------------------------+                         
2022-06-21 07:50:34.687 | INFO | ==================== Check Python ====================                                
2022-06-21 07:50:34.699 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/python.exe" --version                
2022-06-21 07:50:36.266 | INFO | [ success ]                                                                           
2022-06-21 07:50:36.272 | INFO | ==================== Update Dependencies ====================                         
2022-06-21 07:50:36.282 | INFO | "D:/App/AlasApp_0.3.5/AzurLaneAutoScript/toolkit/python.exe" -m pip install -r        
requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --disable-pip-version-check                               
2022-06-21 07:50:36.290 | INFO | Add task <Switch_updater_refresh (delay=0.5)>                                         
2022-06-21 07:50:45.918 | INFO | [ success ]                                                                           
2022-06-21 07:50:45.961 | INFO | Start clearup                                                                         
2022-06-21 07:50:45.968 | INFO | [alas_硫磺岛] exited                                                                  
2022-06-21 07:50:45.979 | INFO | [alas_冲绳岛] exited      
yzbyzz commented 2 years ago

补充一些信息,出现问题的环境

做了如下操作,可以稳定复现更新失败的情况。

1 app.py startup

task_handler.add(updater.schedule_update(), 86400)

修改为

task_handler.add(updater.schedule_update(), 5)

2 updater.py schedule_update

th._task.delay = get_next_time(self.schedule_time)

修改为

th._task.delay = 5

3 updater.py _wait_update

time.sleep(0.25)

修改为

logger.info(f"Remains instances: {_instances}") time.sleep(5)

4 updater.py init

增加

self.cnt = 0

5 updater.py _check_update

整个方法改为

    self.cnt += 1
    if self.cnt < 6:
        logger.info(f"No update")
        return False
    else:
        logger.info(f"New update avaliable")
        return True

- 双击 alas.exe 运行,会撤销本地代码改动。所以使用 console.bat + python gui.py 的方式启动 ALAS。
- 浏览器打开 http://127.0.0.1:22267 ,启动一个调度器。
- 等待 30 秒后,走到更新的流程。
- 同样会卡住。日志如下

```Text
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
                                                         START                                                         
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
2022-06-24 23:54:53.914 | INFO | New update avaliable                                                                  
2022-06-24 23:54:53.935 | INFO | Waiting all running alas finish.                                                      
2022-06-24 23:54:53.955 | INFO | Remains instances: [<module.webui.process_manager.ProcessManager object at            
0x000000000C41C388>]                                                                                                   
2022-06-24 23:54:58.973 | INFO | Remains instances: [<module.webui.process_manager.ProcessManager object at            
0x000000000C41C388>]                                                                                                   
2022-06-24 23:55:03.999 | INFO | Remains instances: [<module.webui.process_manager.ProcessManager object at            
0x000000000C41C388>]                                                                                                   
2022-06-24 23:55:09.027 | INFO | Remains instances: [<module.webui.process_manager.ProcessManager object at            
0x000000000C41C388>]                                                                                                   
2022-06-24 23:55:14.055 | INFO | Remains instances: [<module.webui.process_manager.ProcessManager object at            
0x000000000C41C388>]                  

其他信息

本问题在 Windows7 上稳定出现,在 Mac 上没有复现。Windows10 待验证。 如果只是 Windows7 的问题,这里建议增加选项控制是否自动更新。 现阶段,我会注释 app.py 里的 task_handler.add(updater.schedule_update(), 86400) 来关闭自动更新。

18870 commented 2 years ago

https://github.com/LmeSzinc/AzurLaneAutoScript/blob/7bfff9019ea1740e93cb20ba34b7958230508b2b/config/deploy.template.yaml#L92-L97

改成 null 可以禁用自动更新

python -m module.webui.updater 可以直接运行更新部分的代码。 我无法复现这个问题,需要你帮忙对这里debug。 https://github.com/LmeSzinc/AzurLaneAutoScript/blob/7bfff9019ea1740e93cb20ba34b7958230508b2b/module/webui/updater.py#L209-L219

为了打印log这里monkey patch了内置的print函数,不知道是不是这个出了问题

yzbyzz commented 2 years ago
2022-06-24 23:55:14.055 | INFO | Remains instances: [<module.webui.process_manager.ProcessManager object at            
0x000000000C41C388>]  

从日志来看,我猜测出问题的地方是在 updater.py 发现有更新,等待所有进程退出的过程中出错。(应该还没到执行更新的步骤。) 这里具体的问题在,ALAS 没有继续执行任务了,但是对应的进程还在运行。 从表现来看就是 ALAS 已经不会执行任务了,但是 Web 页面上 调度器按钮还是处于 “运行”状态,需要手动点击“停止”。 此时 ProcessManager 返回的 running_instances 才会为空,也才会执行后面的更新步骤。

我怀疑 Windows7 下发现更新时,不能正确停止对应的 ProcessManager,所以无法更新。 这里我再做进一步的验证。

18870 commented 2 years ago

~原来不是一个问题~

https://github.com/LmeSzinc/AzurLaneAutoScript/blob/3aafd4a5f22c37afa9146eb37b25590c50bd2105/alas.py#L339-L343

我发现 exit 只会退出当前线程,尝试在 exit 调用前打印现在存活的进程。

import threading
logger.info(threading.enumerate())
INFO     11:48:00.855 │ Update event detected                                  
INFO     11:48:00.858 │ [alas] exited. Reason: Update                          
INFO     11:48:00.860 │ [<_MainThread(MainThread, started 1181404)>]           

结果应该只有一个主线程

yzbyzz commented 2 years ago

一些验证

在 module/webui/process_manager.py 中增加方法
    @classmethod
    def show_process(cls):
        """
        Show process brief
        """
        for process in cls._processes.values():
            logger.info(f"[{process}] -> [{process.alive}]")

修改 module/webui/updater.py
    def _wait_update(self, instances, names):
        if self.state == "cancel":
            self.state = 1
        self.state = "wait"
        self.event.set()
        _instances = instances.copy()
        while _instances:
            for alas in _instances:
                if not alas.alive:
                    _instances.remove(alas)
                    logger.info(f"Alas [{alas.config_name}] stopped")
                    logger.info(f"Remains: {[alas.config_name for alas in _instances]}")
            if self.state == "cancel":
                self.state = 1
                self.event.clear()
                ProcessManager.restart_processes(instances, self.event)
                return
            # >>>>>>>>>>>>>>>>>>>>>
            logger.info(f"running instances: {_instances}")
            ProcessManager.show_process()
            time.sleep(5)
            # <<<<<<<<<<<<<<<<<<<<<
        self._run_update(instances, names)

在 Windows7 下

可以看到发现更新后,日志如下

2022-06-25 10:19:11.153 | INFO | [<module.webui.process_manager.ProcessManager object at 0x1146a17d0>] -> [True]      

在浏览器中手动点击“停止”按钮后,日志如下

2022-06-25 10:20:11.153 | INFO | [<module.webui.process_manager.ProcessManager object at 0x1146a17d0>] -> [False]      

此时才会开始执行更新的步骤。

在 Mac 下

发现更新后,日志如下,(不需要手动点击“停止”按钮,就会执行更新)

2022-06-25 10:29:11.153 | INFO | [<module.webui.process_manager.ProcessManager object at 0x1146a17d0>] -> [False]      

目前的结论

Windows7 下 is_alive 判断不准确? https://github.com/LmeSzinc/AzurLaneAutoScript/blob/7bfff9019ea1740e93cb20ba34b7958230508b2b/module/webui/process_manager.py?_pjax=%23js-repo-pjax-container#L79-L84

从下面几篇链接来看,process kill 后的一段时间内 is_alive 仍然可能为 true。

但是目前的检查进程结束,是一个循环判断,这样看来 is_alive 应该是一直返回 true。 所以问题是否在于目前进程退出的方式 exit 在 Windows7 下不生效? https://github.com/LmeSzinc/AzurLaneAutoScript/blob/7bfff9019ea1740e93cb20ba34b7958230508b2b/alas.py#L339-L341

yzbyzz commented 2 years ago

...

INFO     11:48:00.855 │ Update event detected                                  
INFO     11:48:00.858 │ [alas] exited. Reason: Update                          
INFO     11:48:00.860 │ [<_MainThread(MainThread, started 1181404)>]           

结果应该只有一个主线程

Windows7 下打印线程,结果相同,只有一个线程

2022-06-25 12:45:45.609 | INFO | Update event detected                                                                 
2022-06-25 12:45:45.634 | INFO | [alas_硫磺岛] exited. Reason: Update                                                  
2022-06-25 12:45:45.647 | INFO | [<_MainThread(MainThread, started 2388)>]  
yzbyzz commented 2 years ago

重新跑了一次,日志比较清晰一点。

完整日志

alas.txt ```Text ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:51:17.828 | INFO | Start scheduler loop: alas_硫磺岛 2022-06-25 12:51:17.847 | INFO | [Server] cn 2022-06-25 12:51:17.902 | INFO | Bind task {'Alas', 'General'} 2022-06-25 12:51:17.932 | INFO | No task pending 2022-06-25 12:51:17.942 | INFO | [Task] Commission (Enable, 2022-06-25 13:38:09) 2022-06-25 12:51:17.970 | INFO | Bind task {'Commission', 'Alas', 'General'} 2022-06-25 12:51:23.156 | INFO | Wait until 2022-06-25 13:38:09 for task `Commission` 2022-06-25 12:51:23.174 | INFO | Goto main page during wait ═══════════════════════════════════════════════════════ DEVICE ════════════════════════════════════════════════════════ 2022-06-25 12:51:24.019 | INFO | DEVICE 2022-06-25 12:51:24.047 | INFO | [AdbBinary] D:\App\AlasApp_0.3.5\AzurLaneAutoScript__ForTest\toolkit\Lib\site-packages\adbutils\binaries\adb.exe 2022-06-25 12:51:24.078 | INFO | [AdbClient] AdbClient(127.0.0.1, 5037) 2022-06-25 12:51:24.109 | INFO | <<< DETECT DEVICE >>> 2022-06-25 12:51:24.134 | INFO | Here are the available devices, copy to Alas.Emulator.Serial to use it or set Alas.Emulator.Serial="auto" 2022-06-25 12:51:24.166 | INFO | 127.0.0.1:7555 2022-06-25 12:51:24.223 | INFO | already connected to 127.0.0.1:7555 2022-06-25 12:51:24.237 | INFO | [AdbDevice] AdbDevice(serial=127.0.0.1:7555) 2022-06-25 12:51:24.276 | INFO | [PackageName] com.bilibili.azurlane 2022-06-25 12:51:24.296 | INFO | [Server] cn 2022-06-25 12:51:24.321 | INFO | Screenshot interval set to 0.3s 2022-06-25 12:51:26.671 | INFO | [Screen_size] 1280x720 2022-06-25 12:51:29.117 | INFO | [Package_name] com.bilibili.azurlane 2022-06-25 12:51:29.127 | INFO | App is already running, goto main page 2022-06-25 12:51:29.142 | INFO | <<< UI ENSURE >>> 2022-06-25 12:51:29.165 | INFO | UI get current page 2022-06-25 12:51:29.245 | INFO | [UI] page_main 2022-06-25 12:51:29.266 | INFO | Already at page_main 2022-06-25 12:51:44.292 | INFO | Update event detected 2022-06-25 12:51:44.324 | INFO | [alas_硫磺岛] exited. Reason: Update 2022-06-25 12:51:44.344 | INFO | [<_MainThread(MainThread, started 6992)>] ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:52:57.050 | INFO | Start scheduler loop: alas_硫磺岛 2022-06-25 12:52:57.081 | INFO | [Server] cn 2022-06-25 12:52:57.172 | INFO | Bind task {'Alas', 'General'} 2022-06-25 12:52:57.191 | INFO | No task pending 2022-06-25 12:52:57.210 | INFO | [Task] Commission (Enable, 2022-06-25 13:38:09) 2022-06-25 12:52:57.249 | INFO | Bind task {'Commission', 'Alas', 'General'} 2022-06-25 12:53:02.433 | INFO | Wait until 2022-06-25 13:38:09 for task `Commission` 2022-06-25 12:53:02.455 | INFO | Goto main page during wait ═══════════════════════════════════════════════════════ DEVICE ════════════════════════════════════════════════════════ 2022-06-25 12:53:03.276 | INFO | DEVICE 2022-06-25 12:53:03.285 | INFO | [AdbBinary] D:\App\AlasApp_0.3.5\AzurLaneAutoScript__ForTest\toolkit\Lib\site-packages\adbutils\binaries\adb.exe 2022-06-25 12:53:03.320 | INFO | [AdbClient] AdbClient(127.0.0.1, 5037) 2022-06-25 12:53:03.343 | INFO | <<< DETECT DEVICE >>> 2022-06-25 12:53:03.371 | INFO | Here are the available devices, copy to Alas.Emulator.Serial to use it or set Alas.Emulator.Serial="auto" 2022-06-25 12:53:03.402 | INFO | 127.0.0.1:7555 2022-06-25 12:53:03.425 | INFO | already connected to 127.0.0.1:7555 2022-06-25 12:53:03.445 | INFO | [AdbDevice] AdbDevice(serial=127.0.0.1:7555) 2022-06-25 12:53:03.468 | INFO | [PackageName] com.bilibili.azurlane 2022-06-25 12:53:03.498 | INFO | [Server] cn 2022-06-25 12:53:03.515 | INFO | Screenshot interval set to 0.3s 2022-06-25 12:53:05.698 | INFO | [Screen_size] 1280x720 2022-06-25 12:53:08.073 | INFO | [Package_name] com.bilibili.azurlane 2022-06-25 12:53:08.089 | INFO | App is already running, goto main page 2022-06-25 12:53:08.106 | INFO | <<< UI ENSURE >>> 2022-06-25 12:53:08.127 | INFO | UI get current page 2022-06-25 12:53:08.198 | INFO | [UI] page_main 2022-06-25 12:53:08.212 | INFO | Already at page_main ```
gui.txt ```Text ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:51:03.648 | INFO | <<< WEBUI CONFIGS >>> 2022-06-25 12:51:03.688 | INFO | [Theme] default 2022-06-25 12:51:03.707 | INFO | [Language] zh-CN 2022-06-25 12:51:03.715 | INFO | [Password] False 2022-06-25 12:51:03.726 | INFO | [CDN] False 2022-06-25 12:51:03.754 | INFO | [Electron] False 2022-06-25 12:51:03.773 | INFO | <<< APP.PY STARTUP >>> 2022-06-25 12:51:03.804 | INFO | app.py startup ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:51:05.143 | INFO | Add task 2022-06-25 12:51:05.172 | INFO | Start task handler 2022-06-25 12:51:05.189 | INFO | <<< RESTART ALAS >>> 2022-06-25 12:51:05.236 | INFO | Start alas complete 2022-06-25 12:51:10.183 | INFO | >>>> _chec_update, cnt:1 2022-06-25 12:51:10.209 | INFO | No update 2022-06-25 12:51:12.402 | INFO | [Server] cn 2022-06-25 12:51:12.424 | INFO | Using template config, which is read only 2022-06-25 12:51:12.577 | INFO | Add task 2022-06-25 12:51:12.626 | INFO | Add task 2022-06-25 12:51:12.645 | INFO | Add task 2022-06-25 12:51:12.676 | INFO | Start task handler 2022-06-25 12:51:12.708 | INFO | [Server] cn 2022-06-25 12:51:12.773 | INFO | Bind task {'', 'General', 'Alas'} 2022-06-25 12:51:13.474 | INFO | Add task 2022-06-25 12:51:13.501 | INFO | Add task 2022-06-25 12:51:13.527 | INFO | Add task 2022-06-25 12:51:13.554 | INFO | Add task 2022-06-25 12:51:15.245 | INFO | >>>> _chec_update, cnt:2 2022-06-25 12:51:15.262 | INFO | No update ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:51:20.278 | INFO | >>>> _chec_update, cnt:3 2022-06-25 12:51:20.291 | INFO | No update 2022-06-25 12:51:25.346 | INFO | >>>> _chec_update, cnt:4 2022-06-25 12:51:25.369 | INFO | No update 2022-06-25 12:51:30.376 | INFO | >>>> _chec_update, cnt:5 2022-06-25 12:51:30.400 | INFO | No update 2022-06-25 12:51:35.427 | INFO | >>>> _chec_update, cnt:6 2022-06-25 12:51:35.445 | INFO | No update 2022-06-25 12:51:40.473 | INFO | >>>> _chec_update, cnt:7 2022-06-25 12:51:40.498 | INFO | New update avaliable 2022-06-25 12:51:40.512 | INFO | Waiting all running alas finish. 2022-06-25 12:51:40.524 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 1] >>> 2022-06-25 12:51:40.543 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:51:40.577 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 1] >>> 2022-06-25 12:51:40.603 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:40.617 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:51:40.648 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:45.663 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:45.690 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:51:45.710 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:50.737 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:50.762 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:51:50.792 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:55.799 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:51:55.826 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:51:55.860 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:00.868 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:00.886 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:00.915 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:05.940 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:05.957 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:05.983 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:11.001 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:11.023 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:11.047 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:16.081 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:16.104 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:16.133 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:21.156 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:21.174 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:21.210 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:26.240 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:26.258 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:26.284 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:31.305 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:31.318 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:31.348 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:36.366 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:36.383 | INFO | process[] [alas_硫磺岛] -> [True] 2022-06-25 12:52:36.407 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:39.124 | INFO | End of log queue handler loop 2022-06-25 12:52:39.147 | INFO | [alas_硫磺岛] exited 2022-06-25 12:52:41.434 | INFO | Alas [alas_硫磺岛] stopped 2022-06-25 12:52:41.453 | INFO | Remains: [] 2022-06-25 12:52:41.469 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:41.481 | INFO | process[] [alas_硫磺岛] -> [False] 2022-06-25 12:52:41.513 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>> 2022-06-25 12:52:46.533 | INFO | All alas stopped, start updating 2022-06-25 12:52:46.545 | INFO | <<< RUN UPDATE >>> 2022-06-25 12:52:46.571 | INFO | <<< MOCK UPDATE >>> 2022-06-25 12:52:46.584 | INFO | Start clearup 2022-06-25 12:52:46.614 | INFO | [alas_硫磺岛] exited ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:52:52.111 | INFO | <<< WEBUI CONFIGS >>> 2022-06-25 12:52:52.135 | INFO | [Theme] default 2022-06-25 12:52:52.152 | INFO | [Language] zh-CN 2022-06-25 12:52:52.172 | INFO | [Password] False 2022-06-25 12:52:52.181 | INFO | [CDN] False 2022-06-25 12:52:52.207 | INFO | [Electron] False 2022-06-25 12:52:52.222 | INFO | <<< APP.PY STARTUP >>> 2022-06-25 12:52:52.245 | INFO | app.py startup ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:52:53.398 | INFO | Add task 2022-06-25 12:52:53.411 | INFO | Start task handler 2022-06-25 12:52:53.436 | INFO | <<< RESTART ALAS >>> 2022-06-25 12:52:53.483 | INFO | Starting [alas_硫磺岛] 2022-06-25 12:52:53.510 | INFO | Start alas complete ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ START ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ 2022-06-25 12:52:55.221 | INFO | [Server] cn 2022-06-25 12:52:55.298 | INFO | Using template config, which is read only 2022-06-25 12:52:55.501 | INFO | Add task 2022-06-25 12:52:55.565 | INFO | Add task 2022-06-25 12:52:55.595 | INFO | Add task 2022-06-25 12:52:55.647 | INFO | Start task handler 2022-06-25 12:52:55.705 | INFO | [Server] cn 2022-06-25 12:52:55.768 | INFO | Bind task {'', 'General', 'Alas'} 2022-06-25 12:52:56.421 | INFO | Add task 2022-06-25 12:52:56.460 | INFO | Add task 2022-06-25 12:52:56.493 | INFO | Add task 2022-06-25 12:52:56.550 | INFO | Add task 2022-06-25 12:52:58.457 | INFO | >>>> _chec_update, cnt:1 2022-06-25 12:52:58.471 | INFO | No update 2022-06-25 12:53:03.479 | INFO | >>>> _chec_update, cnt:2 2022-06-25 12:53:03.507 | INFO | No update 2022-06-25 12:53:08.524 | INFO | >>>> _chec_update, cnt:3 2022-06-25 12:53:08.549 | INFO | No update 2022-06-25 12:53:13.597 | INFO | >>>> _chec_update, cnt:4 2022-06-25 12:53:13.615 | INFO | No update 2022-06-25 12:53:14.107 | INFO | Task removed. 2022-06-25 12:53:14.151 | INFO | Task removed. 2022-06-25 12:53:14.174 | INFO | Task removed. 2022-06-25 12:53:14.209 | INFO | Task removed. 2022-06-25 12:53:14.287 | INFO | End of task handler loop 2022-06-25 12:53:14.307 | INFO | Finish task handler 2022-06-25 12:53:14.327 | INFO | Start clearup 2022-06-25 12:53:14.740 | INFO | [alas_硫磺岛] exited 2022-06-25 12:53:14.759 | INFO | End of task handler loop 2022-06-25 12:53:14.783 | INFO | Finish task handler 2022-06-25 12:53:14.797 | INFO | Alas closed. ```

简要分析

// gui.txt          
2022-06-25 12:52:05.940 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>>                                           
2022-06-25 12:52:05.957 | INFO | process[<module.webui.process_manager.ProcessManager object at 0x000000000C206D88>]   
[alas_硫磺岛] -> [True]                                                                                                
2022-06-25 12:52:05.983 | INFO | <<< SHOW_PROCESSES, TAG[_WAIT_UPDATE 2] >>>   
18870 commented 2 years ago

试试这个

from multiprocessing import Process
import os
import time

def sysexit():
    print("Run sysexit function")
    time.sleep(3)
    print("Call exit")
    exit(0)
    print("sysexit finish")

def osexit():
    print("Run osexit function")
    time.sleep(3)
    print("Call exit")
    os._exit(0)
    print("osexit finish")

def run(p: Process):
    p.start()
    for _ in range(5):
        time.sleep(1)
        print(p.is_alive())
    print("Kill process")
    p.kill()
    time.sleep(1)
    print(p.is_alive())

if __name__ == "__main__":
    ps = Process(target=sysexit)
    po = Process(target=osexit)

    run(ps)
    run(po)
yzbyzz commented 2 years ago
Run sysexit function
True
True
True
Call exit
False
False
Kill process
False
Run osexit function
True
True
True
Call exit
False
False
Kill process
False

项目中两个停止进程的路径,除了使用的方法不同,两者对应的脚本也是不同的。

yzbyzz commented 2 years ago

看到有两种停止的方式

https://github.com/LmeSzinc/AzurLaneAutoScript/blob/7bfff9019ea1740e93cb20ba34b7958230508b2b/alas.py#L322-L343

https://github.com/LmeSzinc/AzurLaneAutoScript/blob/7bfff9019ea1740e93cb20ba34b7958230508b2b/module/webui/app.py#L326-L335

这里可能应该统一一下退出的方式?

18870 commented 2 years ago

发现更新的时候需要等alas完成任务自己退出,否则可能会卡在某个页面无法再启动。 正常情况下都能自己退出,没有必要跨进程通信让父进程把子进程杀了

你这个我确实想不到什么情况了,摆了

yzbyzz commented 2 years ago

我先按照上面提到的修改配置文件来禁止自动更新吧。 目前看来可能遇到这个问题的人不多,先暂时这样吧。 后面再看看能不能找到原因。

LostMyMind commented 2 years ago

我的Windows7也同样遇到了此问题。一开始还以为是网络波动导致的。