diff --git a/src/resources/uart_esp32.cc b/src/resources/uart_esp32.cc index 2f9b0ab4f..03aa5ac99 100644 --- a/src/resources/uart_esp32.cc +++ b/src/resources/uart_esp32.cc @@ -290,17 +290,12 @@ PRIMITIVE(read) { return Primitive::os_error(err, process); } - // The uart_get_buffered_data_len is not thread safe, so it's not guaranteed that - // we get an updated value. As false negatives can lead to deadlock, we don't trust - // when it returns 0. To work around this, we instead try to read 8 bytes, whenever - // we are suggested 0; it is always safe to try to read too much, so this is only a - // performance issue. - size_t capacity = Utils::max(available, (size_t)8); - Error* error = null; - ByteArray* data = process->allocate_byte_array(capacity, &error, /*force_external*/ true); + ByteArray* data = process->allocate_byte_array(available, &error, /*force_external*/ available != 0); if (data == null) return error; + if (available == 0) return data; + ByteArray::Bytes rx(data); int read = uart_read_bytes(uart->port(), rx.address(), rx.length(), 0); if (read == -1) { @@ -311,8 +306,6 @@ PRIMITIVE(read) { return process->allocate_string_or_error("broken UART read"); } - data->resize_external(process, read); - return data; }