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.