Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a timeout strategy to fix the dos in luavm #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pkg/resourceinterpreter/customized/declarative/luavm/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,30 @@
return l, err
}

// RunScript got a lua vm from pool, and execute script with given arguments.
func (vm *VM) RunScript(script string, fnName string, nRets int, args ...interface{}) ([]lua.LValue, error) {

Check failure on line 73 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method VM.RunScript should have comment or be unexported (revive)

Check failure on line 73 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method VM.RunScript should have comment or be unexported (revive)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
var rets []lua.LValue
var err error
var complete bool
go func() {
defer cancel()
rets, err = vm.RunScriptWithContext(ctx, script, fnName, nRets, args...)
complete = true
}()
for {
select {
case <-ctx.Done():
if complete {
return rets, err
}
return nil, fmt.Errorf("the context timeout")
default:

Check failure on line 90 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

SA5004: should not have an empty default case in a for+select loop; the loop will spin (staticcheck)

Check failure on line 90 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

SA5004: should not have an empty default case in a for+select loop; the loop will spin (staticcheck)
}
}

}

Check failure on line 94 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

Check failure on line 94 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

func (vm *VM) RunScriptWithContext(ctx context.Context, script string, fnName string, nRets int, args ...interface{}) ([]lua.LValue, error) {

Check failure on line 96 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method VM.RunScriptWithContext should have comment or be unexported (revive)

Check failure on line 96 in pkg/resourceinterpreter/customized/declarative/luavm/lua.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method VM.RunScriptWithContext should have comment or be unexported (revive)
a, err := vm.Pool.Get()
if err != nil {
return nil, err
Expand All @@ -81,8 +103,6 @@
l := a.(*lua.LState)
l.Pop(l.GetTop())

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
l.SetContext(ctx)

err = l.DoString(script)
Expand Down
Loading