Hemil Ghori commited on
Commit
d9e83ea
·
1 Parent(s): c0bbf3c

Graceful asyncio cleanup and prevent_thread_lock for Gradio launch

Browse files
Files changed (1) hide show
  1. app.py +20 -3
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
  import threading
 
 
3
  from typing import Optional
4
 
5
  import gradio as gr
@@ -137,6 +139,21 @@ def build_ui():
137
 
138
  if __name__ == "__main__":
139
  demo = build_ui()
140
- # Disable SSR to avoid asyncio selector shutdown errors in some container runtimes
141
- # Use 0.0.0.0 so HF Spaces can bind correctly
142
- demo.launch(server_name="0.0.0.0", share=False, ssr_mode=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import threading
3
+ import atexit
4
+ import asyncio
5
  from typing import Optional
6
 
7
  import gradio as gr
 
139
 
140
  if __name__ == "__main__":
141
  demo = build_ui()
142
+
143
+ # Register cleanup to close asyncio loop at process exit. This suppresses the
144
+ # "Invalid file descriptor" errors raised during interpreter shutdown in
145
+ # some container runtimes.
146
+ def _close_asyncio_loop():
147
+ try:
148
+ loop = asyncio.get_event_loop()
149
+ if loop.is_running():
150
+ loop.stop()
151
+ if not loop.is_closed():
152
+ loop.close()
153
+ except Exception:
154
+ pass
155
+
156
+ atexit.register(_close_asyncio_loop)
157
+
158
+ # Disable SSR and prevent thread lock to avoid asyncio/threading shutdown issues
159
+ demo.launch(server_name="0.0.0.0", share=False, ssr_mode=False, prevent_thread_lock=True)