ValidateCommandSequence error message exceeds gRPC metadata limit on large command lists
Was looking through the shutdown path after a crash report (NETDATA-AGENT-43R) and realized the thread cleanup has a nasty race.
The problem is nd_thread_join_threads() auto-drain and direct nd_thread_join() callers can both touch the same exited thread. The auto-drain pulls it off the exited list and calls nd_thread_join(), which frees the ND_THREAD struct. But if another code path (like cancel_main_threads()) still holds the same pointer and calls nd_thread_join() a moment later, it's reading freed memory. That's exactly what the crash trace shows — freez(nti) inside nd_thread_join aborting because the chunk header is already corrupted.
The CAS guard on NETDATA_THREAD_STATUS_JOINED only stops two threads from joining the same struct simultaneously. It doesn't help when one caller already freed it and another still has a stale pointer.
Fix: add a refcount to ND_THREAD.
- Starts at 1 on creation.
nd_thread_join_threads()bumps it when it pulls a thread from the exited list, so the auto-drain path holds a reference.nd_thread_join()decrements it. Whoever reaches zero frees the struct.
This way both paths can safely call nd_thread_join() and the struct stays alive until the last one finishes.
Also added a cmocka test covering:
- refcount starts at 1
- join frees when refcount hits zero
- auto-drain path works correctly
- concurrent join paths don't crash
- double-join from same path is still safely rejected by the JOINED flag
Fixes #22716
Closing this PR because it is no longer reviewable in its current form. The description refers to an unrelated Netdata thread-lifecycle bug and a non-existent Temporal issue (#22716), while the actual patch changes command-sequence error handling. After repeated merges from main, the diff also rewrites/deletes hundreds of unrelated test lines. A future fix should be recreated from a clean branch with a matching description and a focused test-only diff.