Spaces:
Running on Zero
Running on Zero
stop gradio orphaning event loops (the Invalid file descriptor tracebacks): safe_get_lock/safe_get_stop_event build a throwaway loop per call just to construct a Lock/Event — measured 9 loops, 7 orphaned per boot on 6.23.1; each orphan raises ValueError -1 in BaseEventLoop.__del__ when its socketpair is freed first. Lock()/Event() bind lazily on py3.10+, so the factories are replaced with plain constructors across every module that binds the names. This REMOVES loop creation: no loop, thread, process or coroutine is created. Measured after: 1 loop, 0 orphans, boot log clean
Browse files
app.py
CHANGED
|
@@ -8,18 +8,42 @@ the end. No framework patching.
|
|
| 8 |
|
| 9 |
Code: https://github.com/AbstractEyes/alephllm
|
| 10 |
"""
|
|
|
|
| 11 |
import json
|
| 12 |
import os
|
| 13 |
import re
|
|
|
|
| 14 |
import time
|
| 15 |
import uuid
|
| 16 |
from datetime import datetime, timezone
|
| 17 |
from pathlib import Path
|
| 18 |
|
| 19 |
import gradio as gr
|
|
|
|
| 20 |
import numpy as np
|
| 21 |
import spaces
|
| 22 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
from huggingface_hub import HfApi, hf_hub_download
|
| 24 |
from safetensors.torch import load_file
|
| 25 |
|
|
|
|
| 8 |
|
| 9 |
Code: https://github.com/AbstractEyes/alephllm
|
| 10 |
"""
|
| 11 |
+
import asyncio
|
| 12 |
import json
|
| 13 |
import os
|
| 14 |
import re
|
| 15 |
+
import sys
|
| 16 |
import time
|
| 17 |
import uuid
|
| 18 |
from datetime import datetime, timezone
|
| 19 |
from pathlib import Path
|
| 20 |
|
| 21 |
import gradio as gr
|
| 22 |
+
import gradio.utils
|
| 23 |
import numpy as np
|
| 24 |
import spaces
|
| 25 |
import torch
|
| 26 |
+
|
| 27 |
+
# gradio.utils.safe_get_lock / safe_get_stop_event call get_running_loop(),
|
| 28 |
+
# which always raises during setup (nothing is running yet), so each call
|
| 29 |
+
# builds a throwaway event loop just to construct a Lock/Event and drops
|
| 30 |
+
# the previous one — measured on 6.23.1: 9 loops created, 7 orphaned per
|
| 31 |
+
# boot. Each orphan produces one
|
| 32 |
+
# ValueError: Invalid file descriptor: -1
|
| 33 |
+
# when the collector finalizes it (the socketpair can be freed before the
|
| 34 |
+
# loop object, so __del__ calls fileno() on a closed socket).
|
| 35 |
+
# On Python 3.10+ Lock()/Event() bind lazily to whichever loop first awaits
|
| 36 |
+
# them, so no loop is needed at construction. This REMOVES loop creation —
|
| 37 |
+
# it starts no loop, thread, process or coroutine of its own. Measured
|
| 38 |
+
# after: 1 loop, 0 orphans. gradio.queueing binds the NAMES rather than the
|
| 39 |
+
# module, so every holder is swept.
|
| 40 |
+
_orig_lock = gradio.utils.safe_get_lock
|
| 41 |
+
_orig_stop = gradio.utils.safe_get_stop_event
|
| 42 |
+
for _mod in list(sys.modules.values()):
|
| 43 |
+
if getattr(_mod, "safe_get_lock", None) is _orig_lock:
|
| 44 |
+
_mod.safe_get_lock = lambda: asyncio.Lock()
|
| 45 |
+
if getattr(_mod, "safe_get_stop_event", None) is _orig_stop:
|
| 46 |
+
_mod.safe_get_stop_event = lambda: asyncio.Event()
|
| 47 |
from huggingface_hub import HfApi, hf_hub_download
|
| 48 |
from safetensors.torch import load_file
|
| 49 |
|