Repository navigation
fix(serial): clean disconnect and more robust connection - #593
Conversation
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 98 |
| Duplication | 0 |
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.
b5dfcba to
2d4c4f1
Compare
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
2d4c4f1 to
3e8360f
Compare
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
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>
| // released writer | ||
| if(this.writer) { | ||
| await this.writer.releaseLock(); | ||
| const writer = this.writer; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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?
|
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. |
Follows #589 (merged).
Several problems seen on the bench when disconnecting and reconnecting,
most often right after a passthrough (ESCs read).
"Port initialization failed" after a reconnection
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.
(1 s at most) before closing the port. If the exit fails, the port is
still closed. Without a passthrough nothing changes.
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.
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.
"Disconnect" left the port open after the motor control was used
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.
"Closed port".
Two cmd_InterfaceExit failure lines in the debug log on every disconnect
stop the keep alive. Since 3220c06 (Commands keep retrying after the device is unplugged #579) a write to a closed port
throws, so this command failed and was logged. It now only
stops the keep alive.
Errors while the port closes
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.
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.