Make errors more recoverable
Kevinpgalligan opened this issue · 6 comments
It's natural to make mistakes when developing sketches. I have found, however, that a bug in a sketch body can cause the whole process to hang. I then have to force the sketch window to shut, which crashes the SLIME process, and I have to load all of my packages / compile all my code again. Obviously, this is not ideal from a workflow standpoint.
Perhaps this is an unavoidable side effect of calling into C libraries, but I wonder if it would be possible to make sketch more resilient to these types of errors. Ideally, it would be possible to shut the window of a buggy sketch, redefine the sketch, and then run it again without any further delay. Or, even better, drop the user into a debugger.
I don't have any example cases right now, this is just something I've observed while working with the library, e.g. if I pass bad values to one of the sketch drawing functions.
That indeed would be great.
By the way, this PR to cl-sdl2 might be related (but I'm not sure): lispgames/cl-sdl2#149
A few more thoughts: errors from passing wrong values to draw functions are most likely OpenGL errors or SDL2 errors; both should probably be cleared to make the system usable again (for example (sdl2-ffi.functions:sdl-clear-error)
for SDL2, not sure what exactly is needed for opengl errors.
@Kevinpgalligan Could you please paste the code that makes it hang?
I don't have any specific examples right now, it's just something I've encountered repeatedly while working on sketches. I think the most recent one was when I tried to paint to the canvas with out-of-bounds indexes (e.g. (canvas-paint cv colour 2 0)
when it's a 2x2 canvas). It might be something that needs to be handled on a case-by-case basis. I will update when I encounter it again.
From kit.sdl2:
(defmethod render :around ((window window))
(when (render-enabled window)
(sdl2:in-main-thread ()
(handler-bind ((error
(lambda (e)
(setf (render-enabled window) nil)
(error e)))) ;; by the way,
(call-next-method)))))
When "showing restarts", this handler is triggered, and the window is no longer being rendered even after fixing the error & invoking a restart continue.
See also the entry in kit.sdl2's README: https://github.com/lispgames/sdl2kit#render-disable