From 3162499891808daaaa88a4c66b6456b1093d60e9 Mon Sep 17 00:00:00 2001 From: mefyl Date: Sun, 28 Mar 2021 12:39:28 +0200 Subject: [PATCH 1/2] Fix Invalid_arg exception on stack overflow when waking a request. --- cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml b/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml index 7566f39f9b..0cc0719660 100644 --- a/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml +++ b/cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml @@ -231,7 +231,20 @@ module Make_client_async (P : Params) = Make_api (struct * Remove the type constraint on Lwt.task above and return any old * guff here. It'll compile and crash in the browser! *) Lwt.wakeup wake (response, body) - with e -> Lwt.wakeup_exn wake e) + with + | e + (* If we exhaust the stack, it is possible that + Lwt.wakeup just aboves marks the promise as + completed, but raises Stack_overflow while + running the promise callbacks. In this case + waking calling wakeup_exn on the already + completed promise would raise an Invalid_arg + exception, so although the promise is in a + really bad state we may as well let the actual + Stack_overflow exception go through. *) + when Lwt.state res = Lwt.Sleep + -> + Lwt.wakeup_exn wake e) | _ -> ()); (* perform call *) From 023deb8530e00d36d47d6104f4099b82a874ba47 Mon Sep 17 00:00:00 2001 From: mefyl Date: Thu, 1 Apr 2021 14:01:36 +0200 Subject: [PATCH 2/2] Update CHANGES.md. --- CHANGES.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 987cff4e10..335762824a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,8 @@ +## current + +- lwt_jsoo: Fix `Lwt.wakeup_exn` `Invalid_arg` exception when a js + stack overflow happens in the XHR completion handler (@mefyl #762). + ## v4.0.0 (2021-03-24) - cohttp.response: fix malformed status header for custom status codes (@mseri @aalekseyev #752)