From 6707fc94ae1b3b4227c514109b31c2e2eaba67e8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 May 2016 09:53:04 +0100 Subject: [PATCH] esp8266/modnetwork: Allow to press ctrl-C while scan() is running. Ctrl-C will raise a KeyboardInterrupt and stop the scan (although it will continue to run in the background, it won't report anything). If interrupted, and another scan() is started before the old one completes in the background, then the second scan will fail with an OSError. --- esp8266/modnetwork.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/esp8266/modnetwork.c b/esp8266/modnetwork.c index a0758a0d47..c0763ce0eb 100644 --- a/esp8266/modnetwork.c +++ b/esp8266/modnetwork.c @@ -172,7 +172,17 @@ STATIC mp_obj_t esp_scan(mp_obj_t self_in) { mp_obj_t list = mp_obj_new_list(0, NULL); esp_scan_list = &list; wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb); - ETS_POLL_WHILE(esp_scan_list != NULL); + while (esp_scan_list != NULL) { + // our esp_scan_cb is called via ets_loop_iter so it's safe to set the + // esp_scan_list variable to NULL without disabling interrupts + if (MP_STATE_VM(mp_pending_exception) != NULL) { + esp_scan_list = NULL; + mp_obj_t obj = MP_STATE_VM(mp_pending_exception); + MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; + nlr_raise(obj); + } + ets_loop_iter(); + } if (list == MP_OBJ_NULL) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "scan failed")); }