google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

Use context for runtime, and allow to control timeout for script running? #470

Open Solveay opened 1 year ago

adonovan commented 1 year ago

You can already achieve this with the current API, but it requires an additional goroutine to transmit cancellation of ctx to the interpreter, something like this:

{
    ctx, cancel := context.WithCancel(ctx)
    defer cancel()
    go func() {
        <-ctx.Done()
        thread.Cancel("context cancelled")
    }()
    ... evaluate starlark in 'thread' ...
}

Perhaps we could make this simpler by adding a convenience function to the API, or maybe all it needs is an executable example of cancellation in action.

If you pass the context in the thread using Thread.SetLocal, then long-running built-in functions can retrieve it and honor cancellation too; see https://github.com/google/starlark-go/issues/252#issuecomment-553930328.

See also: