Catch Keyboard Interrupt, Finally section incomplete?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Catch Keyboard Interrupt, Finally section incomplete?

Post by Jibun no kage » Thu Aug 18, 2022 5:42 pm

Catch Keyboard Interrupt, Finally section incomplete? Is this something unique to MP? After I except a keyboard interrupt, the finally section following starts but is not completely executed. This is on Pico port, has not tested on ESP yet.

Test...

Code: Select all

import utime
try:
    print('test')
    while True:
        print('wait')
        utime.sleep(1)
except KeyboardInterrupt as theInterrupt:
    print('interrupt')
finally:
    i=0
    while (i<10):
        print(i)
        utime.sleep(1)
    print('done')
Output...

Code: Select all

>>> %Run -c $EDITOR_CONTENT
test
wait
wait
wait
interrupt
0
Traceback (most recent call last):
  File "<stdin>", line 13, in <module>
KeyboardInterrupt: 
>>> 

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Catch Keyboard Interrupt, Finally section incomplete?

Post by jimmo » Thu Aug 18, 2022 11:57 pm

Jibun no kage wrote:
Thu Aug 18, 2022 5:42 pm
This is on Pico port, has not tested on ESP yet.
This works for me on a Pico, running both 1.19.1 and latest development version.

I see "wait" "wait" "wait" etc, then hit Ctrl-C, and then it prints "interrupt", then "0", "0", etc until I press Ctrl-C again.

Are you sure that whatever terminal you're using isn't sending more than one Ctrl-C. Try using mpremote or screen.

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Catch Keyboard Interrupt, Finally section incomplete?

Post by Jibun no kage » Fri Aug 19, 2022 6:12 pm

Other than I forgot the i=i+1, 0,0,0 as output is what should happen for code as written. So why do I only get a single 0. Interesting. How would I know I am or am not getting more than a single Control-C? That would definitely explain the results I am getting. Is there a way I can mask the Control-C? REPL overrides the typical kbd_intr(-1) right? I am using Thonny REPL at the moment. Will try something else.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Catch Keyboard Interrupt, Finally section incomplete?

Post by jimmo » Sat Aug 20, 2022 1:42 pm

Jibun no kage wrote:
Fri Aug 19, 2022 6:12 pm
I am using Thonny REPL at the moment. Will try something else.
This wouldn't be suprising - In order to facilitate all the extra features, Thonny's REPL isn't at all like a regular console. (For a start, it's line buffered).

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Catch Keyboard Interrupt, Finally section incomplete?

Post by Jibun no kage » Sat Aug 20, 2022 7:12 pm

I figured out part of the issue, or a similar scenario. If you Control-C (in Thonny) while asyncio.run(main()) has control, the finally section in main() never executes, even if you have a valid except stanza in main(). If you wrap a try except finally around asyncio.run(main()), that interrupt is processed. This makes complete sense, given the context of the interrupt is typically during an asyncio.sleep() (in the main(), while loop) which is within the asyncio run frame. It is just a bit counter intuitive.

None of this critical, since I only use Control-C during code development, but clearly there is a scope/control issue that comes into play when the Control-C is invoked.

Post Reply