Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix(ios): stop crashing when a ping is answered twice or the microphone is unavailable

leepokai49e116d40b46
diff --git a/apps/ios/Kit/Sources/CodyncKit/Analytics/CrashReports.swift b/apps/ios/Kit/Sources/CodyncKit/Analytics/CrashReports.swift
index 8c1d27b3..bfc6445c 100644
--- a/apps/ios/Kit/Sources/CodyncKit/Analytics/CrashReports.swift
+++ b/apps/ios/Kit/Sources/CodyncKit/Analytics/CrashReports.swift
@@ -6,7 +6,8 @@ import MetricKit
/// and binary + offset frames, never a message, reason or memory detail.
public final class CrashReports: NSObject, MXMetricManagerSubscriber, Sendable {
public static let shared = CrashReports()
- private static let maxFrames = 50
+ /// A SwiftUI update alone runs dozens of frames deep: fewer would cut off the app's own.
+ private static let maxFrames = 200
/// Subscribes once, at app launch (widgets and the notification extension never do).
public func start() {
diff --git a/apps/ios/Kit/Sources/CodyncKit/Client/HostConnector.swift b/apps/ios/Kit/Sources/CodyncKit/Client/HostConnector.swift
index 40a29285..7fd09dad 100644
--- a/apps/ios/Kit/Sources/CodyncKit/Client/HostConnector.swift
+++ b/apps/ios/Kit/Sources/CodyncKit/Client/HostConnector.swift
@@ -1,4 +1,5 @@
import Foundation
+import os
/// Opens channels to computers (§7.5) and pairs new ones from a QR (§4.1).
public enum HostConnector {
@@ -96,7 +97,10 @@ final class URLSessionSocket: ChannelSocket {
func ping() async throws {
if relay { return try await send(#"{"t":"ping"}"#) }
try await withCheckedThrowingContinuation { (c: CheckedContinuation<Void, Error>) in
+ // URLSession can answer a ping twice when the socket closes; a continuation resumes once.
+ let waiting = OSAllocatedUnfairLock<CheckedContinuation<Void, Error>?>(initialState: c)
task.sendPing { error in
+ guard let c = waiting.withLock({ $0.take() }) else { return }
if let error { c.resume(throwing: error) } else { c.resume() }
}
}
diff --git a/apps/ios/Kit/Sources/CodyncUI/Call/CloudSpeechEngine.swift b/apps/ios/Kit/Sources/CodyncUI/Call/CloudSpeechEngine.swift
index 62be4600..8dedf68b 100644
--- a/apps/ios/Kit/Sources/CodyncUI/Call/CloudSpeechEngine.swift
+++ b/apps/ios/Kit/Sources/CodyncUI/Call/CloudSpeechEngine.swift
@@ -85,7 +85,7 @@ final class CloudSpeechEngine: VoiceEngine {
try session.setCategory(.playAndRecord, mode: .voiceChat, options: [.defaultToSpeaker, .allowBluetoothHFP])
try session.setActive(true)
let input = engine.inputNode
- let format = input.outputFormat(forBus: 0)
+ let format = try MicTap.format(of: input)
input.installTap(onBus: 0, bufferSize: 2048, format: format, block: MicTap.pcm16k(from: format) { [weak self] pcm, level in
Task { @MainActor in self?.heard(pcm, level: level) }
})
diff --git a/apps/ios/Kit/Sources/CodyncUI/Call/GeminiLiveEngine.swift b/apps/ios/Kit/Sources/CodyncUI/Call/GeminiLiveEngine.swift
index cd2c95b4..b777599f 100644
--- a/apps/ios/Kit/Sources/CodyncUI/Call/GeminiLiveEngine.swift
+++ b/apps/ios/Kit/Sources/CodyncUI/Call/GeminiLiveEngine.swift
@@ -257,7 +257,7 @@ final class GeminiLiveEngine: VoiceEngine {
try input.setVoiceProcessingEnabled(true)
engine.attach(player)
engine.connect(player, to: engine.mainMixerNode, format: Self.playFormat)
- let format = input.outputFormat(forBus: 0)
+ let format = try MicTap.format(of: input)
input.installTap(onBus: 0, bufferSize: 2048, format: format, block: MicTap.pcm16k(from: format) { [weak self] pcm, level in
let audio = pcm.base64EncodedString()
Task { @MainActor in self?.captured(audio, level: level) }
diff --git a/apps/ios/Kit/Sources/CodyncUI/Call/OnDeviceEngine.swift b/apps/ios/Kit/Sources/CodyncUI/Call/OnDeviceEngine.swift
index f48db794..e3b5cc07 100644
--- a/apps/ios/Kit/Sources/CodyncUI/Call/OnDeviceEngine.swift
+++ b/apps/ios/Kit/Sources/CodyncUI/Call/OnDeviceEngine.swift
@@ -136,11 +136,12 @@ final class OnDeviceEngine: VoiceEngine {
self.request = request
let input = engine.inputNode
input.removeTap(onBus: 0)
- input.installTap(onBus: 0, bufferSize: 1024, format: input.outputFormat(forBus: 0), block: Self.tap(request) { [weak self] level in
- Task { @MainActor in self?.level = level }
- })
- engine.prepare()
do {
+ let format = try MicTap.format(of: input)
+ input.installTap(onBus: 0, bufferSize: 1024, format: format, block: Self.tap(request) { [weak self] level in
+ Task { @MainActor in self?.level = level }
+ })
+ engine.prepare()
try engine.start()
} catch {
log.error("audio engine: \(error.localizedDescription, privacy: .public)")
diff --git a/apps/ios/Kit/Sources/CodyncUI/Call/VoiceEngine.swift b/apps/ios/Kit/Sources/CodyncUI/Call/VoiceEngine.swift
index 8e897bbd..9d409a84 100644
--- a/apps/ios/Kit/Sources/CodyncUI/Call/VoiceEngine.swift
+++ b/apps/ios/Kit/Sources/CodyncUI/Call/VoiceEngine.swift
@@ -120,6 +120,18 @@ enum VoiceSettings {
}
enum MicTap {
+ struct Unavailable: LocalizedError {
+ var errorDescription: String? { "no microphone is available right now" }
+ }
+
+ /// The microphone's format. With no usable input (no microphone, a route change, an
+ /// interruption) it's 0 Hz, and a tap on it raises an exception that ends the app: throws instead.
+ static func format(of input: AVAudioInputNode) throws -> AVAudioFormat {
+ let format = input.outputFormat(forBus: 0)
+ guard format.sampleRate > 0, format.channelCount > 0 else { throw Unavailable() }
+ return format
+ }
+
/// Microphone → 16 kHz mono PCM16 (little-endian) with a loudness level, 0…1. Built off the
/// main actor: the block runs on the audio thread.
nonisolated static func pcm16k(from format: AVAudioFormat,
diff --git a/apps/ios/Kit/Sources/CodyncUI/Thread/Dictation.swift b/apps/ios/Kit/Sources/CodyncUI/Thread/Dictation.swift
index 1974223c..24326d82 100644
--- a/apps/ios/Kit/Sources/CodyncUI/Thread/Dictation.swift
+++ b/apps/ios/Kit/Sources/CodyncUI/Thread/Dictation.swift
@@ -35,9 +35,7 @@ final class Dictation {
} catch {
return
}
- // No input route (no microphone): installing a tap would throw.
- let format = engine.inputNode.outputFormat(forBus: 0)
- guard format.sampleRate > 0, format.channelCount > 0 else {
+ guard let format = try? MicTap.format(of: engine.inputNode) else {
try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
return
}
diff --git a/apps/ios/Kit/Tests/CodyncKitTests/AnalyticsTests.swift b/apps/ios/Kit/Tests/CodyncKitTests/AnalyticsTests.swift
index 89e30339..bd9bfa2a 100644
--- a/apps/ios/Kit/Tests/CodyncKitTests/AnalyticsTests.swift
+++ b/apps/ios/Kit/Tests/CodyncKitTests/AnalyticsTests.swift
@@ -77,12 +77,12 @@ import Testing
let encoded = String(decoding: try JSONEncoder().encode(unknown), as: UTF8.self)
#expect(encoded.contains(#""type":"Crash""#) && encoded.contains(#""frames":[]"#))
- // A runaway stack keeps the 50 frames nearest the crash.
+ // A runaway stack keeps the 200 frames nearest the crash.
var deep = frame("Codync", 0)
- for i in 1...80 { deep = frame("Codync", i, deep) }
+ for i in 1...250 { deep = frame("Codync", i, deep) }
let capped = CrashReports.frames(Data(#"{"callStacks":[{"callStackRootFrames":[\#(deep)]}]}"#.utf8))
- #expect(capped.count == 50)
- #expect(capped.last == .object(["platform": .string("custom"), "lang": .string("swift"), "function": .string("Codync + 80"),
+ #expect(capped.count == 200)
+ #expect(capped.last == .object(["platform": .string("custom"), "lang": .string("swift"), "function": .string("Codync + 250"),
"module": .string("Codync"), "resolved": .bool(false), "in_app": .bool(true)]))
// Recorded like any other event: off records nothing.