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

Fix 3986 background reset remove existing usb callback #4122

Merged
merged 3 commits into from
Feb 5, 2021

Conversation

hathach
Copy link
Member

@hathach hathach commented Feb 4, 2021

fix #3986

Issue

run_code.py decide to reload (AUTO_RELOAD) and run background_callback_reset() while background list is still queued up with usb event.

background_callback_run_all: 95:
callback_head = 200029A4
callback_tail = 200029A4
background_callback_run_all: 100: // background_callback_run_all() exit while there is queuing callbacks
run_code_py: 340:  // reload request == true
        usb_irq_handler: 103: // usb isr occurs -> more callback queued up
callback_head = 200029A4
callback_tail = 200029BC
background_callback_add_core: 57:
run_code_py: 305: // cleanup_after_vm() is called which in turn reset all the callbacks including above usb events.
background_callback_reset: 122:
background_callback_run_all: 73:

To sum up the issue is not related to usb stack, it seems to be caused by auto-reload while background task are still queued up. background_callback_run_all() is not guarantee to execute all the background since while executing background, those function can potentially trigger more usb ISR.
https://github.com/adafruit/circuitpython/blob/main/supervisor/shared/background_callback.c#L91

We can modify background_callback_run_all() to loop over while callback_head is not NULL. However, there is still race condition where the usb (or other) ISR is triggered during run_code_py: 340: // reload request == true and the cleanup_after_vm(). I actually don't understand the background mechanism enough to propose a sound solution here. @dhalbert let me know if my analysis makes sense to you.

Originally posted by @hathach in #3986 (comment)

Solution

  1. the tud_task() is cheap enough when executing without event, that we could just add it background_callback_add(&usb_callback, usb_background_do, NULL); unconditionally somewhere after background_callback_reset().

Originally posted by @hathach in #3986 (comment)

just a proof to show that issue analysis is correct
unconditionally schedule usb background after
background_callback_reset()
main.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very, very much for spending the time on this! If you want to fix anything up, I'll wait for new commits. Otherwise we can merge as is.

supervisor/shared/background_callback.c Show resolved Hide resolved
supervisor/shared/background_callback.c Show resolved Hide resolved
supervisor/usb.h Show resolved Hide resolved
main.c Outdated Show resolved Hide resolved
@hathach
Copy link
Member Author

hathach commented Feb 5, 2021

Thank you very, very much for spending the time on this! If you want to fix anything up, I'll wait for new commits. Otherwise we can merge as is.

It is always my pleasure to help, don't hesitate to tag me for any usb-related bugs. Except for choosing between run the background immediately vs schedule as PR. I have nothing to add.

dhalbert
dhalbert previously approved these changes Feb 5, 2021
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, this works, and fixes a serious problem. I will merge. Thanks!

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shrank a couple of overflowing builds. All set now!

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

Successfully merging this pull request may close these issues.

Copying multiple larger files can hang the copy if autoreload is on
2 participants