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

UX improvement: conditionalize the installation of rescue handler... #439

Open
sean- opened this issue Jan 22, 2024 · 0 comments
Open

UX improvement: conditionalize the installation of rescue handler... #439

sean- opened this issue Jan 22, 2024 · 0 comments

Comments

@sean-
Copy link
Contributor

sean- commented Jan 22, 2024

While integrating tengo into a program, I had a simple nil pointer dereference error, but it was difficult to track down without patching tengo. I don't think it's right to change the interface of RunContext(), so I have the following proposed patch. Thoughts?

--- script.go.orig	2024-01-22 12:25:56
+++ script.go	2024-01-22 12:27:48
@@ -200,6 +200,7 @@
 	globals       []Object
 	maxAllocs     int64
 	lock          sync.RWMutex
+	disableRecover bool
 }
 
 // Run executes the compiled script in the virtual machine.
@@ -211,6 +212,12 @@
 	return v.Run()
 }
 
+// EnableRescue toggles whether or not the runtime recovery method should be
+// enabled or not (default enabled).
+func (c *Compiled) EnableRescue(b bool) {
+	c.disableRecover = !b
+}
+
 // RunContext is like Run but includes a context.
 func (c *Compiled) RunContext(ctx context.Context) (err error) {
 	c.lock.Lock()
@@ -219,6 +226,7 @@
 	v := NewVM(c.bytecode, c.globals, c.maxAllocs)
 	ch := make(chan error, 1)
 	go func() {
+		if !c.disableRecover {
 		defer func() {
 			if r := recover(); r != nil {
 				switch e := r.(type) {
@@ -231,6 +239,7 @@
 				}
 			}
 		}()
+		}
 		ch <- v.Run()
 	}()
 
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

1 participant