Commit 1681e855 authored by Maxim Ivanov's avatar Maxim Ivanov
Browse files

Return Lua VM to the pool only after it's error was processed

lua.ApiError contains references to the Lua values,
it is likely unsafe to return VM error originated from
to the pool and then accessing that error.

Returned VM might be snatched by another goroutine, resulting in the
unwanted concurrent access.
parent 73457e01
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -1265,7 +1265,6 @@ func (rp *RuntimeProviderLua) Rpc(ctx context.Context, id string, headers, query
	r.vm.SetContext(vmCtx)
	result, fnErr, code, isCustomErr := r.InvokeFunction(RuntimeExecutionModeRPC, lf, headers, queryParams, userID, username, vars, expiry, sessionID, clientIP, clientPort, lang, payload)
	r.vm.SetContext(context.Background())
	rp.Put(r)

	if fnErr != nil {
		if !isCustomErr {
@@ -1279,8 +1278,11 @@ func (rp *RuntimeProviderLua) Rpc(ctx context.Context, id string, headers, query
			code = 13
		}

		return "", clearFnError(fnErr, rp, lf), code
		err = clearFnError(fnErr, rp, lf)
		rp.Put(r) // don't return VM until error originated in that VM is processed
		return "", err, code
	}
	rp.Put(r)

	if result == nil {
		return "", nil, 0
@@ -2092,7 +2094,11 @@ func clearFnError(fnErr error, rp *RuntimeProviderLua, lf *lua.LFunction) error
		}
		return errors.New(msg)
	}
	return fnErr

	// fnErr contains reference to the LuaVM we are about to return to pool,
	// create new error with same error message, but dropping any references
	// to the Lua VM objects
	return errors.New(fnErr.Error())
}

func checkRuntimeLuaVM(logger *zap.Logger, config Config, version string, stdLibs map[string]lua.LGFunction, moduleCache *RuntimeLuaModuleCache) error {