Skip to content

Commit 0ab3725

Browse files
committed
extmod/moduos_dupterm: Dumpterm subsystem is responsible for closing stream.
Make dupterm subsystem close a term stream object when EOF or error occurs. There's no other party than dupterm itself in a better position to do this, and this is required to properly reclaim stream resources, especially if multiple dupterm sessions may be established (e.g. as networking connections).
1 parent 3a29db8 commit 0ab3725

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

esp8266/esp_mphal.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,13 @@ static int call_dupterm_read(void) {
171171
mp_buffer_info_t bufinfo;
172172
mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ);
173173
if (bufinfo.len == 0) {
174-
MP_STATE_PORT(term_obj) = NULL;
175-
mp_printf(&mp_plat_print, "dupterm: EOF received, deactivating\n");
174+
mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL);
176175
return -1;
177176
}
178177
nlr_pop();
179178
return *(byte*)bufinfo.buf;
180179
} else {
181-
MP_STATE_PORT(term_obj) = NULL;
182-
mp_printf(&mp_plat_print, "dupterm: Exception in read() method, deactivating: ");
183-
mp_obj_print_exception(&mp_plat_print, nlr.ret_val);
180+
mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val);
184181
}
185182

186183
return -1;

extmod/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_uos_dupterm_obj);
3434

3535
#if MICROPY_PY_OS_DUPTERM
3636
void mp_uos_dupterm_tx_strn(const char *str, size_t len);
37+
void mp_uos_deactivate(const char *msg, mp_obj_t exc);
3738
#else
3839
#define mp_uos_dupterm_tx_strn(s, l)
3940
#endif

extmod/moduos_dupterm.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,20 @@
3131
#include "py/nlr.h"
3232
#include "py/runtime.h"
3333
#include "py/objtuple.h"
34+
#include "py/stream.h"
3435

3536
#if MICROPY_PY_OS_DUPTERM
3637

38+
void mp_uos_deactivate(const char *msg, mp_obj_t exc) {
39+
mp_obj_t term = MP_STATE_PORT(term_obj);
40+
MP_STATE_PORT(term_obj) = NULL;
41+
mp_printf(&mp_plat_print, msg);
42+
if (exc != MP_OBJ_NULL) {
43+
mp_obj_print_exception(&mp_plat_print, exc);
44+
}
45+
mp_stream_close(term);
46+
}
47+
3748
void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
3849
if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
3950
nlr_buf_t nlr;
@@ -44,9 +55,7 @@ void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
4455
mp_call_method_n_kw(1, 0, write_m);
4556
nlr_pop();
4657
} else {
47-
MP_STATE_PORT(term_obj) = NULL;
48-
mp_printf(&mp_plat_print, "dupterm: Exception in write() method, deactivating: ");
49-
mp_obj_print_exception(&mp_plat_print, nlr.ret_val);
58+
mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val);
5059
}
5160
}
5261
}

0 commit comments

Comments
 (0)