From 14ec7a0d939b485daac755c8159fde15ec03fad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 17 Jun 2021 11:53:00 +0200 Subject: [PATCH] mavlink: tcflush() uart before closing it On NuttX with flow control, if no one was reading from the uart, the close() call would block indefinitely waiting for data to be sent. --- src/modules/mavlink/mavlink_main.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index 7cc14007d3..c06c3d0633 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -324,12 +324,9 @@ Mavlink::destroy_all_instances() } } - //we know all threads have exited, so it's safe to manipulate the linked list and delete objects. + //we know all threads have exited, so it's safe to delete objects. for (Mavlink *inst_to_del : mavlink_module_instances) { - if (inst_to_del != nullptr) { - delete inst_to_del; - inst_to_del = nullptr; - } + delete inst_to_del; } printf("\n"); @@ -2533,6 +2530,8 @@ Mavlink::task_main(int argc, char *argv[]) _streams.clear(); if (_uart_fd >= 0 && !_is_usb_uart) { + /* discard all pending data, as close() might block otherwise on NuttX with flow control enabled */ + tcflush(_uart_fd, TCIOFLUSH); /* close UART */ ::close(_uart_fd); }