[njs] Fixed unhandled promise rejection in handle events.

Alexander Borisov alexander.borisov at nginx.com
Fri Oct 8 09:33:46 UTC 2021


details:   https://hg.nginx.org/njs/rev/b5d102eb81c1
branches:  
changeset: 1714:b5d102eb81c1
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Fri Oct 08 12:32:42 2021 +0300
description:
Fixed unhandled promise rejection in handle events.

This closes #423 issue on GitHub.

diffstat:

 src/njs.h                            |   3 +++
 src/njs_shell.c                      |   2 +-
 src/njs_vm.c                         |  26 +++++++++++---------------
 test/js/promise_rejection_tracker.js |   1 +
 test/njs_expect_test.exp             |   4 ++++
 5 files changed, 20 insertions(+), 16 deletions(-)

diffs (84 lines):

diff -r 5aceb5eaf2b2 -r b5d102eb81c1 src/njs.h
--- a/src/njs.h	Wed Oct 06 13:16:09 2021 +0000
+++ b/src/njs.h	Fri Oct 08 12:32:42 2021 +0300
@@ -265,6 +265,9 @@ NJS_EXPORT njs_int_t njs_vm_posted(njs_v
 
 #define njs_vm_pending(vm)  (njs_vm_waiting(vm) || njs_vm_posted(vm))
 
+#define njs_vm_unhandled_rejection(vm)                                         \
+    ((vm)->options.unhandled_rejection == NJS_VM_OPT_UNHANDLED_REJECTION_THROW \
+    && (vm)->promise_reason != NULL && (vm)->promise_reason->length != 0)
 
 /*
  * Runs the specified function with provided arguments.
diff -r 5aceb5eaf2b2 -r b5d102eb81c1 src/njs_shell.c
--- a/src/njs_shell.c	Wed Oct 06 13:16:09 2021 +0000
+++ b/src/njs_shell.c	Fri Oct 08 12:32:42 2021 +0300
@@ -868,7 +868,7 @@ njs_process_script(njs_opts_t *opts, njs
     }
 
     for ( ;; ) {
-        if (!njs_vm_pending(vm)) {
+        if (!njs_vm_pending(vm) && !njs_vm_unhandled_rejection(vm)) {
             break;
         }
 
diff -r 5aceb5eaf2b2 -r b5d102eb81c1 src/njs_vm.c
--- a/src/njs_vm.c	Wed Oct 06 13:16:09 2021 +0000
+++ b/src/njs_vm.c	Fri Oct 08 12:32:42 2021 +0300
@@ -504,24 +504,20 @@ njs_vm_handle_events(njs_vm_t *vm)
             }
         }
 
-        if (vm->options.unhandled_rejection
-            == NJS_VM_OPT_UNHANDLED_REJECTION_THROW)
-        {
-            if (vm->promise_reason != NULL && vm->promise_reason->length != 0) {
-                ret = njs_value_to_string(vm, &string,
-                                          &vm->promise_reason->start[0]);
-                if (njs_slow_path(ret != NJS_OK)) {
-                    return ret;
-                }
+        if (njs_vm_unhandled_rejection(vm)) {
+            ret = njs_value_to_string(vm, &string,
+                                      &vm->promise_reason->start[0]);
+            if (njs_slow_path(ret != NJS_OK)) {
+                return ret;
+            }
 
-                njs_string_get(&string, &str);
-                njs_vm_error(vm, "unhandled promise rejection: %V", &str);
+            njs_string_get(&string, &str);
+            njs_vm_error(vm, "unhandled promise rejection: %V", &str);
 
-                njs_mp_free(vm->mem_pool, vm->promise_reason);
-                vm->promise_reason = NULL;
+            njs_mp_free(vm->mem_pool, vm->promise_reason);
+            vm->promise_reason = NULL;
 
-                return NJS_ERROR;
-            }
+            return NJS_ERROR;
         }
 
         for ( ;; ) {
diff -r 5aceb5eaf2b2 -r b5d102eb81c1 test/js/promise_rejection_tracker.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/promise_rejection_tracker.js	Fri Oct 08 12:32:42 2021 +0300
@@ -0,0 +1,1 @@
+Promise.reject(1);
\ No newline at end of file
diff -r 5aceb5eaf2b2 -r b5d102eb81c1 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Wed Oct 06 13:16:09 2021 +0000
+++ b/test/njs_expect_test.exp	Fri Oct 08 12:32:42 2021 +0300
@@ -1085,6 +1085,10 @@ njs_run {"./test/js/promise_reject_catch
 njs_run {"./test/js/promise_reject_post_catch.js"} \
 "Error: unhandled promise rejection: undefined"
 
+njs_run {"./test/js/promise_rejection_tracker.js"} \
+"Thrown:
+Error: unhandled promise rejection: 1"
+
 njs_run {"./test/js/promise_all.js"} \
 "resolved:\\\[\\\['one','two'],\\\['three','four']]"
 


More information about the nginx-devel mailing list