From 764b20c32aff20a11d715d46a049d1adde56c361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20=C5=A0=C4=87eki=C4=87?= Date: Fri, 13 Nov 2026 12:49:53 +0100 Subject: [PATCH] fix(terminal): report a closed transport once Writes through a closed terminal view re-ran the close path, so every later keystroke emitted another onClosed event. Both platforms now return early once the transport is closed, and closeTransport emits only on the transition; a rebound source starts a new cycle. --- README.md | 5 +++-- .../terminal/ReactNativeSshTerminalView.kt | 16 ++++++++-------- ios/Terminal/ReactNativeSshTerminalView.swift | 10 ++++++---- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 14be5a0..d03af92 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,9 @@ npx expo run:android Shell input and resize requests share one ordered queue. Shutdown stops accepting new input immediately, and remote closure stops the writer. A closed channel -returns `ChannelClosed`; transport failures and writes through a closed or -unbound Android terminal are reported through `onClosed` with an error message. +returns `ChannelClosed`; a write through an unbound terminal is reported through +`onClosed`. A transport reports `onClosed` exactly once: later writes are dropped +silently and a failing resize adds no further event, until a new source is bound. The SSH integration tests run only when `SSH_TEST_HOST` is set. The `shell_preserves_rapid_single_byte_writes` test sends 16,384 individual writes, diff --git a/android/src/main/java/ai/kilo/reactnativessh/terminal/ReactNativeSshTerminalView.kt b/android/src/main/java/ai/kilo/reactnativessh/terminal/ReactNativeSshTerminalView.kt index c4a9a57..f3124c7 100644 --- a/android/src/main/java/ai/kilo/reactnativessh/terminal/ReactNativeSshTerminalView.kt +++ b/android/src/main/java/ai/kilo/reactnativessh/terminal/ReactNativeSshTerminalView.kt @@ -45,10 +45,11 @@ class ReactNativeSshTerminalView(context: Context, appContext: AppContext) : Exp override fun onClosed(exitStatus: Int?, error: String?) { mainHandler.post { - if (!transportClosed) { - transportClosed = true - session.notifyTransportClosed(exitStatus ?: 0, error) - } + // One transport closes once: the notice and the event are skipped for + // every later close report, and a rebound source starts a new cycle. + if (transportClosed) return@post + transportClosed = true + session.notifyTransportClosed(exitStatus ?: 0, error) TerminalViewEvents.emit(this@ReactNativeSshTerminalView, "onClosed", Bundle().apply { putInt("exitStatus", exitStatus ?: 0) putString("error", error) @@ -59,10 +60,9 @@ class ReactNativeSshTerminalView(context: Context, appContext: AppContext) : Exp private val transport = object : TerminalTransport { override fun write(data: ByteArray, offset: Int, count: Int) { - if (transportClosed) { - sink.onClosed(null, "terminal source is closed") - return - } + // A closed transport stays closed until a new source is bound: later writes + // are dropped here, because the close notice and event already went out. + if (transportClosed) return val target = source if (target == null) { sink.onClosed(null, "terminal source '${sourceId.orEmpty()}' is not bound") diff --git a/ios/Terminal/ReactNativeSshTerminalView.swift b/ios/Terminal/ReactNativeSshTerminalView.swift index 8836df0..8225df9 100644 --- a/ios/Terminal/ReactNativeSshTerminalView.swift +++ b/ios/Terminal/ReactNativeSshTerminalView.swift @@ -246,8 +246,9 @@ public final class ReactNativeSshTerminalView: ExpoView, TerminalViewDelegate, T } private func write(_ bytes: Data) { + // A closed transport stays closed until a new source is bound: later writes + // are dropped here, because the close notice and event already went out. if transportClosed { - closeTransport(exitStatus: nil, error: "terminal source is closed") return } guard let source else { @@ -258,10 +259,11 @@ public final class ReactNativeSshTerminalView: ExpoView, TerminalViewDelegate, T } private func closeTransport(exitStatus: Int?, error: String?) { - if !transportClosed { - transportClosed = true - feedCloseNotice(exitStatus: exitStatus, error: error) + guard !transportClosed else { + return } + transportClosed = true + feedCloseNotice(exitStatus: exitStatus, error: error) let errorValue: Any = error.map { $0 as Any } ?? NSNull() onClosed(["exitStatus": exitStatus ?? 0, "error": errorValue]) }