Skip to content

Commit a4d78c8

Browse files
committed
fix(openlist):Add error handling and stability improvements for OpenList service
- Add try-catch blocks around OpenList startup/shutdown operations - Implement global uncaught exception handler for JNI/native crashes - Add synchronization and duplicate startup checks in OpenList.startup() - Ensure data directory creation before service initialization - Add proper error logging and user feedback for service failures - Set foregroundServiceType to dataSync for Android compliance
1 parent d910dc4 commit a4d78c8

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<service
7676
android:name=".OpenListService"
7777
android:exported="true"
78+
android:foregroundServiceType="dataSync"
7879
tools:ignore="ExportedService" />
7980

8081
<receiver

android/app/src/main/kotlin/com/github/openlistteam/openlistflutter/App.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,21 @@ class App : FlutterApplication() {
1616
super.onCreate()
1717

1818
app = this
19+
20+
// 设置全局异常处理器来捕获未处理的异常
21+
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
22+
android.util.Log.e("App", "Uncaught exception in thread ${thread.name}", throwable)
23+
24+
// 如果是 JNI 相关的错误,记录详细信息
25+
if (throwable.message?.contains("JNI") == true ||
26+
throwable.message?.contains("native") == true ||
27+
throwable is UnsatisfiedLinkError) {
28+
android.util.Log.e("App", "Native/JNI related crash detected")
29+
}
30+
31+
// 调用默认的异常处理器
32+
val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
33+
defaultHandler?.uncaughtException(thread, throwable)
34+
}
1935
}
2036
}

android/app/src/main/kotlin/com/github/openlistteam/openlistflutter/OpenListService.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,46 @@ class OpenListService : Service(), OpenList.Listener {
125125
if (isRunning) {
126126
// 关闭操作也放到子线程中执行,避免阻塞主线程
127127
mScope.launch(Dispatchers.IO) {
128-
OpenList.shutdown()
128+
try {
129+
OpenList.shutdown()
130+
} catch (e: Exception) {
131+
android.util.Log.e(TAG, "Shutdown error", e)
132+
launch(Dispatchers.Main) {
133+
toast("关闭失败: ${e.message}")
134+
}
135+
}
129136
}
130137
} else {
131138
toast(getString(R.string.starting))
132139
isRunning = true
133140
// 在子线程中启动OpenList服务,避免阻塞主线程
134141
mScope.launch(Dispatchers.IO) {
135142
try {
143+
// 确保在启动前进行初始化
144+
OpenList.init()
145+
// 添加延迟确保初始化完成
146+
kotlinx.coroutines.delay(100)
136147
OpenList.startup()
137148
// 启动完成后在主线程中更新状态
138149
launch(Dispatchers.Main) {
139150
notifyStatusChanged()
140151
}
141152
} catch (e: Exception) {
153+
android.util.Log.e(TAG, "Startup error", e)
142154
// 启动失败时重置状态
143155
isRunning = false
144156
launch(Dispatchers.Main) {
145157
toast("启动失败: ${e.message}")
146158
notifyStatusChanged()
147159
}
160+
} catch (t: Throwable) {
161+
android.util.Log.e(TAG, "Startup fatal error", t)
162+
// 处理更严重的错误(如 JNI 崩溃)
163+
isRunning = false
164+
launch(Dispatchers.Main) {
165+
toast("启动严重错误: ${t.message}")
166+
notifyStatusChanged()
167+
}
148168
}
149169
}
150170
}

android/app/src/main/kotlin/com/github/openlistteam/openlistflutter/model/openlist/OpenList.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,36 @@ object OpenList : Event, LogCallback {
9494
}
9595

9696
@SuppressLint("SdCardPath")
97+
@Synchronized
9798
fun startup() {
9899
Log.d(TAG, "startup: $dataDir")
99-
init()
100-
Openlistlib.start()
100+
try {
101+
// 确保数据目录存在
102+
val dataDirFile = File(dataDir)
103+
if (!dataDirFile.exists()) {
104+
dataDirFile.mkdirs()
105+
Log.d(TAG, "Created data directory: $dataDir")
106+
}
107+
108+
// 重新初始化以确保配置正确
109+
init()
110+
111+
// 检查是否已经在运行
112+
if (isRunning()) {
113+
Log.w(TAG, "OpenList is already running")
114+
return
115+
}
116+
117+
Log.d(TAG, "Starting OpenList...")
118+
Openlistlib.start()
119+
Log.d(TAG, "OpenList started successfully")
120+
} catch (e: Exception) {
121+
Log.e(TAG, "Failed to start OpenList", e)
122+
throw e
123+
} catch (t: Throwable) {
124+
Log.e(TAG, "Fatal error starting OpenList", t)
125+
throw RuntimeException("Fatal error starting OpenList", t)
126+
}
101127
}
102128

103129
fun getHttpPort(): Int {

0 commit comments

Comments
 (0)