ESP32: KeyboardInterrupt raised in thread while typed in UART REPL.
Hello,
Here is how to reproduce my problem:
- I start a task A in a new thread.
- Then I start another task B in the main thread (UART REPL).
- Then I press CTRL+C.
Expected behaviour:
Task B gets a KeyboardInterrupt exception. Task A continues running.
Actual behaviour:
KeyboardInterrupt is randomly raised in task A or B.
Here is the full log:
MicroPython v1.12-96-gc3095b37e-dirty on 2020-01-28; ESP32 module with ESP32
Type "help()" for more information.
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import _thread
=== import time
===
=== def long_process(name):
=== for i in range(1000):
=== print(name, i)
=== time.sleep(1)
===
=== _thread.start_new_thread(long_process, ("thread",))
===
=== long_process("main")
main 0
thread 0
main 1
thread 1
main 2
thread 2
main 3
thread 3
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
File "<stdin>", line 7, in long_process
KeyboardInterrupt:
>>> thread 4
thread 5
thread 6
thread 7
>>> thread 8
thread 9
>>>
MPY: soft reboot
MicroPython v1.12-96-gc3095b37e-dirty on 2020-01-28; ESP32 module with ESP32
Type "help()" for more information.
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import _thread
=== import time
===
=== def long_process(name):
=== for i in range(1000):
=== print(name, i)
=== time.sleep(1)
===
=== _thread.start_new_thread(long_process, ("thread",))
===
=== long_process("main")
main 0
thread 0
main 1
thread 1
main 2
thread 2
Unhandled exception in thread started by <function long_process at 0x3ffe4e20>
Traceback (most recent call last):
File "<stdin>", line 7, in long_process
KeyboardInterrupt:
main 3
main 4
main 5
main 6
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
File "<stdin>", line 7, in long_process
File "<stdin>", line 7, in long_process
KeyboardInterrupt:
>>>
During the first try you can see that after the KeyboardInterrupt the thread task is still running. This is the excepted behaviour.
Then after a soft reboot it's the opposite. The thread task got the interrupt and the main task is still running.
My understanding is that the UART received chars are handled in an interrupt, and when the CTRL+C char is detected it raise the KeyboardInterrupt exception in the current running thread.
Is this the normal behaviour or is this a bug?
Thanks,
Yann.
rp2: KeyboardInterrupt thrown in 2nd thread instead of main
I remember having read about it somewhere but couldn't find it before opening this issue..
When using a thread on the pico, my KeyboardInterrupt is always thrown in that thread running on the 2nd core. This is problematic as it always stops my background library instead of whatever I just executed in the repl. The 2nd KeyboardInterrupt will get thrown in my repl on the 1st core but then I always have to reset the device or start the thread again.
In a running application this might not be a problem but for testing it's a nuisance.
Is there any easy way to change this behaviour? I'd say the thread on the 2nd core doesn't even need to catch a KeyboardInterrupt (or at least be the 2nd one to receive it so the repl/1st core comes first).