Luigi commited on
Commit
6ff484e
·
1 Parent(s): fcb2aaa

Fix GeneratorExit handling by re-raising after UI reset

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import time
3
  import gc
 
4
  import threading
5
  from itertools import islice
6
  from datetime import datetime
@@ -669,14 +670,13 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
669
  except GeneratorExit:
670
  # Handle Gradio's cancellation signal gracefully
671
  print("Generation cancelled by user.")
672
- # Immediately reset UI state before returning
673
  yield {
674
  txt: gr.update(interactive=True),
675
  submit_btn: gr.update(interactive=True),
676
  cancel_btn: gr.update(visible=False),
677
  }
678
- ui_reset_done = True
679
- return
680
  except Exception as e:
681
  print(f"An error occurred during generation: {e}")
682
  # If an error happens, add it to the chat history to inform the user.
@@ -687,8 +687,8 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
687
  yield {chat: error_history}
688
  finally:
689
  # 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.
690
- # Only do this if we haven't already reset in the except block
691
- if not ui_reset_done:
692
  print("Resetting UI state.")
693
  yield {
694
  txt: gr.update(interactive=True),
 
1
  import os
2
  import time
3
  import gc
4
+ import sys
5
  import threading
6
  from itertools import islice
7
  from datetime import datetime
 
670
  except GeneratorExit:
671
  # Handle Gradio's cancellation signal gracefully
672
  print("Generation cancelled by user.")
673
+ # Immediately reset UI state, then re-raise to properly close generator
674
  yield {
675
  txt: gr.update(interactive=True),
676
  submit_btn: gr.update(interactive=True),
677
  cancel_btn: gr.update(visible=False),
678
  }
679
+ raise # Re-raise GeneratorExit to properly close the generator
 
680
  except Exception as e:
681
  print(f"An error occurred during generation: {e}")
682
  # If an error happens, add it to the chat history to inform the user.
 
687
  yield {chat: error_history}
688
  finally:
689
  # 3. ALWAYS reset the UI to an "idle" state upon completion, error, or cancellation.
690
+ # Skip if GeneratorExit was raised (already handled above)
691
+ if not isinstance(sys.exc_info()[1], GeneratorExit):
692
  print("Resetting UI state.")
693
  yield {
694
  txt: gr.update(interactive=True),