From f966160ceadf3d0c8c42327cf4da5fc1c7fb0742 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Thu, 7 Dec 2017 16:14:51 +0200 Subject: [PATCH] Fix JSVM race --- mw_js_plugin.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mw_js_plugin.go b/mw_js_plugin.go index e14079f8c62f..f9d13d227666 100644 --- a/mw_js_plugin.go +++ b/mw_js_plugin.go @@ -13,6 +13,7 @@ import ( "path/filepath" "strings" "time" + "sync" "github.com/robertkrimen/otto" _ "github.com/robertkrimen/otto/underscore" @@ -280,6 +281,7 @@ func mapStrsToIfaces(m map[string]string) map[string]interface{} { // --- Utility functions during startup to ensure a sane VM is present for each API Def ---- type JSVM struct { + sync.Mutex Spec *APISpec VM *otto.Otto Timeout time.Duration @@ -290,6 +292,9 @@ type JSVM struct { // Init creates the JSVM with the core library and sets up a default // timeout. func (j *JSVM) Init(spec *APISpec) { + j.Lock() + defer j.Unlock() + vm := otto.New() // Init TykJS namespace, constructors etc.