Skip to content

fix(serial): clean disconnect and more robust connection - #593

Merged
stylesuxx merged 12 commits into
stylesuxx:developfrom
Jschmiedd:fix/connection-cleanup
Nov 19, 2026
Merged

stylesuxx merged 12 commits into
stylesuxx:developfrom
Jschmiedd:fix/connection-cleanup

Conversation

@Jschmiedd

@Jschmiedd Jschmiedd commented Nov 11, 2026 •

Copy link
Copy Markdown

Follows #589 (merged).

Several problems seen on the bench when disconnecting and reconnecting,
most often right after a passthrough (ESCs read).

  1. "Port initialization failed" after a reconnection

    • "Disconnect" sent the 4-way exit and closed the port without reading
      the flight controller's answer. That answer came out on the next
      connection (10 reconnections out of 10, measured with the time of every
      byte sent and received), and the flight controller's first answer
      came late, up to about 2 s. A single unanswered API version request
      was mistaken for a flight controller left in passthrough.
    • After a passthrough, "Disconnect" now reads the answer to the exit
      (1 s at most) before closing the port. If the exit fails, the port is
      still closed. Without a passthrough nothing changes.
    • Every connection request (API version, then variant, version, build
      info, board info, UID, motor data and features) is now asked again
      after a timeout, up to 5 times in all, 250 ms apart, before the
      flight controller is assumed to be in passthrough or the connection
      is given up. A flight controller that answers at once is not delayed.
    • The command queue is cleared when the port opens and closes; an
      answer for a command dropped in the meantime is ignored. Only the
      answer to the MSP command sent is taken: a late answer to another MSP
      command is skipped.
      Bench: before, 2 failures out of 16 connections. With the answer to the
      exit read, no late answer left (0 out of 7). The flight controller still
      stays silent for a moment after some reconnections, on a healthy board
      too, with TRUEGLIDE (4 out of 11) and with stock Betaflight 25.12.2
      (2 out of 10). Silences were only seen after a passthrough (none in 11
      connections without one); why the flight controller is silent is not
      known. Before this change, which asks every request again, a silence
      after the API version made the connection fail (2 out of 27). With
      this PR: no failure in 8 reconnections, 1 to 3 s apart, on my test
      board; that rarer case did not happen on the bench and is covered by
      two tests.
  2. "Disconnect" left the port open after the motor control was used

    • If "Enable motor control" was ticked before the ESCs were read,
      close() stopped the reader, then asked for the features to stop the
      motors, just after the passthrough. The request timed out, and
      close() threw before the port was closed: reconnecting failed with
      "Port already in use by another application" (seen on the bench).
      The motor stop is now sent while the reader still runs and its
      failure is caught. The motors are marked as stopped when entering
      the passthrough and after a successful stop.
    • "Disconnect" now waits for the port to be closed before logging
      "Closed port".
  3. Two cmd_InterfaceExit failure lines in the debug log on every disconnect

  4. Errors while the port closes

    • The writer was released but still set: a late battery state request
      wrote to it ("writer has been released"). It is now cleared before
      being released, and a battery state without an answer is no longer
      left uncaught.
    • An MSP command cancelled by the port closing is no longer logged as a
      failure.

Tests: 19 new tests in App, FourWay, Msp, QueueProcessor and Serial. 17 fail
on develop and pass here; the other 2 check that the default behaviour
does not change. Waiting for the port to close in "Disconnect"
and the battery state change have no automated test. yarn test and yarn
lint pass (Node 18). Checked on the bench, no props.

@codacy-production

codacy-production Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 98 complexity · 0 duplication

Metric Results
Complexity 98
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Several small problems seen when connecting and disconnecting, most often
after a passthrough or a failed flash:

- Commands still waiting when the port closes timed out later, against a
  closed port, and data received before a reconnection could be handed to
  the next command. The queue now drops every waiting command and the
  data received so far when the port opens or closes (clear()).
- After a passthrough the flight controller stops the motor outputs and no
  longer answers MSP, but closing the port still sent a motor stop, which
  only timed out ("Timed out" in the log right after "Port closed"). The
  motors are now marked as stopped when entering the passthrough, and the
  stop is sent while the reader still runs. Without a passthrough the
  motors are still stopped.
- A late answer to an earlier MSP command (for example after a time out)
  could be taken as the answer to the next command. Only the answer to the
  command sent is now taken; a command cancelled by the port closing is no
  longer logged as a failure.
- A single late answer to the API version request, just after a
  reconnection, was taken for a flight controller left in passthrough. The
  request is now asked up to 3 times, 250 ms apart, on a time out.
- "Disconnect" did not wait for the port to be closed before logging
  "Port closed".

Tests: the queue rejects the current and the waiting commands at once and
keeps no old data; a late MSP answer to another command is skipped; a
cancelled command is not logged; no motor stop is sent after a
passthrough, and it still is without one.

The two App changes (API version asked again, disconnect waiting for the
port) have no automated test in this PR, since App is not exported for
unit tests here; they were checked on the bench.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
@Jschmiedd
Jschmiedd force-pushed the fix/connection-cleanup branch from 2d4c4f1 to 3e8360f Compare November 11, 2026 21:33
Jschmiedd and others added 6 commits November 12, 2026 12:01
Once the port is gone, disconnect() still sent the 4 way exit command,
only to stop the keep alive. The command can not be written any more, so
every "Disconnect" logged "cmd_InterfaceExit failed: Serial port is
closed" and "Failed processing command cmd_InterfaceExit after 10
retries". disconnect() now only stops the keep alive (FourWay.stop()).
The exit command is still sent by "Disconnect" before the port closes.

Test: disconnect() stops the keep alive and sends no exit command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
On the bench, reconnecting 1 to 2 s after "Disconnect" left the flight
controller silent for a moment: 1 or 2 API version requests without an
answer most of the time (always caught by the 3 attempts), and once 3
requests in a row (about 4 s), so the connection failed with "Port
initialization failed". Reconnecting after 4 s or more, the flight
controller always answered at once.

Up to 5 attempts (about 6 s) cover the longest silence seen. A flight
controller that answers at once is not delayed.

Tests: a flight controller silent for 4 requests is waited for; the
connection is given up after 5 requests without an answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
…he port

Measured on the bench (time of every byte sent and received after the
port opens):

- After a passthrough, the answer of the flight controller to
  cmd_InterfaceExit was read on the next connection, 1 to 2 ms after the
  port opened (11 connections out of 11): "Disconnect" closed the port
  without reading it. Reconnecting at once, the flight controller then
  answered late (up to about 2 s, 2 connections out of 11).
- Without a passthrough (ESCs not read), no late answer at all (11
  connections out of 11, every answer within 14 to 23 ms).

"Disconnect" now reads the answer to the exit when the flight controller
is in passthrough (1 s at most) before closing the port. Without a
passthrough nothing changes.

Also seen on the bench: the battery state, asked every second, could be
written to the released writer while the port closed ("This writable
stream writer has been released"), an uncaught error. The writer is now
dropped before it is released, so a late command is refused as for a
closed port, and the battery state no longer leaves a missing answer
uncaught.

Tests: the exit answer is awaited only after a passthrough and the port
closes even without an answer; a command sent while the port closes is
refused with PortClosedError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
On develop, when the motor control was enabled before the ESCs were read,
"Disconnect" asked for the features to stop the motors after the reader
was stopped. The request timed out, close() threw before the port was
closed, and reconnecting failed with "Port already in use" (seen on the
bench). close() now catches this failure; this test checks that the
reader, the writer and the port are still released and closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
The comment said the flight controller was seen silent for up to about
4 s. What the bench showed is a connection that failed after 3 requests
without an answer (about 3.5 s); the length of that silence could not be
measured. The comment now says so, and that 5 requests cover about 6 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
…r is silent

Just after a passthrough, the flight controller may be silent for a
moment on reconnection: on the bench, 4 of 11 reconnections on a healthy
board with TrueGlide and 2 of 10 with Betaflight 25.12.2. Only the API
version was asked again; a silence on one of the following requests
(variant, version, build info, board info, UID, motor data, features)
made the connection fail with "Port initialization failed" (2 of 27
reconnections in an earlier bench run).

Every connection request now goes through the same retry as the API
version: up to 5 attempts, 250 ms apart, on a time out only. A flight
controller that answers at once is not delayed.

Tests: a flight controller silent twice after the API version is waited
for; the connection is given up after 5 unanswered requests. Both fail
before this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCt5QANL2A4NKbRdzyx
@Jschmiedd
Jschmiedd marked this pull request as ready for review November 13, 2026 22:16
claude and others added 3 commits November 14, 2026 18:30
As asked in the review of stylesuxx#590 and stylesuxx#592: no comment where the code is
explicit enough.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCsXEtnF7V6bZuqbME7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvqUCsXEtnF7V6bZuqbME7
Co-Authored-By: Claude <noreply@anthropic.com>
Comment thread src/utils/Serial.js Outdated
// released writer
if(this.writer) {
await this.writer.releaseLock();
const writer = this.writer;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The assignment does not really protect anything, nicer to read would be:

await this.writer.releaseLock();
this.writer = null

A bit ambivalent regarding the comment, not sure it is really needed here, code is pretty self explanatory.


try {
apiVersion = await this.serial.getApiVersion();
apiVersion = await this.getApiVersion();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The passthrough recovery a few lines below still uses the old behaviour.

Line 293 calls exitFourWayInterface() without waiting for the answer, and line 296 calls this.serial.getApiVersion() only once. That's the same "silent right after leaving passthrough" case. Can you please use exitFourWayInterface(true) and this.getApiVersion() there too?

@Jschmiedd

Copy link
Copy Markdown
Author

Thanks for the review,

both points are done: exitFourWayInterface(true) and this.getApiVersion() in the passthrough recovery, and the writer is released, then cleared, without the extra variable and comment.

@stylesuxx
stylesuxx merged commit 1a10e7c into stylesuxx:develop Nov 19, 2026
3 checks passed
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.

3 participants