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

Runtime clears its own interrupt #589

Closed
gbl08ma opened this issue Jul 26, 2024 · 1 comment
Closed

Runtime clears its own interrupt #589

gbl08ma opened this issue Jul 26, 2024 · 1 comment

Comments

@gbl08ma
Copy link

gbl08ma commented Jul 26, 2024

I ran across what I consider to be a surprising behavior, especially after carefully reading the documentation for Interrupt and ClearInterrupt:

func TestInterruptRunTwice(t *testing.T) {
	const SCRIPT = `
	var i = 0;
	for (;;) {
		i++;
	}
	`

	vm := New()
	time.AfterFunc(200*time.Millisecond, func() {
		vm.Interrupt("halt")
	})

	_, err := vm.RunString(SCRIPT)
	if err == nil {
		t.Fatal("Err is nil")
	}

	// this will block forever, because the VM uninterrupted itself at the end of the last run
	_, err = vm.RunString(SCRIPT)
	if err == nil {
		t.Fatal("Err is nil")
	}
}

My expectation was that the VM would remain interrupted until ClearInterrupt is called.
My understanding is that this surprising behavior may have come to be after the changes in #405, but at the very least it should be documented in the Interrupt and RunScript/RunString/RunProgram functions.

If this is the intended behavior, this begs the question of how to best ensure that a runtime remains stopped (returning *InterruptedError, etc.) even if there are requests to run something[1]. Is the most straightforward solution to add extra logic that always checks if Interrupt needs to be called again after each RunScript/RunString/RunProgram invocation?

[1] Requests which may have been, for example, enqueued via the RunOnLoop function of the goja_nodejs event loop, prior to the loop being Terminated, and which I may want to return immediately with an error due to the runtime having been interrupted after a timeout waiting for the remaining enqueued jobs to complete...

@dop251
Copy link
Owner

dop251 commented Jul 27, 2024

Interrupt meant to be a one-off event. You should have a state associated with a Runtime that would contain a flag saying it should not be used anymore. If it's not stated clear enough in the docs, please raise a PR.

@dop251 dop251 closed this as completed Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants