Spaces:
Sleeping
Sleeping
Fix GeneratorExit handling to prevent runtime error
Browse files- Catch GeneratorExit and re-raise it to satisfy Python's generator protocol
- Track cancellation state to prevent yielding in finally block after GeneratorExit
- Prevents 'generator ignored GeneratorExit' RuntimeError
- Ensures proper cleanup without violating generator contract
app.py
CHANGED
|
@@ -657,6 +657,7 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 657 |
cancel_btn: gr.update(visible=True),
|
| 658 |
}
|
| 659 |
|
|
|
|
| 660 |
try:
|
| 661 |
# 2. Call the backend and stream updates
|
| 662 |
backend_args = [user_msg, chat_history] + list(args)
|
|
@@ -665,6 +666,11 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 665 |
chat: response_chunk[0],
|
| 666 |
dbg: response_chunk[1],
|
| 667 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
except Exception as e:
|
| 669 |
print(f"An error occurred during generation: {e}")
|
| 670 |
# If an error happens, add it to the chat history to inform the user.
|
|
@@ -674,13 +680,14 @@ with gr.Blocks(title="LLM Inference with ZeroGPU") as demo:
|
|
| 674 |
]
|
| 675 |
yield {chat: error_history}
|
| 676 |
finally:
|
| 677 |
-
#
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
|
|
|
| 684 |
|
| 685 |
def set_cancel_flag():
|
| 686 |
"""Called by the cancel button, sets the global event."""
|
|
|
|
| 657 |
cancel_btn: gr.update(visible=True),
|
| 658 |
}
|
| 659 |
|
| 660 |
+
cancelled = False
|
| 661 |
try:
|
| 662 |
# 2. Call the backend and stream updates
|
| 663 |
backend_args = [user_msg, chat_history] + list(args)
|
|
|
|
| 666 |
chat: response_chunk[0],
|
| 667 |
dbg: response_chunk[1],
|
| 668 |
}
|
| 669 |
+
except GeneratorExit:
|
| 670 |
+
# Mark as cancelled and re-raise to prevent "generator ignored GeneratorExit"
|
| 671 |
+
cancelled = True
|
| 672 |
+
print("Generation cancelled by user.")
|
| 673 |
+
raise
|
| 674 |
except Exception as e:
|
| 675 |
print(f"An error occurred during generation: {e}")
|
| 676 |
# If an error happens, add it to the chat history to inform the user.
|
|
|
|
| 680 |
]
|
| 681 |
yield {chat: error_history}
|
| 682 |
finally:
|
| 683 |
+
# Only reset UI if not cancelled (to avoid "generator ignored GeneratorExit")
|
| 684 |
+
if not cancelled:
|
| 685 |
+
print("Resetting UI state.")
|
| 686 |
+
yield {
|
| 687 |
+
txt: gr.update(interactive=True),
|
| 688 |
+
submit_btn: gr.update(interactive=True),
|
| 689 |
+
cancel_btn: gr.update(visible=False),
|
| 690 |
+
}
|
| 691 |
|
| 692 |
def set_cancel_flag():
|
| 693 |
"""Called by the cancel button, sets the global event."""
|