Skip to content

Commit

Permalink
Interrupt the JS runtime during VU init if the context was cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Dec 7, 2022
1 parent 834d62a commit f9d3341
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (b *Bundle) initializeProgramObject(rt *goja.Runtime, init *InitContext) pr
return pgm
}

//nolint:funlen
func (b *Bundle) instantiate(init *InitContext, vuID uint64) (err error) {
rt := init.moduleVUImpl.runtime
logger := init.logger
Expand Down Expand Up @@ -335,6 +336,20 @@ func (b *Bundle) instantiate(init *InitContext, vuID uint64) (err error) {
init.moduleVUImpl.eventLoop = eventloop.New(init.moduleVUImpl)
pgm := b.initializeProgramObject(rt, init)

// TODO: make something cleaner for interrupting scripts, and more unified
// (e.g. as a part of the event loop or RunWithPanicCatching()?
initCtxDone := init.moduleVUImpl.ctx.Done()
initDone := make(chan struct{})
watchDone := make(chan struct{})
go func() {
select {
case <-initCtxDone:
rt.Interrupt(context.Canceled)
case <-initDone: // do nothing
}
close(watchDone)
}()

err = common.RunWithPanicCatching(logger, rt, func() error {
return init.moduleVUImpl.eventLoop.Start(func() error {
f, errRun := rt.RunProgram(b.Program)
Expand All @@ -350,6 +365,8 @@ func (b *Bundle) instantiate(init *InitContext, vuID uint64) (err error) {
panic("Somehow a commonjs main module is not wrapped in a function")
})
})
close(initDone)
<-watchDone

if err != nil {
var exception *goja.Exception
Expand Down

0 comments on commit f9d3341

Please sign in to comment.