Repository navigation
fix(serial): cleanly close serial port on disconnect when stream is locked (fixes #1437) #1480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,8 @@ export class TransportWebSerial implements Transport { | |
| private pipePromise: Promise<void> | null = null; | ||
| private abortController: AbortController; | ||
| private portReadable: ReadableStream<Uint8Array>; | ||
| private reader?: ReadableStreamDefaultReader<DeviceOutput>; | ||
| private readLoopPromise?: Promise<void>; | ||
|
|
||
| private lastStatus: DeviceStatusEnum = DeviceStatusEnum.DeviceDisconnected; | ||
| private closingByUser = false; | ||
|
|
@@ -296,38 +298,56 @@ export class TransportWebSerial implements Transport { | |
| }; | ||
| navigator.serial.addEventListener("disconnect", onOsDisconnect); | ||
|
|
||
| this.reader = reader; | ||
|
|
||
| log.debug("read loop starting"); | ||
| this.emitStatus(DeviceStatusEnum.DeviceConnected); | ||
|
|
||
| try { | ||
| while (true) { | ||
| const { value, done } = await reader.read(); | ||
| if (done) { | ||
| log.debug("read loop: done=true"); | ||
| break; | ||
| this.readLoopPromise = (async () => { | ||
| try { | ||
| while (true) { | ||
| const { value, done } = await reader.read(); | ||
| if (done) { | ||
| log.debug("read loop: done=true"); | ||
| break; | ||
| } | ||
| ctrl.enqueue(value); | ||
| } | ||
| ctrl.enqueue(value); | ||
| } | ||
| ctrl.close(); | ||
| } catch (error) { | ||
| const e = error as Error; | ||
| log.warn("read loop threw", { | ||
| closingByUser: this.closingByUser, | ||
| name: e?.name, | ||
| message: e?.message, | ||
| }); | ||
| if (!this.closingByUser) { | ||
| this.emitStatus(DeviceStatusEnum.DeviceDisconnected, "read-error"); | ||
| try { | ||
| ctrl.close(); | ||
| } catch {} | ||
| } catch (error) { | ||
| const e = error as Error; | ||
| log.warn("read loop threw", { | ||
| closingByUser: this.closingByUser, | ||
| name: e?.name, | ||
| message: e?.message, | ||
| }); | ||
| if (!this.closingByUser) { | ||
| this.emitStatus( | ||
| DeviceStatusEnum.DeviceDisconnected, | ||
| "read-error", | ||
| ); | ||
| } | ||
| try { | ||
| ctrl.error( | ||
| error instanceof Error ? error : new Error(String(error)), | ||
| ); | ||
| } catch {} | ||
| try { | ||
| await transformed.cancel(); | ||
| } catch {} | ||
| } finally { | ||
| try { | ||
| reader.releaseLock(); | ||
| } catch {} | ||
| this.reader = undefined; | ||
| navigator.serial.removeEventListener("disconnect", onOsDisconnect); | ||
| log.debug("read loop: released reader lock + listener"); | ||
| } | ||
| ctrl.error(error instanceof Error ? error : new Error(String(error))); | ||
| try { | ||
| await transformed.cancel(); | ||
| } catch {} | ||
| } finally { | ||
| reader.releaseLock(); | ||
| navigator.serial.removeEventListener("disconnect", onOsDisconnect); | ||
| log.debug("read loop: released reader lock + listener"); | ||
| } | ||
| })(); | ||
|
|
||
| await this.readLoopPromise; | ||
| }, | ||
| }); | ||
| } | ||
|
|
@@ -347,10 +367,14 @@ export class TransportWebSerial implements Transport { | |
| return; | ||
| } | ||
| this.lastStatus = next; | ||
| this.fromDeviceController?.enqueue({ | ||
| type: "status", | ||
| data: { status: next, reason }, | ||
| }); | ||
| try { | ||
| this.fromDeviceController?.enqueue({ | ||
| type: "status", | ||
| data: { status: next, reason }, | ||
| }); | ||
| } catch { | ||
| log.debug("emitStatus: controller already closed"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -361,6 +385,9 @@ export class TransportWebSerial implements Transport { | |
| try { | ||
| this.closingByUser = true; | ||
|
|
||
| // Emit status while the controller is open so subscribers receive the disconnect event | ||
| this.emitStatus(DeviceStatusEnum.DeviceDisconnected, "user"); | ||
|
|
||
| // Stop outbound piping | ||
| this.abortController.abort(); | ||
| log.debug("disconnect: aborted toDevice pipe"); | ||
|
|
@@ -369,8 +396,28 @@ export class TransportWebSerial implements Transport { | |
| log.debug("disconnect: pipePromise settled"); | ||
| } | ||
|
|
||
| // Cancel any remaining streams | ||
| if (this._fromDevice?.locked) { | ||
| // Stop inbound reading by cancelling the reader and waiting for the loop to exit | ||
| if (this.reader) { | ||
| try { | ||
| await this.reader.cancel(); | ||
| log.debug("disconnect: cancelled reader"); | ||
| } catch (e) { | ||
| const err = e as Error; | ||
| log.warn("disconnect: reader.cancel() threw", { | ||
| name: err?.name, | ||
| message: err?.message, | ||
| }); | ||
| } | ||
| } | ||
| if (this.readLoopPromise) { | ||
| try { | ||
| await this.readLoopPromise; | ||
| log.debug("disconnect: readLoopPromise settled"); | ||
| } catch {} | ||
| } | ||
|
Comment on lines
+412
to
+417
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis
Await the inbound pipe before closing the serial port.
Store the inbound pipe promise and await it after Suggested fix
🤖 Prompt for AI Agents
|
||
|
|
||
| // Cancel any remaining streams only if NOT locked (spec throws TypeError if cancelled while locked) | ||
| if (this._fromDevice && !this._fromDevice.locked) { | ||
| try { | ||
| await this._fromDevice.cancel(); | ||
| log.debug("disconnect: cancelled fromDevice"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: meshtastic/web
Length of output: 5085
🏁 Script executed:
Repository: meshtastic/web
Length of output: 12767
Cancel the reader on the read-error path.
When
ctrl.enqueue(value)throws,readerstill lockstransformed, sotransformed.cancel()rejects withTypeError. The empty catch suppresses that error. Thefinallyblock then releases the lock and clearsthis.readerwithout cancellingtransformed.disconnect()has no later path that cancelstransformed: it can cancelthis.readeronly while it exists, and_fromDevice.cancel()does not cancel the locked pipe. The pipe can therefore keepportReadablelocked, causingconnection.close()to fail.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents