Skip to content

Commit

Permalink
ports: Fix sys.stdout.buffer.write() return value.
Browse files Browse the repository at this point in the history
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.

One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.

A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:

mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.

The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
  multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes

As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().

Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().

This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.

Signed-off-by: Maarten van der Schrieck <[email protected]>
  • Loading branch information
mvds00 authored and dpgeorge committed Dec 21, 2023
1 parent 91ee8ac commit 3bca93b
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 40 deletions.
9 changes: 7 additions & 2 deletions ports/cc3200/hal/cc3200_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,15 @@ void mp_hal_delay_ms(mp_uint_t delay) {
}
}

void mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_os_dupterm_tx_strn(str, len);
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t ret = len;
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
ret = dupterm_res;
}
// and also to telnet
telnet_tx_strn(str, len);
return ret;
}

int mp_hal_stdin_rx_chr(void) {
Expand Down
14 changes: 12 additions & 2 deletions ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,34 @@ int mp_hal_stdin_rx_chr(void) {
}
}

void mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
// Only release the GIL if many characters are being sent
mp_uint_t ret = len;
bool did_write = false;
bool release_gil = len > 20;
if (release_gil) {
MP_THREAD_GIL_EXIT();
}
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
usb_serial_jtag_tx_strn(str, len);
did_write = true;
#elif CONFIG_USB_OTG_SUPPORTED
usb_tx_strn(str, len);
did_write = true;
#endif
#if MICROPY_HW_ENABLE_UART_REPL
uart_stdout_tx_strn(str, len);
did_write = true;
#endif
if (release_gil) {
MP_THREAD_GIL_ENTER();
}
mp_os_dupterm_tx_strn(str, len);
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
did_write = true;
ret = MIN((mp_uint_t)dupterm_res, ret);
}
return did_write ? ret : 0;
}

uint32_t mp_hal_ticks_ms(void) {
Expand Down
10 changes: 8 additions & 2 deletions ports/esp8266/esp_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ void mp_hal_debug_str(const char *str) {
}
#endif

void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
mp_os_dupterm_tx_strn(str, len);
mp_uint_t mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res < 0) {
// no outputs, nothing was written
return 0;
} else {
return dupterm_res;
}
}

void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {
Expand Down
17 changes: 14 additions & 3 deletions ports/mimxrt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ int mp_hal_stdin_rx_chr(void) {
}
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
bool did_write = false;
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
size_t i = 0;
while (i < len) {
uint32_t n = len - i;
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
n = CFG_TUD_CDC_EP_BUFSIZE;
Expand All @@ -125,17 +128,25 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
MICROPY_EVENT_POLL_HOOK
}
if (ticks_us64() >= timeout) {
ret = i;
break;
}

uint32_t n2 = tud_cdc_write(str + i, n);
tud_cdc_write_flush();
i += n2;
}
did_write = true;
ret = MIN(i, ret);
}
#if MICROPY_PY_OS_DUPTERM
mp_os_dupterm_tx_strn(str, len);
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
did_write = true;
ret = MIN((mp_uint_t)dupterm_res, ret);
}
#endif
return did_write ? ret : 0;
}

uint64_t mp_hal_time_ns(void) {
Expand Down
9 changes: 7 additions & 2 deletions ports/minimal/uart_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ int mp_hal_stdin_rx_chr(void) {
}

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
#if MICROPY_MIN_USE_STDOUT
int r = write(STDOUT_FILENO, str, len);
(void)r;
if (r >= 0) {
// in case of an error in the syscall, report no bytes written
ret = 0;
}
#elif MICROPY_MIN_USE_STM32_MCU
while (len--) {
// wait for TXE
Expand All @@ -41,4 +45,5 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
USART1->DR = *str++;
}
#endif
return ret;
}
6 changes: 4 additions & 2 deletions ports/nrf/drivers/bluetooth/ble_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ int mp_hal_stdin_rx_chr(void) {
return (int)byte;
}

void mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
// Not connected: drop output
if (!ble_uart_enabled()) return;
if (!ble_uart_enabled()) return 0;

mp_uint_t ret = len;
uint8_t *buf = (uint8_t *)str;
size_t send_len;

Expand All @@ -134,6 +135,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
len -= send_len;
buf += send_len;
}
return ret;
}

void ble_uart_tx_char(char c) {
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/drivers/usb/usb_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ int mp_hal_stdin_rx_chr(void) {
return 0;
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {

mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
for (const char *top = str + len; str < top; str++) {
ringbuf_put((ringbuf_t*)&tx_ringbuf, *str);
usb_cdc_loop();
}
return len;
}

void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
Expand Down
4 changes: 3 additions & 1 deletion ports/nrf/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,12 @@ int mp_hal_stdin_rx_chr(void) {
return 0;
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
if (MP_STATE_VM(dupterm_objs[0]) != MP_OBJ_NULL) {
uart_tx_strn(MP_STATE_VM(dupterm_objs[0]), str, len);
return len;
}
return 0;
}

void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
Expand Down
4 changes: 3 additions & 1 deletion ports/pic16bit/pic16bit_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ void mp_hal_stdout_tx_str(const char *str) {
mp_hal_stdout_tx_strn(str, strlen(str));
}

void mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t ret = len;
for (; len > 0; --len) {
uart_tx_char(*str++);
}
return ret;
}

void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
Expand Down
3 changes: 2 additions & 1 deletion ports/powerpc/uart_lpc_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ int mp_hal_stdin_rx_chr(void) {
}


void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
int i;
for (i = 0; i < len; i++) {
while (lpc_uart_tx_full()) {
;
}
lpc_uart_reg_write(REG_RBR, str[i]);
}
return len;
}
3 changes: 2 additions & 1 deletion ports/powerpc/uart_potato.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int mp_hal_stdin_rx_chr(void) {
return (char)(val & 0x000000ff);
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
int i;

for (i = 0; i < len; i++) {
Expand All @@ -129,4 +129,5 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
}
potato_uart_reg_write(POTATO_CONSOLE_TX, val);
}
return len;
}
18 changes: 15 additions & 3 deletions ports/renesas-ra/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,20 @@ int mp_hal_stdin_rx_chr(void) {
}

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
bool did_write = false;
#if MICROPY_HW_ENABLE_UART_REPL
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
did_write = true;
}
#endif

#if MICROPY_HW_USB_CDC
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
size_t i = 0;
while (i < len) {
uint32_t n = len - i;
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
n = CFG_TUD_CDC_EP_BUFSIZE;
Expand All @@ -180,12 +184,20 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
tud_cdc_write_flush();
i += n2;
}
ret = MIN(i, ret);
did_write = true;
}
#endif

#if MICROPY_PY_OS_DUPTERM
mp_os_dupterm_tx_strn(str, len);
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
did_write = true;
ret = MIN((mp_uint_t)dupterm_res, ret);
}
#endif

return did_write ? ret : 0;
}

void mp_hal_ticks_cpu_enable(void) {
Expand Down
18 changes: 15 additions & 3 deletions ports/rp2/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,18 @@ int mp_hal_stdin_rx_chr(void) {
}

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
bool did_write = false;
#if MICROPY_HW_ENABLE_UART_REPL
mp_uart_write_strn(str, len);
did_write = true;
#endif

#if MICROPY_HW_USB_CDC
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
size_t i = 0;
while (i < len) {
uint32_t n = len - i;
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
n = CFG_TUD_CDC_EP_BUFSIZE;
Expand All @@ -173,18 +177,26 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_usbd_task();
}
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
ret = i;
break;
}
uint32_t n2 = tud_cdc_write(str + i, n);
tud_cdc_write_flush();
i += n2;
}
ret = MIN(i, ret);
did_write = true;
}
#endif

#if MICROPY_PY_OS_DUPTERM
mp_os_dupterm_tx_strn(str, len);
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
did_write = true;
ret = MIN((mp_uint_t)dupterm_res, ret);
}
#endif
return did_write ? ret : 0;
}

void mp_hal_delay_ms(mp_uint_t ms) {
Expand Down
17 changes: 14 additions & 3 deletions ports/samd/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,12 @@ int mp_hal_stdin_rx_chr(void) {
}
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
bool did_write = false;
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
size_t i = 0;
while (i < len) {
uint32_t n = len - i;
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
n = CFG_TUD_CDC_EP_BUFSIZE;
Expand All @@ -213,14 +216,22 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
MICROPY_EVENT_POLL_HOOK_WITH_USB;
}
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
ret = i;
break;
}
uint32_t n2 = tud_cdc_write(str + i, n);
tud_cdc_write_flush();
i += n2;
}
ret = MIN(i, ret);
did_write = true;
}
#if MICROPY_PY_OS_DUPTERM
mp_os_dupterm_tx_strn(str, len);
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
did_write = true;
ret = MIN((mp_uint_t)dupterm_res, ret);
}
#endif
return did_write ? ret : 0;
}
12 changes: 10 additions & 2 deletions ports/stm32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ MP_WEAK int mp_hal_stdin_rx_chr(void) {
}
}

MP_WEAK void mp_hal_stdout_tx_strn(const char *str, size_t len) {
MP_WEAK mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t ret = len;
bool did_write = false;
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
did_write = true;
}
#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
lcd_print_strn(str, len);
#endif
mp_os_dupterm_tx_strn(str, len);
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
did_write = true;
ret = MIN((mp_uint_t)dupterm_res, ret);
}
return did_write ? ret : 0;
}

#if __CORTEX_M >= 0x03
Expand Down
Loading

0 comments on commit 3bca93b

Please sign in to comment.