Skip to content

Commit 6f419b5

Browse files
committed
Fix timeout handling in request--curl-sync
1 parent 01e338c commit 6f419b5

1 file changed

Lines changed: 47 additions & 26 deletions

File tree

request.el

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,33 +1153,54 @@ See info entries on sentinels regarding PROC and EVENT."
11531153
(or error (and (numberp code) (>= code 400) `(error . (http ,code)))))
11541154
(apply #'request--callback buffer settings))))))
11551155

1156-
(cl-defun request--curl-sync (url &rest settings &key response &allow-other-keys)
1156+
(cl-defun request--curl-sync
1157+
(url &rest settings &key response &allow-other-keys)
11571158
"Internal synchronous curl call to URL with SETTINGS bespeaking RESPONSE."
1158-
(let (finished)
1159-
(prog1 (apply #'request--curl url
1160-
:semaphore (lambda (&rest _) (setq finished t))
1161-
settings)
1162-
(cl-loop with buf = (request-response--buffer response)
1163-
with interval = 0.05
1164-
with timeout = 5
1165-
with maxiter = (truncate (/ timeout interval))
1166-
with iter = 0
1167-
until (or (>= iter maxiter) finished)
1168-
do (accept-process-output nil interval)
1169-
for proc = (get-buffer-process buf)
1170-
if (or (not proc) (not (process-live-p proc)))
1171-
;; only run the clock if lollygagging
1172-
;; (before or after process lifetime)
1173-
do (cl-incf iter)
1174-
end
1175-
finally (when (>= iter maxiter)
1176-
(let ((m "request--curl-sync: semaphore never called"))
1177-
(princ (format "%s %S %s\n"
1178-
m
1179-
buf
1180-
(buffer-live-p buf))
1181-
#'external-debugging-output)
1182-
(request-log 'error m)))))))
1159+
(let
1160+
((finished nil))
1161+
(prog1
1162+
(apply #'request--curl url
1163+
:semaphore
1164+
(lambda
1165+
(&rest _)
1166+
(setq finished t))
1167+
settings)
1168+
(let*
1169+
((buffer
1170+
(request-response--buffer response))
1171+
(interval 0.05)
1172+
(timeout
1173+
(or
1174+
(plist-get settings :timeout)
1175+
30))
1176+
(time-spent 0))
1177+
(while
1178+
(and
1179+
(not finished)
1180+
(< time-spent timeout)
1181+
(buffer-live-p buffer)
1182+
(get-buffer-process buffer))
1183+
(accept-process-output nil interval)
1184+
(setq time-spent
1185+
(+ time-spent interval)))
1186+
1187+
(when
1188+
(and
1189+
(not finished)
1190+
(>= time-spent timeout))
1191+
(setf
1192+
(request-response-error-thrown response)
1193+
(cons 'error "Timeout"))
1194+
(setf
1195+
(request-response-symbol-status response)
1196+
'timeout)
1197+
(when
1198+
(and
1199+
(buffer-live-p buffer)
1200+
(get-buffer-process buffer))
1201+
(funcall
1202+
(request--choose-backend 'terminate-process)
1203+
(get-buffer-process buffer))))))))
11831204

11841205
(defun request--curl-get-cookies (host localpart secure)
11851206
"Return entry for HOST LOCALPART SECURE in cookie jar."

0 commit comments

Comments
 (0)