expr
expr copied to clipboard
Timeout
Is there a way to timeout and kill a running expr.Run/VM.Run process if it is taking too long? This is to terminate/interupt runaway code that was miscoded,etc. I did a string search for use of golang context and did not see any.
Thank you.
I propose these code fragments:
const shortDuration = 1 * time.Millisecond
d := time.Now().Add(shortDuration)
ctx, cancel := context.WithDeadline(context.Background(), d)
vm1:=expr.VM{}
VM.Run(prg, env, ctx)
func (vm *VM) Run(program *Program, env interface{}, ctx, cancel) (out interface{}, err error) {
defer func() {
if r := recover(); r != nil {
f := &file.Error{
Location: program.Locations[vm.pp],
Message: fmt.Sprintf("%v", r),
}
err = f.Bind(program.Source)
}
}()
var stopRunning:=false
go func(){
select {
case: <-ctx.Done():
cancel()
stopRunning=true
}
}()
.
.
.
vm.limit = MemoryBudget
vm.ip = 0
vm.pp = 0
for vm.ip < len(vm.bytecode) {
.
.
.
if stopRunning{
break;
}
}
What about RunWithContext
like we have http.NewRequest
and http.NewRequestWithContext
.
Lack of timeout enables DDoS attacks.
Duplicate of #260