Remove session callbacks on exception

What are you trying to do?
I am trying to remove all add_next_tick_callback from executing whenever exception is raised in prior execution.

What have you tried that did NOT work as expected? If you have also posted this question elsewhere (e.g. StackOverflow), please include a link to that post.
I have tried to remove session callbacks whenever exception is raised. But it’s not working and all the functions added in the queue through add_next_tick_callback still executes independently.

If this is a question about Bokeh code, please include a complete Minimal, Reproducible Example so that reviewers can test and see what you see.

from bokeh.models import Button
import time
from bokeh.io import curdoc
from bokeh.io import show

def dummy_func2():
    time.sleep(2)
    print("Function 2")

def dummy_func1():
    time.sleep(2)
    print("Function 1")
    try:
        raise ValueError("Error")
    except ValueError:
        print("Error Raised")
        print(curdoc().session_callbacks[0])
        curdoc().remove_next_tick_callback(curdoc().session_callbacks[0])

def func1():
    curdoc().add_next_tick_callback(dummy_func1)
    curdoc().add_next_tick_callback(dummy_func2)

button = Button(label="Foo", button_type="success")
button.on_click(func1)

curdoc().add_root(button)

Even though exception is raised in dummy_func1 and dummy_func2 callback is removed from the session, dummy_func2 executes.

Any support would be appreciable. Let me know if there are any other ways to remove session callbacks from the queue once it comes out of python function.

Thanks,
Chirag Modi

There’s actually no way to remove a queued next-tick callback at the Tornado level. We had put in place a hacky solution in the Bokeh APIs that worked at one point:

However, this code has not been touched in many years. It’s possible there is something about newer versions of Tornado (or something else) that made this hacky workaround stop working.

All I can suggest is to make a GitHub Issue with full details but I would also want to be clear and state up front that one outcome may just be a determination that remove_next_tick_callback is simply unsupportable and will have to be removed altogether.

My only practical suggestion is to try something similar with a flag to try and make the second callback skip its work.

Thank you for your response.

I will create an issue in github.

For the possible solution provided here, How can I use this with the example that I provided? What changes do I need to make?

@cmodi369 you would define a global variable e.g. skip_callack = False that the first callback could set, and the second callback could test.