ports: Fix sys.stdout.buffer.write() return value.

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 <maarten@thingsconnected.nl>
pull/11850/head
Maarten van der Schrieck 2023-06-18 11:46:25 +02:00 zatwierdzone przez Damien George
rodzic 91ee8ac894
commit 3bca93b2d0
22 zmienionych plików z 136 dodań i 40 usunięć

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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;
@ -125,6 +128,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
MICROPY_EVENT_POLL_HOOK
}
if (ticks_us64() >= timeout) {
ret = i;
break;
}
@ -132,10 +136,17 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
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) {

Wyświetl plik

@ -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
@ -41,4 +45,5 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
USART1->DR = *str++;
}
#endif
return ret;
}

Wyświetl plik

@ -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;
@ -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) {

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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) {

Wyświetl plik

@ -108,7 +108,7 @@ 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()) {
@ -116,4 +116,5 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
}
lpc_uart_reg_write(REG_RBR, str[i]);
}
return len;
}

Wyświetl plik

@ -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++) {
@ -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;
}

Wyświetl plik

@ -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;
@ -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) {

Wyświetl plik

@ -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;
@ -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) {

Wyświetl plik

@ -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;
@ -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;
}

Wyświetl plik

@ -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

Wyświetl plik

@ -184,10 +184,15 @@ main_term:;
return c;
}
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) {
ssize_t ret;
MP_HAL_RETRY_SYSCALL(ret, write(STDOUT_FILENO, str, len), {});
mp_os_dupterm_tx_strn(str, len);
mp_uint_t written = ret < 0 ? 0 : ret;
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
if (dupterm_res >= 0) {
written = MIN((mp_uint_t)dupterm_res, written);
}
return written;
}
// cooked is same as uncooked because the terminal does some postprocessing

Wyświetl plik

@ -27,8 +27,9 @@
#include "library.h"
#include "mphalport.h"
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_js_write(str, len);
return len;
}
void mp_hal_delay_ms(mp_uint_t ms) {

Wyświetl plik

@ -28,7 +28,7 @@
#include "shared/runtime/interrupt_char.h"
#define mp_hal_stdin_rx_chr() (0)
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);
void mp_hal_delay_ms(mp_uint_t ms);
void mp_hal_delay_us(mp_uint_t us);

Wyświetl plik

@ -221,10 +221,11 @@ 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) {
MP_THREAD_GIL_EXIT();
write(STDOUT_FILENO, str, len);
int ret = write(STDOUT_FILENO, str, len);
MP_THREAD_GIL_ENTER();
return ret < 0 ? 0 : ret; // return the number of bytes written, so in case of an error in the syscall, return 0
}
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {

Wyświetl plik

@ -44,7 +44,8 @@ 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;
#ifdef CONFIG_CONSOLE_SUBSYS
while (len--) {
char c = *str++;
@ -60,4 +61,5 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
uart_poll_out(uart_console_dev, *str++);
}
#endif
return ret;
}

Wyświetl plik

@ -56,7 +56,7 @@ void mp_hal_stdout_tx_str(const char *str);
#endif
#ifndef mp_hal_stdout_tx_strn
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);
#endif
#ifndef mp_hal_stdout_tx_strn_cooked

Wyświetl plik

@ -142,8 +142,7 @@ STATIC mp_uint_t stdio_buffer_read(mp_obj_t self_in, void *buf, mp_uint_t size,
}
STATIC mp_uint_t stdio_buffer_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
mp_hal_stdout_tx_strn(buf, size);
return size;
return mp_hal_stdout_tx_strn(buf, size);
}
STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = {