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.
This commit is contained in:
gguidone authored and Matthias Grob committed 2026-08-05 09:25:08 +02:00
1 parent ee99864808
commit 61b7efcbc3
1 file changed
+1 -2
+1 -2
View File
@@ -4125,8 +4125,7 @@ void MavlinkReceiver::start()
param.sched_priority = SCHED_PRIORITY_MAX - 80;
(void)pthread_attr_setschedparam(&receiveloop_attr, &param);
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);