[njs] Fixed sharing of externals between main and cloned VMs.

Dmitry Volyntsev xeioex at nginx.com
Mon Nov 11 12:37:56 UTC 2019


details:   https://hg.nginx.org/njs/rev/984520294a3c
branches:  
changeset: 1229:984520294a3c
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Nov 11 15:37:08 2019 +0300
description:
Fixed sharing of externals between main and cloned VMs.

The issue was introduced in 7e7d0dac4572.

This closes #245 issue on Github.

diffstat:

 src/njs_extern.c         |  17 ++++++++++++++---
 src/test/njs_unit_test.c |   3 +++
 2 files changed, 17 insertions(+), 3 deletions(-)

diffs (53 lines):

diff -r 71decc0d345c -r 984520294a3c src/njs_extern.c
--- a/src/njs_extern.c	Fri Nov 08 20:33:46 2019 +0300
+++ b/src/njs_extern.c	Mon Nov 11 15:37:08 2019 +0300
@@ -172,22 +172,33 @@ njs_vm_external_create(njs_vm_t *vm, njs
     const njs_extern_t *proto, njs_external_ptr_t object)
 {
     void       *obj;
+    uint32_t   n;
     njs_arr_t  *externals;
 
     if (njs_slow_path(proto == NULL)) {
         return NJS_ERROR;
     }
 
-    if (njs_slow_path(vm->external_objects == NULL)) {
-        externals = njs_arr_create(vm->mem_pool, 4, sizeof(void *));
+    if (vm->external_objects->mem_pool != vm->mem_pool) {
+
+        /* Making a local copy of externals in shared VM. */
+
+        n = vm->external_objects->items;
+
+        externals = njs_arr_create(vm->mem_pool, n + 4, sizeof(void *));
         if (njs_slow_path(externals == NULL)) {
             return NJS_ERROR;
         }
 
+        if (n > 0) {
+            memcpy(externals->start, vm->external_objects->start,
+                   n * sizeof(void *));
+            externals->items = n;
+        }
+
         vm->external_objects = externals;
     }
 
-
     obj = njs_arr_add(vm->external_objects);
     if (njs_slow_path(obj == NULL)) {
         return NJS_ERROR;
diff -r 71decc0d345c -r 984520294a3c src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Nov 08 20:33:46 2019 +0300
+++ b/src/test/njs_unit_test.c	Mon Nov 11 15:37:08 2019 +0300
@@ -14694,6 +14694,9 @@ static njs_unit_test_t  njs_shared_test[
 
     { njs_str("isNaN(function(){})"),
       njs_str("true") },
+
+    { njs_str("var r; for (var i = 0; i < 2**10; i++) {r = $r.create('XXX').uri;}"),
+      njs_str("undefined") },
 };
 
 


More information about the nginx-devel mailing list