ctreffe / alfred

Alfred - A library for rapid experiment development
MIT License
10 stars 1 forks source link

Cannot abort experiments on first page using on_first_show() or on_every_show() #267

Closed ctreffe closed 4 months ago

ctreffe commented 4 months ago

Based on the alfred3 documentation, I expect to be able to abort an experiment from every page and every page hook. However, the following code works as expected in a single page experiment when using the _on_expaccess hook, but not when using _on_firstshow or _on_everyshow:

import alfred3 as al
from datetime import datetime
exp = al.Experiment()

@exp.member
class SinglePage(al.Page):
    title = "Hello World"
    name = "my_page"

    def on_exp_access(self):
        current_time = datetime.now()

        if not current_time.isoweekday() < 6:
            self.exp.abort(
                reason="inactive",
                title="Experiment inactive",
                icon="users",
                msg="The experiment is inactive on weekends."
            )

Changing _on_expaccess to either _on_firstshow or _on_everyshow yields the following error:

2024-04-28 22:45:59,946 - exp.default_id - ERROR - experiment id=default_id - session id=sid-8e2fc5b22172454eac5c73aa1e5408fa - Exception during experiment startup.
Traceback (most recent call last):
  File "/Users/ctreffe/.venv/alfred3/lib/python3.12/site-packages/alfred3/localserver.py", line 77, in start
    script.exp_session._start()
  File "/Users/ctreffe/.venv/alfred3/lib/python3.12/site-packages/alfred3/experiment.py", line 939, in _start
    self.user_interface_controller.start()
  File "/Users/ctreffe/.venv/alfred3/lib/python3.12/site-packages/alfred3/ui_controller.py", line 913, in start
    self.exp.movement_manager.start()
  File "/Users/ctreffe/.venv/alfred3/lib/python3.12/site-packages/alfred3/ui_controller.py", line 523, in start
    self.current_page._on_showing_widget(show_time=time.time())
  File "/Users/ctreffe/.venv/alfred3/lib/python3.12/site-packages/alfred3/page.py", line 253, in _on_showing_widget
    raise AbortMove
alfred3.exceptions.AbortMove

I think this might be a bug or maybe we need to outline relevant limitations on the usage of the abort() method?

jobrachem commented 4 months ago

So that means, instead of aborting in a controlled way, the experiment crashes?

ctreffe commented 4 months ago

Exactly. Sorry, I cropped to much of the console output, the experiment returns a 500 server error when this happens.

jobrachem commented 4 months ago

Sounds like a bug to me. The AbortMove exception should be handled, and the experiment should be aborted in a controlled way. I'll look into it 👍

ctreffe commented 4 months ago

Alright, many thanks. This is not high on my priority list, however. I will use on_exp_access for the time being.

jobrachem commented 4 months ago

Ok, I found out what went wrong. The issue occurs only if you abort in an on_first_show or on_each_show hook on the first page, because the AbortMove exception was not handled during experiment startup. The issue is fixed in https://github.com/ctreffe/alfred/pull/268