Skip to content

Commit 8925a35

Browse files
authored
Merge pull request #762 from Routineco/bugfix/lwt-wakeup-exn-invalid-arg
Fix Invalid_arg exception on stack overflow when waking up a request.
2 parents 57716f8 + 023deb8 commit 8925a35

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## current
2+
3+
- lwt_jsoo: Fix `Lwt.wakeup_exn` `Invalid_arg` exception when a js
4+
stack overflow happens in the XHR completion handler (@mefyl #762).
5+
16
## v4.0.0 (2021-03-24)
27

38
- cohttp.response: fix malformed status header for custom status codes (@mseri @aalekseyev #752)

cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,20 @@ module Make_client_async (P : Params) = Make_api (struct
231231
* Remove the type constraint on Lwt.task above and return any old
232232
* guff here. It'll compile and crash in the browser! *)
233233
Lwt.wakeup wake (response, body)
234-
with e -> Lwt.wakeup_exn wake e)
234+
with
235+
| e
236+
(* If we exhaust the stack, it is possible that
237+
Lwt.wakeup just aboves marks the promise as
238+
completed, but raises Stack_overflow while
239+
running the promise callbacks. In this case
240+
waking calling wakeup_exn on the already
241+
completed promise would raise an Invalid_arg
242+
exception, so although the promise is in a
243+
really bad state we may as well let the actual
244+
Stack_overflow exception go through. *)
245+
when Lwt.state res = Lwt.Sleep
246+
->
247+
Lwt.wakeup_exn wake e)
235248
| _ -> ());
236249

237250
(* perform call *)

0 commit comments

Comments
 (0)