expr icon indicating copy to clipboard operation
expr copied to clipboard

Timeout

Open seekerkoruth opened this issue 3 years ago • 2 comments

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.

seekerkoruth avatar Jun 23 '21 12:06 seekerkoruth

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;
		}
	}

seekerkoruth avatar Jun 23 '21 12:06 seekerkoruth

What about RunWithContext like we have http.NewRequest and http.NewRequestWithContext.

Lack of timeout enables DDoS attacks.

alexec avatar Oct 21 '21 20:10 alexec

Duplicate of #260

antonmedv avatar Nov 05 '22 19:11 antonmedv