Skip to content

Commit a0ce766

Browse files
committed
The executor may be wrongly shutdown, which is fixed
1 parent e0e1cbb commit a0ce766

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId = "com.bhznjns.inputsharereporter"
1212
minSdk = 24
1313
targetSdk = 34
14-
versionCode = 4
15-
versionName = "1.1.2"
14+
versionCode = 5
15+
versionName = "1.1.3"
1616

1717
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1818
}

app/src/main/java/com/bhznjns/inputsharereporter/ReporterServer.kt

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ReporterServer : Service() {
4545
serverSocket = LocalServerSocket(ABSTRACT_SOCKET_NAME)
4646
Log.i("ReporterServer", "Server started on abstract address: localabstract:$ABSTRACT_SOCKET_NAME")
4747

48-
// Accept client connection (this is a blocking call)
4948
clientSocket = serverSocket?.accept()
5049
Log.i("ReporterServer", "Client connected")
5150
outputStream = clientSocket?.outputStream
@@ -83,9 +82,6 @@ class ReporterServer : Service() {
8382
if (outputStream == null || clientSocket == null || !clientSocket!!.isConnected) {
8483
Log.e("ReporterServer", "Client is not connected or socket is closed.")
8584
// If sending fails, assume connection is lost and stop/retry server
86-
if (isRunning.get()) {
87-
stopServer(true)
88-
}
8985
return false
9086
}
9187

@@ -95,16 +91,10 @@ class ReporterServer : Service() {
9591
} catch (e: IOException) {
9692
// Catch IOException for write/flush errors, indicating connection issue
9793
Log.e("ReporterServer", "Server sending data error: ${e.message}")
98-
if (isRunning.get()) {
99-
stopServer(true) // Retry connection on send error
100-
}
10194
return false
10295
} catch (e: Exception) {
10396
// Catch other potential exceptions
10497
Log.e("ReporterServer", "Unexpected sending data error: ${e.message}")
105-
if (isRunning.get()) {
106-
stopServer(true) // Retry connection on send error
107-
}
10898
return false
10999
}
110100
return true
@@ -124,11 +114,25 @@ class ReporterServer : Service() {
124114
break
125115
}
126116
}
117+
stopServer(true)
127118
Log.d("ReporterServer", "Heartbeat loop finished.")
128119
}
129120
}
130121

131122
private fun stopServer(retry: Boolean) {
123+
fun stopExecutor() {
124+
executor.shutdownNow()
125+
try {
126+
// Wait a bit for tasks to terminate
127+
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
128+
Log.w("ReporterServer", "Executor did not terminate in time.")
129+
}
130+
} catch (e: InterruptedException) {
131+
Log.e("ReporterServer", "Interrupted while waiting for executor termination.")
132+
Thread.currentThread().interrupt() // Restore interrupt flag
133+
}
134+
}
135+
132136
// Use compareAndSet to ensure only one thread stops the server
133137
if (!isRunning.compareAndSet(true, false)) {
134138
Log.w("ReporterServer", "Server is already stopping or stopped.")
@@ -138,6 +142,7 @@ class ReporterServer : Service() {
138142
Log.i("ReporterServer", "Attempting to stop server...")
139143
try {
140144
// Closing LocalSocket will likely cause read/write operations on it to throw IOException
145+
outputStream?.close()
141146
clientSocket?.close()
142147
serverSocket?.close()
143148
Log.i("ReporterServer", "Server socket resources closed.")
@@ -151,27 +156,18 @@ class ReporterServer : Service() {
151156
outputStream = null
152157
}
153158

154-
// Shutdown executor gracefully
155-
executor.shutdownNow()
156-
try {
157-
// Wait a bit for tasks to terminate
158-
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
159-
Log.w("ReporterServer", "Executor did not terminate in time.")
160-
}
161-
} catch (e: InterruptedException) {
162-
Log.e("ReporterServer", "Interrupted while waiting for executor termination.")
163-
Thread.currentThread().interrupt() // Restore interrupt flag
159+
if (!retry) {
160+
stopExecutor()
161+
Log.i("ReporterServer", "Server fully stopped (no retry).")
162+
return
164163
}
165164

166-
if (!retry) Log.i("ReporterServer", "Server fully stopped (no retry).")
167-
// when need to reconnect, show the disconnected message
168165
uiHandler.post { Toast.makeText(this, I18n.choose(listOf(
169166
"PC client disconnected.",
170167
"电脑端已断开连接。",
171168
)), Toast.LENGTH_SHORT).show() }
172-
173169
uiHandler.postDelayed({
174-
Log.i("ReporterServer", "Attempting to restart server after delay...")
170+
Log.i("ReporterServer", "Attempting to restart server...")
175171
startServer()
176172
}, RETRY_INTERVAL_MS)
177173
}
@@ -193,7 +189,8 @@ class ReporterServer : Service() {
193189
inner class LocalBinder : Binder() {
194190
fun sendEvent(event: Int) {
195191
executor.execute {
196-
this@ReporterServer.sendEvent(event)
192+
val ret = this@ReporterServer.sendEvent(event)
193+
if (!ret) stopServer(true)
197194
}
198195
}
199196
}

0 commit comments

Comments
 (0)