@@ -41,10 +41,17 @@ def check_timeouts():
4141 if time .time () - last_active > time_limit :
4242 logging .debug (f"Client { sid } timed out" )
4343 stop_nexttrace_for_sid (sid )
44- del client_last_active [sid ]
4544 time .sleep (1 )
4645
4746
47+ def cleanup_client_state (sid ):
48+ task_removed = clients .pop (sid , None )
49+ if task_removed :
50+ logging .info (f"Client { sid } removed from clients dictionary" )
51+ if client_last_active .pop (sid , None ) is not None :
52+ logging .debug (f"Client { sid } removed from last active tracker" )
53+
54+
4855def stop_nexttrace_for_sid (sid ):
4956 task = clients .get (sid )
5057 if not task :
@@ -64,10 +71,6 @@ def stop_nexttrace_for_sid(sid):
6471 task .process .kill ()
6572 logging .info (f"Process killed forcefully for client { sid } " )
6673 task .emit_complete ()
67- if sid in clients :
68- del clients [sid ]
69- logging .info (f"Client { sid } removed from clients dictionary after process termination" )
70- client_last_active .pop (sid , None )
7174
7275
7376Thread (target = check_timeouts , daemon = True ).start ()
@@ -132,6 +135,11 @@ def emit_complete(self):
132135 if not self ._complete_emitted :
133136 self ._complete_emitted = True
134137 self .socketio .emit ('nexttrace_complete' , room = self .sid )
138+ cleanup_client_state (self .sid )
139+
140+ def emit_error_row (self , message ):
141+ error_payload = json .dumps (['-1' , '' , '' , '0' , 'ERROR' , '' , message ], ensure_ascii = False )
142+ self .socketio .emit ('nexttrace_output' , error_payload , room = self .sid )
135143
136144 def run (self ):
137145 fixParam = '--map --raw -q 1 --send-time 1' # -d disable-geoip
@@ -143,14 +151,20 @@ def run(self):
143151
144152 pattern = re .compile (r'[&;<>\"\'()|\[\]{}$#!%*+=]' )
145153 if pattern .search (self .params ):
146- self .socketio . emit ( 'nexttrace_output' , 'Invalid params' , room = self . sid )
154+ self .emit_error_row ( '参数不合法,任务未启动' )
147155 self .emit_complete ()
148- raise ValueError ( 'Invalid params' )
156+ return
149157 logging .debug (f"cmd: { [self .nexttrace_path ] + self .params .split () + fixParam .split ()} " )
150- self .process = subprocess .Popen (
151- [self .nexttrace_path ] + self .params .split () + fixParam .split (),
152- stdout = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True , env = process_env , bufsize = 1
153- )
158+ try :
159+ self .process = subprocess .Popen (
160+ [self .nexttrace_path ] + self .params .split () + fixParam .split (),
161+ stdout = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True , env = process_env , bufsize = 1
162+ )
163+ except OSError as exc :
164+ logging .error (f"Failed to start nexttrace for client { self .sid } : { exc } " )
165+ self .emit_error_row ('启动 nexttrace 失败,请检查服务器配置' )
166+ self .emit_complete ()
167+ return
154168 output_monitor = OutputMonitor (self .process , self .socketio , self .sid , options )
155169 output_monitor_flag = True
156170
0 commit comments