]> git.kaiwu.me - njs.git/commitdiff
Modules: fix top-level await handling in QuickJS
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 30 Oct 2026 01:46:09 +0000 (18:46 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 2 Nov 2026 14:42:35 +0000 (06:42 -0800)
Previously, the result of module evaluation was awaited, but the
outcome was not checked:

  - a rejected top-level await was silently ignored;
  - a top-level await on a promise settled only by a timer or another
    asynchronous event remained pending.

In both cases the module was considered loaded without its exports
and each request failed later with a misleading "js function not
found" error.

Now, both cases fail the context creation with an explicit error.

The issue was introduced in 9a5430e6 (0.9.5).

nginx/ngx_js.c

index b7d04a5d4a4db4b1555d30d525d722c9c010182d..5167a6d9b79b0159f8560acd13294f0dd2bdc728 100644 (file)
@@ -1202,12 +1202,21 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
     }
 
     rc = ngx_qjs_await(cx, ctx->log, &rv);
-    JS_FreeValue(cx, rv);
-    if (rc == NGX_ERROR) {
+    if (rc == NGX_ERROR || JS_IsException(rv)) {
+        JS_FreeValue(cx, rv);
         ngx_qjs_log_exception(engine, ctx->log, "await exception");
         goto failed;
     }
 
+    if (JS_PromiseState(cx, rv) == JS_PROMISE_PENDING) {
+        JS_FreeValue(cx, rv);
+        ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
+                      "js top-level await is not settled");
+        goto failed;
+    }
+
+    JS_FreeValue(cx, rv);
+
     return engine;
 
 failed: