防止被多次初始化

(cherry picked from commit 77f1a8b30d2c30c03f0ec45fc32fe8eef9d2b4c7)
This commit is contained in:
Rust 2025-10-29 15:14:16 +08:00 committed by speakeloudest
parent 4250f61345
commit fbdf4a2337
2 changed files with 23 additions and 0 deletions

View File

@ -99,6 +99,9 @@ class KRSingBoxImp {
/// Stream
final List<StreamSubscription<dynamic>> _kr_subscriptions = [];
///
bool _kr_isInitialized = false;
///
bool get kr_isProxyReady => kr_status.value is SingboxStarted;
@ -131,6 +134,12 @@ class KRSingBoxImp {
///
Future<void> init() async {
//
if (_kr_isInitialized) {
KRLogUtil.kr_i('SingBox 已经初始化,跳过重复初始化', tag: 'SingBox');
return;
}
try {
if (_initialized) {
KRLogUtil.kr_i('SingBox 已经初始化,跳过重复初始化');
@ -249,9 +258,12 @@ class KRSingBoxImp {
});
KRLogUtil.kr_i('SingBox 初始化完成');
_kr_isInitialized = true;
} catch (e, stackTrace) {
KRLogUtil.kr_e('SingBox 初始化失败: $e');
KRLogUtil.kr_e('错误堆栈: $stackTrace');
//
_kr_isInitialized = false;
rethrow;
}
}

View File

@ -31,6 +31,9 @@ class FFISingboxService with InfraLogger implements SingboxService {
late final ReceivePort _statusReceiver;
Stream<SingboxStats>? _serviceStatsStream;
Stream<List<SingboxOutboundGroup>>? _outboundsStream;
///
bool _isInitialized = false;
static SingboxNativeLibrary _gen() {
String fullPath = "";
@ -52,6 +55,12 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
Future<void> init() async {
//
if (_isInitialized) {
loggy.debug("already initialized, skipping");
return;
}
loggy.debug("initializing");
_statusReceiver = ReceivePort('service status receiver');
final source = _statusReceiver
@ -62,6 +71,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
source,
const SingboxStopped(),
).autoConnect();
_isInitialized = true;
}
@override