Fix serial connection reliability issues#2494
Merged
sensei-hacker merged 2 commits intoiNavFlight:maintenance-9.xfrom Dec 23, 2025
Merged
Fix serial connection reliability issues#2494sensei-hacker merged 2 commits intoiNavFlight:maintenance-9.xfrom
sensei-hacker merged 2 commits intoiNavFlight:maintenance-9.xfrom
Conversation
Two fixes for intermittent connection failures:
1. Fix error handler in serial.js to correctly report errors
- The error handler was resolving with {error: false} when there
was an actual error, causing confusing "connection canceled"
messages and preventing proper error handling
2. Reset MSP decoder state before adding receive listeners
- Move FC.resetState() earlier in the connection sequence
- Add MSP.disconnect_cleanup() to reset decoder state
- This ensures garbage bytes or boot messages received during
USB enumeration don't corrupt the MSP decoder state
Contributor
|
You are nearing your monthly Qodo Merge usage quota. For more information, please visit here. PR Compliance Guide 🔍All compliance sections have been disabled in the configurations. |
When serial port errors occurred, the port handle wasn't being properly cleaned up, leaving zombie handles that blocked reconnection attempts. Changes: - Add cleanup in error handler: removeAllListeners() + destroy() - Make connect() async to properly await cleanup of existing ports - Add 100ms delay after destroy() to let OS release file handle - Fix close() race condition: null reference before close callback - Handle case where port exists but isn't open This fixes the "Resource temporarily unavailable Cannot lock port" error that occurred when reconnecting after a connection failure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Summary
Fixes intermittent connection failures where the configurator would fail to connect to the flight controller with MSP timeouts or confusing "connection canceled" messages. It wouldn't reconnect until Configurator is closed and re-opened. This was due to leaking the file descriptor.
Changes
1. Fix error handler in serial.js
The error handler was resolving with
{error: false}when there was an actual error. This caused:Before:
resolve({error: false, id: this._id++});After:
resolve({error: true, msg: error.message || 'Serial port error'});2. Reset MSP decoder state before adding receive listeners
Moved
FC.resetState()earlier in the connection sequence and addedMSP.disconnect_cleanup()to reset the MSP decoder state. This ensures:Testing
Related Issues
Fixes regression introduced in PR #2476 (error handler bug)
PR Type
Bug fix
Description
Fix error handler incorrectly reporting success on serial port errors
Reset MSP decoder state before adding receive listeners to prevent corruption
Ensure clean decoder state (IDLE) before processing any received data
Add logging for serial port errors to aid debugging
Diagram Walkthrough
File Walkthrough
serial.js
Fix serial error handler to report errors correctlyjs/main/serial.js
error: trueinstead oferror:falsewhen serial port errors occurserial_backend.js
Reset MSP decoder state before listeners addedjs/serial_backend.js
FC.resetState()and addedMSP.disconnect_cleanup()earlier inconnection sequence
garbage bytes from corrupting decoder
data