fix: 复用 URLSession 避免 webSocketTask 抛 NSException + 全抛点桥接
影响: XType 在 ~8 分钟级会再次 SIGSEGV(crash 4F17E81B,
EXC_BAD_ACCESS 0xD,栈顶 webSocketTaskForRequest → objc_exception_throw
→ _Unwind_RaiseException);Swift 不接 ObjC NSException,任何抛 NSException
的 ObjC API 调用都会让进程 SIGSEGV。架构脆弱性:installTap / webSocketTask
都撞过,需要把剩余可能抛点也桥接。
改动:
- XunfeiEngine session 字段改为 private lazy var,与实例同生命周期
(进程级,永不 invalidate,deinit 兜底);反复 invalidate/create 让
self delegate 同时关联 invalidated 旧 session + 新 session,
webSocketTask(with:) 抛 INVALIDATED_SESSION
- cleanup() 不再 invalidateAndCancel session,只 cancel 旧 wsTask
- ObjCBridge.h/.m 加通用 XTObjCRun(void (^)(void)) helper:
包任意 Swift block,@try/@catch NSException,失败 NSLog reason 返回 1
- XunfeiEngine.prepare() 把 webSocketTask(with:) 用 XTObjCRun 包
(belt-and-suspenders:lazy session 已避免 invalidated 状态污染,
但加一层通用兜底)
- AppleEngine.prepare() 把 SFSpeechAudioBufferRecognitionRequest()
init 用 XTObjCRun 包;同时把 tap 回调从 [req] capture 改成
[weak self] + self?.request?.append(buf) 防 req 已 endAudio
后还在 fire 回调
- AppleEngine.beginTask() 把 recognitionTask(with:) 用 XTObjCRun 包
设计: 不动 AVAudioEngine stored init(改造成本/收益不划算);
后续新引入的 ObjC API 都需要走 XTObjCRun 兜底。
Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
1 parent
d7695c651c
commit
dd0d1b4e74
3 files changed
+79
-30
No files matched your search
@@ -14,3 +14,8 @@ int XTObjCTryInstallTap(AVAudioNode * _Nonnull node,
|
||||
AVAudioFrameCount bufferSize,
|
||||
AVAudioFormat * _Nullable format,
|
||||
void (^ _Nonnull tapBlock)(AVAudioPCMBuffer * _Nonnull, AVAudioTime * _Nonnull));
|
||||
|
||||
// 通用 wrapper:把可能抛 NSException 的任意 Swift block 包起来,失败返回 1 + NSLog reason。
|
||||
// 用于 Swift 端给「未知是否抛 NSException 的 ObjC API 调用」(如 webSocketTask(with:)、recognitionTask(with:))
|
||||
// 加 belt-and-suspenders 保护。Swift 不能 catch ObjC NSException,只能靠 ObjC 桥接。
|
||||
int XTObjCRun(void (^_Nonnull block)(void));
|
||||
@@ -14,3 +14,14 @@ int XTObjCTryInstallTap(AVAudioNode *node,
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int XTObjCRun(void (^block)(void)) {
|
||||
@try {
|
||||
block();
|
||||
return 0;
|
||||
} @catch (NSException *e) {
|
||||
// 通用 wrapper:挡住任意可能抛 NSException 的 ObjC API 调用
|
||||
NSLog(@"XTObjC: NSException caught name=%@ reason=%@", e.name, e.reason);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
+63
-30
@@ -155,15 +155,24 @@ final class AppleEngine: ASREngine {
|
||||
func prepare() {
|
||||
text = ""
|
||||
finalSem = DispatchSemaphore(value: 0)
|
||||
let req = SFSpeechAudioBufferRecognitionRequest()
|
||||
req.shouldReportPartialResults = true
|
||||
req.addsPunctuation = true
|
||||
if recognizer?.supportsOnDeviceRecognition == true {
|
||||
req.requiresOnDeviceRecognition = true
|
||||
} else {
|
||||
log("apple: on-device unavailable → server fallback")
|
||||
// SFSpeechAudioBufferRecognitionRequest() 在 SFSpeech 内部状态不对时可能抛 NSException
|
||||
// (recognizer invalidated / 系统 Speech 权限被吊销 / Tcc 阻断)——
|
||||
// belt-and-suspenders 包通用桥接,失败 log + return(不置 prepared,后续 readyForBegin=false)
|
||||
let reqRC = XTObjCRun { [self] in
|
||||
let req = SFSpeechAudioBufferRecognitionRequest()
|
||||
req.shouldReportPartialResults = true
|
||||
req.addsPunctuation = true
|
||||
if self.recognizer?.supportsOnDeviceRecognition == true {
|
||||
req.requiresOnDeviceRecognition = true
|
||||
} else {
|
||||
log("apple: on-device unavailable → server fallback")
|
||||
}
|
||||
self.request = req
|
||||
}
|
||||
if reqRC != 0 {
|
||||
log("apple: SFSpeechAudioBufferRecognitionRequest raised NSException")
|
||||
return
|
||||
}
|
||||
request = req
|
||||
if useSharedTap {
|
||||
// dual 模式: PCM 由 XunfeiEngine 的 tap 回调转发过来,
|
||||
// 自身不装 tap 不启动 engine(避免和 xunfei 抢 bus 0)
|
||||
@@ -173,8 +182,8 @@ final class AppleEngine: ASREngine {
|
||||
let input = engine.inputNode
|
||||
let fmt = input.outputFormat(forBus: 0)
|
||||
input.removeTap(onBus: 0)
|
||||
input.installTap(onBus: 0, bufferSize: 2048, format: fmt) { [req] buf, _ in
|
||||
req.append(buf)
|
||||
input.installTap(onBus: 0, bufferSize: 2048, format: fmt) { [weak self] buf, _ in
|
||||
self?.request?.append(buf)
|
||||
}
|
||||
do { try engine.start() } catch { log("apple engine start failed: \(error)") }
|
||||
prepared = true
|
||||
@@ -182,14 +191,23 @@ final class AppleEngine: ASREngine {
|
||||
|
||||
func beginTask() {
|
||||
guard let req = request, let rec = recognizer, prepared else { return }
|
||||
task = rec.recognitionTask(with: req) { [weak self] r, err in
|
||||
guard let self = self else { return }
|
||||
if let r = r {
|
||||
let t = r.bestTranscription.formattedString
|
||||
if !t.isEmpty { self.text = t }
|
||||
if r.isFinal { self.finalSem.signal() }
|
||||
// recognitionTask(with:) 在 SFSpeech 内部状态不对时可能抛 NSException
|
||||
// (e.g. recognizer invalidated / request 已 endAudio 后再调)——
|
||||
// belt-and-suspenders 用通用桥接包一层,失败 log + 不置 task(后续会被 cancel 清掉)
|
||||
let taskRC = XTObjCRun { [self] in
|
||||
self.task = rec.recognitionTask(with: req) { [weak self] r, err in
|
||||
guard let self = self else { return }
|
||||
if let r = r {
|
||||
let t = r.bestTranscription.formattedString
|
||||
if !t.isEmpty { self.text = t }
|
||||
if r.isFinal { self.finalSem.signal() }
|
||||
}
|
||||
if err != nil { log("ASR err: \(err!)"); self.finalSem.signal() }
|
||||
}
|
||||
if err != nil { log("ASR err: \(err!)"); self.finalSem.signal() }
|
||||
}
|
||||
if taskRC != 0 {
|
||||
log("apple: recognitionTask(with:) raised NSException — engine may be invalidated")
|
||||
return
|
||||
}
|
||||
log("apple: recording (on-device: \(self.recognizer?.supportsOnDeviceRecognition == true))")
|
||||
}
|
||||
@@ -281,8 +299,16 @@ final class AppleLLMEngine: ASREngine {
|
||||
final class XunfeiEngine: NSObject, ASREngine {
|
||||
let q = DispatchQueue(label: "xtype.xunfei")
|
||||
let engine = AVAudioEngine()
|
||||
var session: URLSession?
|
||||
var wsTask: URLSessionWebSocketTask?
|
||||
// session 与 XunfeiEngine 同生命周期(进程级):lazy 首次访问时创建,永不 invalidate,
|
||||
// 只在 deinit 兜底。反复 invalidate/create 会让 self delegate 同时关联 invalidated 旧 session
|
||||
// + 新 session,webSocketTask(with:) 抛 NSException "INVALIDATED_SESSION"(0xD 走 SIGSEGV,
|
||||
// crash 4F17E81B-DE64-491B-8D59-373FBF5CB17A 验证此路径)。
|
||||
private lazy var session: URLSession = {
|
||||
let delegateQ = OperationQueue()
|
||||
delegateQ.maxConcurrentOperationCount = 1
|
||||
return URLSession(configuration: .default, delegate: self, delegateQueue: delegateQ)
|
||||
}()
|
||||
var pcmBuf = Data() // 16k Int16 mono PCM 累积
|
||||
var rawBuf = Data() // 硬件格式 PCM 累积(Float32 mono @ hwRate,待手动转换)
|
||||
var hwFmt: AVAudioFormat? // 输入节点硬件格式(用于算下采样比)
|
||||
@@ -349,13 +375,20 @@ final class XunfeiEngine: NSObject, ASREngine {
|
||||
return
|
||||
}
|
||||
|
||||
// 用串行 queue 保证 delegate 回调按序触发
|
||||
let delegateQ = OperationQueue()
|
||||
delegateQ.maxConcurrentOperationCount = 1
|
||||
session = URLSession(configuration: .default, delegate: self, delegateQueue: delegateQ)
|
||||
wsTask = session!.webSocketTask(with: url)
|
||||
opened = false
|
||||
wsTask?.resume()
|
||||
// 用串行 queue 保证 delegate 回调按序触发(session 是 lazy 复用,见字段声明)
|
||||
// webSocketTask(with:) 历史抛过 INVALIDATED_SESSION NSException(crash 4F17E81B),
|
||||
// 即便已切到 lazy session,belt-and-suspenders 包一层通用桥接。
|
||||
let wsRC = XTObjCRun { [self] in
|
||||
self.wsTask = self.session.webSocketTask(with: url)
|
||||
self.opened = false
|
||||
self.wsTask?.resume()
|
||||
}
|
||||
if wsRC != 0 {
|
||||
lastErr = "webSocketTask(with:) raised NSException"
|
||||
log("XUNFEI: \(lastErr!)")
|
||||
wsTask = nil
|
||||
return
|
||||
}
|
||||
|
||||
// tap 回调是音频实时线程,绝对禁止在这里 log / 阻塞 / 分配大对象。
|
||||
// 只做一次 unsafe 指针复制 + 把字节扔到 serial queue。
|
||||
@@ -375,13 +408,13 @@ final class XunfeiEngine: NSObject, ASREngine {
|
||||
self.sharedTapForwarder?(buf, t)
|
||||
}
|
||||
if tapRC != 0 {
|
||||
// NSException 已被 ObjC 桥接接住,记 lastErr + 清理已 resume 的 wsTask/session 后返回
|
||||
// NSException 已被 ObjC 桥接接住,记 lastErr + 清理已 resume 的 wsTask 后返回
|
||||
// (session 是 lazy 复用,不在这里 invalidate — 反复 invalidate/create 会让
|
||||
// 下一次 prepare() 的 webSocketTask(with:) 抛 INVALIDATED_SESSION NSException)
|
||||
lastErr = "installTap raised NSException (bus busy or format mismatch?)"
|
||||
log("XUNFEI: \(lastErr!)")
|
||||
wsTask?.cancel(with: .goingAway, reason: nil)
|
||||
wsTask = nil
|
||||
session?.invalidateAndCancel()
|
||||
session = nil
|
||||
return
|
||||
}
|
||||
do { try engine.start() } catch {
|
||||
@@ -397,8 +430,8 @@ final class XunfeiEngine: NSObject, ASREngine {
|
||||
frameTimer?.cancel(); frameTimer = nil
|
||||
wsTask?.cancel(with: .goingAway, reason: nil)
|
||||
wsTask = nil
|
||||
session?.invalidateAndCancel()
|
||||
session = nil
|
||||
// session 是 lazy 复用(见字段声明),不在这里 invalidate —
|
||||
// 反复 invalidate/create 会让 webSocketTask(with:) 抛 INVALIDATED_SESSION NSException
|
||||
engine.inputNode.removeTap(onBus: 0)
|
||||
if engine.isRunning { engine.stop() }
|
||||
pcmBuf = Data(); rawBuf = Data(); resultText = ""
|
||||
|
||||
Reference in new issue
Block a user