Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/mavlink/src/main/cpp/mavlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void *listen(int mavlink_port) {
if (bind(fd, (struct sockaddr *) (&addr), sizeof(addr)) != 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Unable to bind MavLink port %d: %s",
mavlink_port, strerror(errno));
close(fd);
return 0;
}

Expand All @@ -88,6 +89,7 @@ void *listen(int mavlink_port) {
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG,
"Unable to bind MavLink rx timeout: %s", strerror(errno));
close(fd);
return 0;
}

Expand All @@ -102,11 +104,13 @@ void *listen(int mavlink_port) {
continue;
} else {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Error receiving mavlink: %s", strerror(errno));
close(fd);
return 0;
}
} else if (ret == 0) {
// peer has done an orderly shutdown
__android_log_print(ANDROID_LOG_ERROR, TAG, "Shutting down mavlink: ret=0");
close(fd);
return 0;
}

Expand Down Expand Up @@ -315,6 +319,7 @@ void *listen(int mavlink_port) {
usleep(1);
}

close(fd);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Mavlink thread done.");
return 0;
}
Expand Down Expand Up @@ -375,6 +380,11 @@ Java_com_openipc_mavlink_MavlinkNative_nativeCallBack(JNIEnv *env, jclass clazz,
extern "C"
JNIEXPORT void JNICALL
Java_com_openipc_mavlink_MavlinkNative_nativeStart(JNIEnv *env, jclass clazz, jobject context) {
// mavlink_thread_signal is how a previous listen() loop was told to exit;
// it has to be cleared here or a restart (e.g. toggling streaming mode off
// again) would see it already set and exit its loop before ever reading a
// packet.
mavlink_thread_signal = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Quick restarts can stop telemetry 🐞 Bug ☼ Reliability

nativeStart clears mavlink_thread_signal and launches another detached listener without waiting
for the previous listener to close its socket. If the old thread has observed the stop signal but
has not yet closed the descriptor, the new thread can fail to bind before the old one exits, leaving
no thread receiving telemetry.
Agent Prompt
## Issue description
A restart can reset the shared stop signal and launch a new listener before the previous detached listener has closed its bound socket. Retain and synchronize ownership of the listener thread so starting waits for prior shutdown, and use an atomic or otherwise synchronized stop state.

## Issue Context
The listener may take up to the receive timeout to observe a stop request. The replacement thread can therefore encounter the still-bound port and exit immediately, followed by the previous thread closing its socket and leaving no active listener.

## Fix Focus Areas
- app/mavlink/src/main/cpp/mavlink.cpp[57-57]
- app/mavlink/src/main/cpp/mavlink.cpp[85-104]
- app/mavlink/src/main/cpp/mavlink.cpp[322-324]
- app/mavlink/src/main/cpp/mavlink.cpp[382-397]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

auto threadFunction = []() {
listen(14550);
};
Expand Down