Skip to content

UDP FIX - #2864

Open
error414 wants to merge 1 commit into
iNavFlight:maintenance-10.xfrom
error414:error414/fix/udp
Open

UDP FIX#2864
error414 wants to merge 1 commit into
iNavFlight:maintenance-10.xfrom
error414:error414/fix/udp

Conversation

@error414

@error414 error414 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

In INAV configurator UDP connection does not work at all, TCP works. This PR fix UDP connection,

@qodo-code-review

Copy link
Copy Markdown
Contributor

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Fix UDP connection by binding to an ephemeral local port

🐞 Bug fix 🕐 20-40 Minutes

Grey Divider

AI Description

• UDP connect bound the local socket to the remote device port, so it failed; now binds to port 0.
• Socket is created per connection and released on reconnect, error, or close, avoiding stale
 sockets.
• udpClose becomes an awaited IPC handler, and send rejects cleanly when no socket is open.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed main-process socket lifecycle, Electron IPC handlers, and renderer ConnectionUdp consumer jointly implement one bounded outcome: sending and receiving configurator traffic over UDP.
  • They are propagation hops of the same transport rather than independent capabilities.
Sources

js/main/main.js[379-380]

  ipcMain.handle('udpClose', (_event) => {
    return udp.close();
Behavioral complexity 🟡 Medium 🧩 Interacting branches, Coordinated invariants

  • Before: one module-global socket was created at import time, bound during connect, and reused.
  • After: each connect releases the prior socket, creates a tracked socket, binds asynchronously to port 0, and conditionally forwards events only while current.
  • New correctness condition: socket identity, bound state, connection metadata, asynchronous errors, send eligibility, and close completion must remain consistent across reconnect and close interleavings.
  • The IPC close change also changes disconnect from fire-and-forget notification to an awaited operation.
Sources

js/main/main.js[379-380]

  ipcMain.handle('udpClose', (_event) => {
    return udp.close();
Potential damage 🟡 Medium 🩹 Temporary loss

  • If the changed lifecycle or IPC behavior regresses, the configurator may fail to establish, maintain, send through, receive on, or close a UDP session.
  • The source shows no persistent configurator state or irreversible external mutation; lost datagrams would not be recoverable, but future communication can resume after a successful reconnect.
  • The stale-socket event checks contain messages and post-bind errors from sockets that are no longer current, but they do not establish containment for every possible lifecycle race.
  • Reconnecting or restarting the UDP session can restore future communication.
  • In-flight or dropped UDP datagrams are not replayed by this code, and no automatic repair or compensation path is shown.
Assessed commit 331a08c
Diagram

sequenceDiagram
    participant R as Renderer
    participant M as main.js IPC
    participant U as udp.js
    participant S as dgram socket
    participant D as UDP device
    R->>M: udpConnect(ip, port)
    M->>U: connect(ip, port)
    U->>S: createSocket and bind(0)
    S-->>U: bound
    U-->>R: id
    R->>M: udpSend(data)
    M->>U: send(data)
    U->>S: send to ip:port
    S->>D: datagram
    R->>M: udpClose
    M->>U: close()
    U->>S: close()
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Use dgram socket.connect(port, ip) for a connected UDP socket
  • ➕ The OS filters datagrams from other hosts and the socket can use send() without an explicit address
  • ➕ Removes the need to track _ip and _port separately
  • ➖ Surfaces ICMP errors as socket errors, which may need extra handling in the UI
  • ➖ Still needs an ephemeral local bind, so it does not remove the core fix
2. Keep one long-lived socket created at module load
  • ➕ Smaller diff and no per-connect socket churn
  • ➕ Matches the original structure
  • ➖ Cannot rebind to a new local port after a failed bind without recreating the socket
  • ➖ Retains the stale-state and error-routing issues this PR fixes

Recommendation: The PR's approach is sound: a fresh socket per connection, bound to port 0, fixes the root cause. Using socket.connect() would be a reasonable follow-up to simplify ip/port tracking, but it is not required for this fix. Reviewers should confirm that the renderer reads the renamed error field (msg instead of errorMsg) and that udpClose callers are updated to await the invoke result.

Files changed (2) +68 / -20

Bug fix (2) +68 / -20
main.jsMake udpClose an invoke handler that returns its result +2/-2

Make udpClose an invoke handler that returns its result

• Replaces ipcMain.on('udpClose') with ipcMain.handle and returns the promise from udp.close(). The renderer can now await the close and receive its success or error result.

js/main/main.js

udp.jsCreate socket per connection and bind to ephemeral port +66/-18

Create socket per connection and bind to ephemeral port

• The socket is created inside connect() instead of at module load, and bind uses port 0 so the OS picks a free local port. Previously the socket bound to the remote port, which blocked the connection. Stale sockets are released on reconnect and on error, and the bound flag routes pre-bind errors to the connect() result. close() now calls socket.close() instead of disconnect(), which throws on an unconnected socket, and clears ip/port. send() guards against a missing socket. The connect error field is renamed from errorMsg to msg.

js/main/udp.js

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Rapid reconnects can target the wrong peer 🐞 Bug ≡ Correctness
Description
connect() replaces the active socket, but its pending bind callback assigns _ip and _port
without checking whether that socket is still current. If another UDP connection starts before the
earlier bind callback runs, the earlier callback can report success and overwrite the endpoint used
by send() on the newer socket.
Code

js/main/udp.js[R42-45]

+                socket.bind(0, () => {
+                    bound = true;
+                    this._ip = ip;
+                    this._port = port;
Evidence
A new connect releases the previous socket and assigns a replacement, while the bind callback still
writes shared endpoint fields without a socket-identity check. Sending combines those shared fields
with whichever socket is current, and the IPC handler does not serialize connect requests.

js/main/udp.js[14-18]
js/main/udp.js[42-46]
js/main/udp.js[74-82]
js/main/main.js[371-373]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A pending UDP bind callback can report a superseded connection as successful and assign its endpoint to a newer socket. Replacing or closing a socket must also settle its pending connect request.

## Fix Focus Areas
- js/main/udp.js[10-49]
- js/main/udp.js[54-69]
- js/main/udp.js[88-104]

## Recommended Fix
Track each pending connect operation. When replacing or closing its socket, settle that operation with a failure result. In the bind callback, update the endpoint and report success only if its socket is still the active one; ensure cancellation and bind errors cannot settle the same operation twice.

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


Grey Divider

Context sources
✅ Compliance rules (platform): 15 rules
✅ Cross-repo context — repo relationships
Review mode: Auto: ⚖️ Balanced: UDP socket lifecycle and IPC behavior changes carry meaningful runtime risk.

Grey Divider

Tip of the day
💡 Did you know, you can see how risky a PR is at a glance with blast radius labels

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Configurator test build ready — commit 331a08c

Download build artifacts for PR #2864

Available platforms (scroll to the Artifacts section at the bottom of the run page):

  • Windows x64 (ZIP, MSI) and x32 (ZIP, MSI)
  • macOS arm64 (ZIP, DMG) and x64 (ZIP, DMG)
  • Linux x64 (DEB, RPM, ZIP) and aarch64 (DEB, RPM, ZIP)

A GitHub login is required to download artifacts. Build is for testing only.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant