From 4438ad791332f0376e759d410381cbda28e13ca2 Mon Sep 17 00:00:00 2001 From: gguidone Date: Fri, 31 Jul 2026 14:02:19 +0200 Subject: [PATCH] fix(mavlink): stop sizing the receive thread stack by sizeof(MavlinkReceiver) The receive thread stack is requested as sizeof(MavlinkReceiver) + 2840, but the object is not on that stack. It is a member of the heap-allocated Mavlink object (mavlink_main.h), and pthread_create is handed only (void *)this, which start_trampoline casts straight back to a pointer. Every MAVLink instance therefore reserves a second copy of the object as stack the thread can never touch. The term is a leftover from a refactor round trip: before 0a0c404a08 heap (new/delete in start_helper), stack 2840 0a0c404a08 object became a stack local, stack sizeof + 2840 4498509426 object became a heap-resident member, term left behind so the pairing has been wrong since 4498509426 (Apr 2021). On px4_fmu-v4 sizeof(MavlinkReceiver) is 4592 and sizeof(Mavlink) is 5568, so the receiver is 82% of its heap-allocated parent. PX4_STACK_ADJUSTED is the identity on NuttX and this board has no networking, so the request was 7432 B per instance. Deepest measured high-water over the receive paths is 1908 B, 26% of that. Replaced with a flat 4000 B, 2.1x the deepest measurement, returning 3432 B per MAVLink instance. Measured on px4_fmu-v4 with CONFIG_STACK_COLORATION=y, taking top once between each of: full parameter download and rewrite, 30-item mission upload and readback, MAVFTP listing and 400 kB log download, and COMMAND_LONG handling. MAVFTP was the deepest path. Effect on hardware: with a USB ground station attached the board goes from no MAVLink heartbeat at all in 110 s to a heartbeat in 1 s. (cherry picked from commit 61b7efcbc352747f38aecdb68f73cdf5ad88a4e8) --- src/modules/mavlink/mavlink_receiver.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 3d6ace82730..907167a4ed9 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -4127,8 +4127,7 @@ void MavlinkReceiver::start() param.sched_priority = SCHED_PRIORITY_MAX - 80; (void)pthread_attr_setschedparam(&receiveloop_attr, ¶m); - pthread_attr_setstacksize(&receiveloop_attr, - PX4_STACK_ADJUSTED(sizeof(MavlinkReceiver) + 2840 + MAVLINK_RECEIVER_NET_ADDED_STACK)); + pthread_attr_setstacksize(&receiveloop_attr, PX4_STACK_ADJUSTED(4000 + MAVLINK_RECEIVER_NET_ADDED_STACK)); pthread_create(&_thread, &receiveloop_attr, MavlinkReceiver::start_trampoline, (void *)this);