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

Dont lock the event loop once a promise rejection is left unhandled #2693

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions js/eventloop/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (e *EventLoop) popAll() (queue []func() error, awaiting bool) {
// or a queued function returns an error. The provided firstCallback will be the first thing executed.
// After Start returns the event loop can be reused as long as waitOnRegistered is called.
func (e *EventLoop) Start(firstCallback func() error) error {
e.pendingPromiseRejections = make(map[*goja.Promise]struct{})
e.queue = []func() error{firstCallback}
for {
queue, awaiting := e.popAll()
Expand Down
49 changes: 49 additions & 0 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2655,3 +2655,52 @@ func TestExecutionInfo(t *testing.T) {
})
}
}

func TestPromiseRejectionIsCleared(t *testing.T) {
t.Parallel()

r1, err := getSimpleRunner(t, "/script.js", `
exports.default = () => {
let p = new Promise((res) => {
if (__ITER == 1) {
throw "oops"
}
res("yes");
})
p.then((r) => {
console.log(r);
})
}`)
require.NoError(t, err)
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, r1.MakeArchive())
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
r := r
t.Run(name, func(t *testing.T) {
t.Parallel()
initVU, err := r.NewVU(1, 1, make(chan metrics.SampleContainer, 100))
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
require.NoError(t, err)

err = vu.RunOnce()
require.ErrorContains(t, err, "Uncaught (in promise) oops")

err = vu.RunOnce()
require.NoError(t, err)
})
}
}