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

[BUG] deinit() method is interrupted by auto-reload #750

Open
regicidalplutophage opened this issue Mar 20, 2023 · 3 comments
Open

[BUG] deinit() method is interrupted by auto-reload #750

regicidalplutophage opened this issue Mar 20, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@regicidalplutophage
Copy link
Member

regicidalplutophage commented Mar 20, 2023

Describe the bug
Auto-reload stops execution abruptly: deinit() is being interrupted.

To Reproduce
A debug statement has been added to deinit():

    def deinit(self) -> None:
        for module in self.modules:
            try:
                module.deinit(self)
            except Exception as err:
                if debug.enabled:
                    debug(f'Error in {module}.deinit: {err}')
        for ext in self.extensions:
            try:
                ext.deinit(self.sandbox)
            except Exception as err:
                if debug.enabled:
                    debug(f'Error in {ext}.deinit: {err}')       
        debug('Deinitialized!')

It never shows up in serial upon autoreload:

main.py output:
Starting
579768 kmk.kmk_keyboard: Unexpected error: cleaning up

Code stopped by auto-reload. Reloading soon.

Other functions that are supposed to be called during deinit() never get called.

Expected behavior
Stopping execution with ctrl+c works as it should:

main.py output:
Starting
486835 kmk.kmk_keyboard: Unexpected error: cleaning up
486841 kmk.kmk_keyboard: Deinitialized!
Traceback (most recent call last):
  File "main.py", line 202, in <module>
  File "kmk/kmk_keyboard.py", line 523, in go
  File "kmk/kmk_keyboard.py", line 607, in _main_loop
  File "kmk/kmk_keyboard.py", line 470, in after_hid_send
  File "kmk/extensions/RGB.py", line 244, in after_hid_send
  File "kmk/extensions/RGB.py", line 432, in animate
KeyboardInterrupt:

Code done running.

Autoreload should function similarly.

Debug output
If applicable, add debug output from the serial console to help explain your problem.

Additional context
Add any other context about the problem here.

@regicidalplutophage regicidalplutophage added the bug Something isn't working label Mar 20, 2023
@xs5871
Copy link
Collaborator

xs5871 commented Mar 23, 2023

I've looked into it and that's just how the supervisor does things. On an auto-reload event, it'll just drop everything. atexit doesn't help, because it requires a "successfull" exit of the vm.
You'd have to take that complaint upstream to Circuitpython.

Interesting note:It still works if you touch something in the filesystem. Only actual file content writes make the supervisor kill the VM without notice, it seems like.

@xs5871 xs5871 changed the title deinit() method is interrupted by auto-reload[BUG] [BUG] deinit() method is interrupted by auto-reload Mar 24, 2023
@grasegger
Copy link
Contributor

Maybe a deinit py file could help?

https://docs.circuitpython.org/en/latest/shared-bindings/supervisor/index.html#supervisor.set_next_code_file

On the first go call kmk could set a file within the kmk dir to be run next. On autoreload that deinit file reads the code.py and grabs the keyboard extensions to execute deinit on all of them. Finally it enqueues the code.py to be executet and calls supervisor.reload()

@xs5871
Copy link
Collaborator

xs5871 commented Aug 28, 2023

That wouldn't work. You have to effectively re-initialize the entire keyboard in order to find out what all the extension and modules are. That would defeat the purpose of a deinit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants