Spaces:
Sleeping
Sleeping
hf-actions
commited on
Commit
·
8216f24
1
Parent(s):
44c4a8a
fix: retry DNS resolution in validate_tokens() with backoff (FB_DNS_RETRY_ATTEMPTS)
Browse files
app.py
CHANGED
|
@@ -154,13 +154,29 @@ if __name__ == "__main__":
|
|
| 154 |
ok = False
|
| 155 |
else:
|
| 156 |
# quick DNS resolution check to avoid noisy stack traces when DNS fails
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
try:
|
| 162 |
with open("log.txt", "a", encoding="utf-8") as lf:
|
| 163 |
-
lf.write(f"[{__import__('time').strftime('%Y-%m-%d %H:%M:%S')}] FB_DNS_RESOLUTION_FAILED {
|
| 164 |
except Exception:
|
| 165 |
logger.exception("Failed to write DNS failure to log.txt")
|
| 166 |
ok = False
|
|
|
|
| 154 |
ok = False
|
| 155 |
else:
|
| 156 |
# quick DNS resolution check to avoid noisy stack traces when DNS fails
|
| 157 |
+
# Retry DNS resolution a few times with exponential backoff to handle transient name-resolution blips
|
| 158 |
+
dns_attempts = int(os.getenv("FB_DNS_RETRY_ATTEMPTS", "3"))
|
| 159 |
+
dns_backoff_base = int(os.getenv("FB_DNS_BACKOFF_BASE", "1"))
|
| 160 |
+
dns_ok = False
|
| 161 |
+
last_dns_exc = None
|
| 162 |
+
for attempt in range(1, dns_attempts + 1):
|
| 163 |
+
try:
|
| 164 |
+
socket.getaddrinfo("graph.facebook.com", 443)
|
| 165 |
+
dns_ok = True
|
| 166 |
+
break
|
| 167 |
+
except Exception as e:
|
| 168 |
+
last_dns_exc = e
|
| 169 |
+
logger.warning("DNS resolution attempt %s/%s failed: %s", attempt, dns_attempts, e)
|
| 170 |
+
if attempt < dns_attempts:
|
| 171 |
+
try:
|
| 172 |
+
time.sleep(dns_backoff_base * (2 ** (attempt - 1)))
|
| 173 |
+
except Exception:
|
| 174 |
+
pass
|
| 175 |
+
if not dns_ok:
|
| 176 |
+
logger.error("DNS resolution failed for graph.facebook.com after %s attempts: %s", dns_attempts, last_dns_exc)
|
| 177 |
try:
|
| 178 |
with open("log.txt", "a", encoding="utf-8") as lf:
|
| 179 |
+
lf.write(f"[{__import__('time').strftime('%Y-%m-%d %H:%M:%S')}] FB_DNS_RESOLUTION_FAILED attempts={dns_attempts} error={last_dns_exc}\n")
|
| 180 |
except Exception:
|
| 181 |
logger.exception("Failed to write DNS failure to log.txt")
|
| 182 |
ok = False
|