@@ -365,44 +365,53 @@ async def start(self, port: int | None = None):
365
365
assert self ._python_cmd
366
366
367
367
self .state = ServerState .starting
368
- args = ["-su" , "-X" , "utf8" , "main.py" ]
369
- if self .backend is ServerBackend .cpu :
370
- args .append ("--cpu" )
371
- elif self .backend is ServerBackend .directml :
372
- args .append ("--directml" )
373
- elif self .backend is ServerBackend .mps :
374
- args .append ("--force-fp16" )
375
- if settings .server_arguments :
376
- args += settings .server_arguments .split (" " )
377
- if port is not None :
378
- args += ["--port" , str (port )]
379
- self ._process = await asyncio .create_subprocess_exec (
380
- self ._python_cmd ,
381
- * args ,
382
- cwd = self .comfy_dir ,
383
- stdout = asyncio .subprocess .PIPE ,
384
- stderr = asyncio .subprocess .PIPE ,
385
- creationflags = _process_flags ,
386
- )
368
+ try :
369
+ args = ["-su" , "-Xutf8" , "main.py" ]
370
+ if self .backend is ServerBackend .cpu :
371
+ args .append ("--cpu" )
372
+ elif self .backend is ServerBackend .directml :
373
+ args .append ("--directml" )
374
+ elif self .backend is ServerBackend .mps :
375
+ args .append ("--force-fp16" )
376
+ if settings .server_arguments :
377
+ args += settings .server_arguments .split (" " )
378
+ if port is not None :
379
+ args += ["--port" , str (port )]
380
+ self ._process = await asyncio .create_subprocess_exec (
381
+ self ._python_cmd ,
382
+ * args ,
383
+ cwd = self .comfy_dir ,
384
+ stdout = asyncio .subprocess .PIPE ,
385
+ stderr = asyncio .subprocess .PIPE ,
386
+ creationflags = _process_flags ,
387
+ )
387
388
388
- assert self ._process .stdout is not None
389
- async for line in self ._process .stdout :
390
- text = line .decode ("utf-8" ).strip ()
391
- server_log .info (text )
392
- if text .startswith ("To see the GUI go to:" ):
393
- self .state = ServerState .running
394
- self .url = text .split ("http://" )[- 1 ]
395
- break
389
+ assert self ._process .stdout is not None
390
+ async for line in self ._process .stdout :
391
+ text = _decode_utf8_log_error (line ).strip ()
392
+ server_log .info (text )
393
+ if text .startswith ("To see the GUI go to:" ):
394
+ self .state = ServerState .running
395
+ self .url = text .split ("http://" )[- 1 ]
396
+ break
397
+ except Exception as e :
398
+ log .exception (f"Error during server start: { str (e )} " )
399
+ if self ._process is None :
400
+ self .state = ServerState .stopped
401
+ raise e
396
402
397
403
if self .state != ServerState .running :
398
404
error = "Process exited unexpectedly"
399
405
try :
400
406
out , err = await asyncio .wait_for (self ._process .communicate (), timeout = 10 )
401
- server_log .info (out . decode ( "utf-8" ).strip ())
402
- error = err . decode ( "utf-8" )
407
+ server_log .info (_decode_utf8_log_error ( out ).strip ())
408
+ error = _decode_utf8_log_error ( err )
403
409
server_log .error (error )
404
410
except asyncio .TimeoutError :
405
411
self ._process .kill ()
412
+ except Exception as e :
413
+ log .exception (f"Error while waiting for process: { str (e )} " )
414
+ error = str (e )
406
415
407
416
self .state = ServerState .stopped
408
417
ret = self ._process .returncode
@@ -419,7 +428,7 @@ async def run(self):
419
428
420
429
async def forward (stream : asyncio .StreamReader ):
421
430
async for line in stream :
422
- server_log .info (line . decode ( ).strip ())
431
+ server_log .info (_decode_utf8_log_error ( line ).strip ())
423
432
424
433
try :
425
434
await asyncio .gather (
@@ -572,6 +581,15 @@ def _prepend_file(path: Path, line: str):
572
581
file .truncate ()
573
582
574
583
584
+ def _decode_utf8_log_error (b : bytes ):
585
+ try :
586
+ return b .decode ("utf-8" )
587
+ except UnicodeDecodeError as e :
588
+ result = b .decode ("utf-8" , errors = "replace" )
589
+ log .warning (f"Failed to UTF-8 decode: '{ result } ': { str (e )} " )
590
+ return result
591
+
592
+
575
593
def rename_extracted_folder (name : str , path : Path , suffix : str ):
576
594
if path .exists () and path .is_dir () and not any (path .iterdir ()):
577
595
path .rmdir ()
0 commit comments