@app-swiss-knife/react-native-sdk 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +473 -2
- package/LICENSE +21 -0
- package/README.md +279 -47
- package/SwissKnifePurchases.podspec +1 -1
- package/android/build.gradle +24 -2
- package/android/consumer-rules.pro +12 -0
- package/android/src/main/AndroidManifest.xml +15 -1
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +5 -4
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +20 -2
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +168 -6
- package/android/src/play/java/com/swissknife/purchases/StoreIntentHandler.kt +7 -0
- package/android/src/rustore/java/com/swissknife/purchases/StoreIntentHandler.kt +9 -0
- package/android/vendor/GENERATED.md +1 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +111 -38
- package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +180 -115
- package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +16 -18
- package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +13 -13
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +56 -25
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +365 -206
- package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +195 -26
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +31 -6
- package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +5 -0
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +56 -31
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +673 -87
- package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +214 -30
- package/android/vendor/java/com/appswissknife/notifications/NotificationRenderer.kt +195 -0
- package/android/vendor/java/com/appswissknife/notifications/PermissionRequestActivity.kt +60 -0
- package/android/vendor/java/com/appswissknife/notifications/PermissionStatus.kt +41 -0
- package/android/vendor/java/com/appswissknife/notifications/PushEventQueue.kt +143 -0
- package/android/vendor/java/com/appswissknife/notifications/PushPayload.kt +72 -0
- package/android/vendor/java/com/appswissknife/notifications/PushRegistration.kt +89 -0
- package/android/vendor/java/com/appswissknife/notifications/SwissKnifeMessagingService.kt +57 -0
- package/android/vendor/java/com/appswissknife/notifications/SwissKnifeNotifications.kt +438 -0
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +312 -65
- package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +1 -0
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +705 -96
- package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +55 -24
- package/android/vendor/java/com/appswissknife/purchases/ProductKindHint.kt +29 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +24 -0
- package/android/vendor-rustore/AndroidManifest.xml +59 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreGateway.kt +428 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreInitProvider.kt +43 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStorePriceEnrichmentProvider.kt +85 -0
- package/app.plugin.js +343 -0
- package/ios/SwissKnifeJSON.swift +24 -2
- package/ios/SwissKnifePurchases.m +20 -0
- package/ios/SwissKnifePurchases.swift +97 -3
- package/ios/SwissKnifePushBootstrap.m +34 -0
- package/ios/vendor/GENERATED.md +1 -0
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +138 -89
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +345 -44
- package/ios/vendor/SwissKnifeiOS/Net/BackendBackoffGate.swift +43 -2
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +495 -16
- package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +231 -23
- package/ios/vendor/SwissKnifeiOS/Notifications/PushPayload.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Notifications/PushRegistrar.swift +322 -0
- package/ios/vendor/SwissKnifeiOS/Notifications/SwissKnife+Notifications.swift +403 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +1 -0
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +11 -7
- package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +37 -5
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +38 -7
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +34 -6
- package/ios/vendor/SwissKnifeiOS/Types.swift +31 -1
- package/ios-notification-service/SwissKnifeNotificationService.podspec +37 -0
- package/ios-notification-service/vendor/GENERATED.md +11 -0
- package/ios-notification-service/vendor/SwissKnifeNotificationService/SwissKnifeNotificationServiceExtension.swift +177 -0
- package/lib/commonjs/attribution.js +4 -3
- package/lib/commonjs/attribution.js.map +1 -1
- package/lib/commonjs/identity.js +12 -11
- package/lib/commonjs/identity.js.map +1 -1
- package/lib/commonjs/index.js +16 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/internal/config.js +105 -8
- package/lib/commonjs/internal/config.js.map +1 -1
- package/lib/commonjs/internal/telemetry-context.js +70 -0
- package/lib/commonjs/internal/telemetry-context.js.map +1 -0
- package/lib/commonjs/modules/analytics.js +18 -25
- package/lib/commonjs/modules/analytics.js.map +1 -1
- package/lib/commonjs/modules/crash-reporting.js +27 -6
- package/lib/commonjs/modules/crash-reporting.js.map +1 -1
- package/lib/commonjs/modules/log.js +1 -1
- package/lib/commonjs/modules/notifications.js +500 -0
- package/lib/commonjs/modules/notifications.js.map +1 -0
- package/lib/commonjs/modules/performance.js +57 -8
- package/lib/commonjs/modules/performance.js.map +1 -1
- package/lib/commonjs/native/module.js +6 -0
- package/lib/commonjs/native/module.js.map +1 -1
- package/lib/commonjs/native/spec.js +12 -5
- package/lib/commonjs/native/spec.js.map +1 -1
- package/lib/commonjs/purchases.js +4 -4
- package/lib/commonjs/types.js +12 -15
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/attribution.js +4 -3
- package/lib/module/attribution.js.map +1 -1
- package/lib/module/identity.js +12 -11
- package/lib/module/identity.js.map +1 -1
- package/lib/module/index.js +8 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal/config.js +103 -8
- package/lib/module/internal/config.js.map +1 -1
- package/lib/module/internal/telemetry-context.js +63 -0
- package/lib/module/internal/telemetry-context.js.map +1 -0
- package/lib/module/modules/analytics.js +18 -25
- package/lib/module/modules/analytics.js.map +1 -1
- package/lib/module/modules/crash-reporting.js +27 -6
- package/lib/module/modules/crash-reporting.js.map +1 -1
- package/lib/module/modules/log.js +1 -1
- package/lib/module/modules/notifications.js +491 -0
- package/lib/module/modules/notifications.js.map +1 -0
- package/lib/module/modules/performance.js +57 -8
- package/lib/module/modules/performance.js.map +1 -1
- package/lib/module/native/module.js +6 -1
- package/lib/module/native/module.js.map +1 -1
- package/lib/module/native/spec.js +11 -4
- package/lib/module/native/spec.js.map +1 -1
- package/lib/module/purchases.js +4 -4
- package/lib/module/types.js +12 -15
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/commonjs/attribution.d.ts +3 -2
- package/lib/typescript/commonjs/attribution.d.ts.map +1 -1
- package/lib/typescript/commonjs/identity.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +2 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/config.d.ts +16 -2
- package/lib/typescript/commonjs/internal/config.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/telemetry-context.d.ts +22 -0
- package/lib/typescript/commonjs/internal/telemetry-context.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts +8 -11
- package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +18 -5
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/notifications.d.ts +110 -0
- package/lib/typescript/commonjs/modules/notifications.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/performance.d.ts +13 -0
- package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/module.d.ts +2 -0
- package/lib/typescript/commonjs/native/module.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/spec.d.ts +33 -4
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -1
- package/lib/typescript/commonjs/purchases.d.ts +4 -4
- package/lib/typescript/commonjs/types.d.ts +4 -5
- package/lib/typescript/commonjs/types.d.ts.map +1 -1
- package/lib/typescript/module/attribution.d.ts +3 -2
- package/lib/typescript/module/attribution.d.ts.map +1 -1
- package/lib/typescript/module/identity.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +2 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/internal/config.d.ts +16 -2
- package/lib/typescript/module/internal/config.d.ts.map +1 -1
- package/lib/typescript/module/internal/telemetry-context.d.ts +22 -0
- package/lib/typescript/module/internal/telemetry-context.d.ts.map +1 -0
- package/lib/typescript/module/modules/analytics.d.ts +8 -11
- package/lib/typescript/module/modules/analytics.d.ts.map +1 -1
- package/lib/typescript/module/modules/crash-reporting.d.ts +18 -5
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/module/modules/notifications.d.ts +110 -0
- package/lib/typescript/module/modules/notifications.d.ts.map +1 -0
- package/lib/typescript/module/modules/performance.d.ts +13 -0
- package/lib/typescript/module/modules/performance.d.ts.map +1 -1
- package/lib/typescript/module/native/module.d.ts +2 -0
- package/lib/typescript/module/native/module.d.ts.map +1 -1
- package/lib/typescript/module/native/spec.d.ts +33 -4
- package/lib/typescript/module/native/spec.d.ts.map +1 -1
- package/lib/typescript/module/purchases.d.ts +4 -4
- package/lib/typescript/module/types.d.ts +4 -5
- package/lib/typescript/module/types.d.ts.map +1 -1
- package/package.json +13 -4
- package/src/attribution.ts +4 -3
- package/src/identity.ts +12 -11
- package/src/index.ts +16 -4
- package/src/internal/config.ts +113 -8
- package/src/internal/telemetry-context.ts +76 -0
- package/src/modules/analytics.ts +24 -33
- package/src/modules/crash-reporting.ts +50 -9
- package/src/modules/log.ts +1 -1
- package/src/modules/notifications.ts +557 -0
- package/src/modules/performance.ts +94 -14
- package/src/native/module.ts +6 -0
- package/src/native/spec.ts +45 -10
- package/src/purchases.ts +4 -4
- package/src/types.ts +12 -15
- package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +0 -326
- package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +0 -296
|
@@ -110,20 +110,63 @@ public struct SKErrorEvent: Codable {
|
|
|
110
110
|
// MARK: - Scope
|
|
111
111
|
|
|
112
112
|
public final class SKErrorScope {
|
|
113
|
-
internal
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
internal struct Snapshot {
|
|
114
|
+
var tags: [String: String] = [:]
|
|
115
|
+
var extra: [String: String] = [:]
|
|
116
|
+
var user: SKUser?
|
|
117
|
+
var identityUserId: String?
|
|
118
|
+
|
|
119
|
+
var effectiveUser: SKUser? {
|
|
120
|
+
let id = user?.id ?? identityUserId
|
|
121
|
+
guard id != nil || user?.email != nil || user?.username != nil else { return nil }
|
|
122
|
+
return SKUser(id: id, email: user?.email, username: user?.username)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private let lock = NSLock()
|
|
127
|
+
private var state = Snapshot()
|
|
128
|
+
|
|
129
|
+
// Copy under the same lock as mutations: Swift value semantics alone do
|
|
130
|
+
// not make a dictionary safe to copy while another thread changes it.
|
|
131
|
+
internal func snapshot() -> Snapshot {
|
|
132
|
+
lock.lock()
|
|
133
|
+
defer { lock.unlock() }
|
|
134
|
+
return state
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Fatal exception handling must not wait for the interrupted writer.
|
|
138
|
+
internal func snapshotIfAvailable() -> Snapshot? {
|
|
139
|
+
guard lock.try() else { return nil }
|
|
140
|
+
defer { lock.unlock() }
|
|
141
|
+
return state
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
internal var tags: [String: String] { snapshot().tags }
|
|
145
|
+
internal var extra: [String: String] { snapshot().extra }
|
|
146
|
+
internal var user: SKUser? { snapshot().user }
|
|
147
|
+
internal var identityUserId: String? {
|
|
148
|
+
get { snapshot().identityUserId }
|
|
149
|
+
set { lock.lock(); defer { lock.unlock() }; state.identityUserId = newValue }
|
|
150
|
+
}
|
|
116
151
|
|
|
117
152
|
public func setTag(_ key: String, value: String) {
|
|
118
|
-
|
|
153
|
+
lock.lock()
|
|
154
|
+
defer { lock.unlock() }
|
|
155
|
+
state.tags[key] = value
|
|
119
156
|
}
|
|
120
157
|
|
|
121
158
|
public func setExtra(_ key: String, value: Any) {
|
|
122
|
-
|
|
159
|
+
// User-defined descriptions may execute arbitrary code: evaluate outside the lock.
|
|
160
|
+
let rendered = "\(value)"
|
|
161
|
+
lock.lock()
|
|
162
|
+
defer { lock.unlock() }
|
|
163
|
+
state.extra[key] = rendered
|
|
123
164
|
}
|
|
124
165
|
|
|
125
166
|
public func setUser(_ user: SKUser?) {
|
|
126
|
-
|
|
167
|
+
lock.lock()
|
|
168
|
+
defer { lock.unlock() }
|
|
169
|
+
state.user = user
|
|
127
170
|
}
|
|
128
171
|
}
|
|
129
172
|
|
|
@@ -131,6 +174,7 @@ public final class SKErrorScope {
|
|
|
131
174
|
|
|
132
175
|
internal final class RingBuffer<T> {
|
|
133
176
|
private var buffer: [T] = []
|
|
177
|
+
private let lock = NSLock()
|
|
134
178
|
private let capacity: Int
|
|
135
179
|
|
|
136
180
|
init(capacity: Int) {
|
|
@@ -138,15 +182,235 @@ internal final class RingBuffer<T> {
|
|
|
138
182
|
}
|
|
139
183
|
|
|
140
184
|
func append(_ element: T) {
|
|
185
|
+
lock.lock()
|
|
186
|
+
defer { lock.unlock() }
|
|
141
187
|
if buffer.count >= capacity {
|
|
142
188
|
buffer.removeFirst()
|
|
143
189
|
}
|
|
144
190
|
buffer.append(element)
|
|
145
191
|
}
|
|
146
192
|
|
|
147
|
-
func snapshot() -> [T] {
|
|
193
|
+
func snapshot() -> [T] {
|
|
194
|
+
lock.lock()
|
|
195
|
+
defer { lock.unlock() }
|
|
196
|
+
return buffer
|
|
197
|
+
}
|
|
148
198
|
|
|
149
|
-
func
|
|
199
|
+
func snapshotIfAvailable() -> [T]? {
|
|
200
|
+
guard lock.try() else { return nil }
|
|
201
|
+
defer { lock.unlock() }
|
|
202
|
+
return buffer
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
func clear() {
|
|
206
|
+
lock.lock()
|
|
207
|
+
defer { lock.unlock() }
|
|
208
|
+
buffer = []
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// MARK: - ANR Watchdog
|
|
213
|
+
|
|
214
|
+
/// Detects a blocked main thread: post a ping to main, sleep `timeout`, and
|
|
215
|
+
/// report a hang if the ping has not run by then.
|
|
216
|
+
///
|
|
217
|
+
/// - **Latched.** One hang is one report. After reporting, no new ping is
|
|
218
|
+
/// posted until the SAME ping has run; only then does the watchdog rearm.
|
|
219
|
+
/// It used to report again every cycle, so a 30 s hang with the default
|
|
220
|
+
/// 5 s timeout produced six identical ANRs.
|
|
221
|
+
/// - **Paused in the background.** Nothing is checked between
|
|
222
|
+
/// `didEnterBackground` and `willEnterForeground`, and the first cycle
|
|
223
|
+
/// after resume is a grace cycle: the main queue of a process that was
|
|
224
|
+
/// just suspended has a backlog, which is not a hang.
|
|
225
|
+
/// - **Oversleep guard.** If the watchdog thread itself woke far later than
|
|
226
|
+
/// it asked (the whole process was frozen), an unanswered ping says
|
|
227
|
+
/// nothing about main, so that cycle does not report.
|
|
228
|
+
/// - The ping counters are written on main and read on the watchdog thread,
|
|
229
|
+
/// so they live under `lock`. (`var responded = false` used to be shared
|
|
230
|
+
/// between the two threads with no synchronisation at all.)
|
|
231
|
+
///
|
|
232
|
+
/// `Environment` holds the seams — how to reach main, how to sleep, and a
|
|
233
|
+
/// monotonic clock — so tests can drive `runCycle()` one cycle at a time.
|
|
234
|
+
internal final class ANRWatchdog: @unchecked Sendable {
|
|
235
|
+
|
|
236
|
+
struct Environment {
|
|
237
|
+
var postToMain: (@escaping () -> Void) -> Void
|
|
238
|
+
var sleep: (TimeInterval) -> Void
|
|
239
|
+
/// Monotonic seconds. Includes time the device slept, so a frozen
|
|
240
|
+
/// process shows up as an oversleep.
|
|
241
|
+
var now: () -> TimeInterval
|
|
242
|
+
|
|
243
|
+
static let live = Environment(
|
|
244
|
+
postToMain: { DispatchQueue.main.async(execute: $0) },
|
|
245
|
+
sleep: { Thread.sleep(forTimeInterval: $0) },
|
|
246
|
+
now: { TimeInterval(clock_gettime_nsec_np(CLOCK_MONOTONIC)) / 1_000_000_000 }
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
let timeout: TimeInterval
|
|
251
|
+
static let oversleepSlack: TimeInterval = 1
|
|
252
|
+
private let environment: Environment
|
|
253
|
+
private let onHang: () -> Void
|
|
254
|
+
|
|
255
|
+
// Everything below is guarded by `lock`. It is never held across a
|
|
256
|
+
// sleep, a post to main, or `onHang`.
|
|
257
|
+
private let lock = NSLock()
|
|
258
|
+
private var running = false
|
|
259
|
+
/// Bumped by every start and stop, so a loop from an earlier start
|
|
260
|
+
/// exits instead of running alongside a new one.
|
|
261
|
+
private var generation = 0
|
|
262
|
+
private var lastPingSent: UInt64 = 0
|
|
263
|
+
private var lastPingAnswered: UInt64 = 0
|
|
264
|
+
/// The ping a hang was reported on; `nil` when armed.
|
|
265
|
+
private var latchedPing: UInt64?
|
|
266
|
+
private var inBackground = false
|
|
267
|
+
private var graceCycles = 0
|
|
268
|
+
|
|
269
|
+
init(
|
|
270
|
+
timeout: TimeInterval,
|
|
271
|
+
environment: Environment = .live,
|
|
272
|
+
onHang: @escaping () -> Void
|
|
273
|
+
) {
|
|
274
|
+
self.timeout = timeout
|
|
275
|
+
self.environment = environment
|
|
276
|
+
self.onHang = onHang
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
var isRunning: Bool {
|
|
280
|
+
lock.lock(); defer { lock.unlock() }
|
|
281
|
+
return running
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/// Start the watchdog thread. Idempotent: a running watchdog keeps its
|
|
285
|
+
/// one thread.
|
|
286
|
+
func start() {
|
|
287
|
+
lock.lock()
|
|
288
|
+
if running {
|
|
289
|
+
lock.unlock()
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
running = true
|
|
293
|
+
generation += 1
|
|
294
|
+
let gen = generation
|
|
295
|
+
lock.unlock()
|
|
296
|
+
|
|
297
|
+
let thread = Thread { [weak self] in
|
|
298
|
+
while let watchdog = self, watchdog.runCycle(generation: gen) {}
|
|
299
|
+
}
|
|
300
|
+
thread.name = "com.swissknife.anr-watchdog"
|
|
301
|
+
thread.qualityOfService = .utility
|
|
302
|
+
thread.start()
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/// Stop checking. The thread exits when its current sleep ends.
|
|
306
|
+
func stop() {
|
|
307
|
+
lock.lock()
|
|
308
|
+
running = false
|
|
309
|
+
generation += 1
|
|
310
|
+
lock.unlock()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
func appDidEnterBackground() {
|
|
314
|
+
lock.lock()
|
|
315
|
+
inBackground = true
|
|
316
|
+
lock.unlock()
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
func appWillEnterForeground() {
|
|
320
|
+
lock.lock()
|
|
321
|
+
inBackground = false
|
|
322
|
+
graceCycles = 1
|
|
323
|
+
lock.unlock()
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/// Run one check on the calling thread: what the watchdog thread does in
|
|
327
|
+
/// a loop. Returns `false` once the watchdog has been stopped.
|
|
328
|
+
@discardableResult
|
|
329
|
+
func runCycle() -> Bool {
|
|
330
|
+
lock.lock()
|
|
331
|
+
let gen = generation
|
|
332
|
+
lock.unlock()
|
|
333
|
+
return runCycle(generation: gen)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/// Test seam — mark the watchdog running without spawning its thread,
|
|
337
|
+
/// so a test can drive `runCycle()` deterministically.
|
|
338
|
+
internal func _armWithoutThreadForTesting() {
|
|
339
|
+
lock.lock()
|
|
340
|
+
running = true
|
|
341
|
+
generation += 1
|
|
342
|
+
lock.unlock()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private enum Step {
|
|
346
|
+
case stop
|
|
347
|
+
/// Nothing to check this cycle (background, or a hang still latched).
|
|
348
|
+
case idle
|
|
349
|
+
case ping(UInt64)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private func runCycle(generation gen: Int) -> Bool {
|
|
353
|
+
lock.lock()
|
|
354
|
+
let step = beginCycleLocked(gen)
|
|
355
|
+
lock.unlock()
|
|
356
|
+
|
|
357
|
+
let ping: UInt64
|
|
358
|
+
switch step {
|
|
359
|
+
case .stop:
|
|
360
|
+
return false
|
|
361
|
+
case .idle:
|
|
362
|
+
ping = 0
|
|
363
|
+
case .ping(let id):
|
|
364
|
+
ping = id
|
|
365
|
+
environment.postToMain { [weak self] in self?.answer(id) }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let sleptFrom = environment.now()
|
|
369
|
+
environment.sleep(timeout)
|
|
370
|
+
let slept = environment.now() - sleptFrom
|
|
371
|
+
|
|
372
|
+
lock.lock()
|
|
373
|
+
let report = endCycleLocked(gen, ping: ping, slept: slept)
|
|
374
|
+
let keepGoing = running && generation == gen
|
|
375
|
+
lock.unlock()
|
|
376
|
+
|
|
377
|
+
if report { onHang() }
|
|
378
|
+
return keepGoing
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private func beginCycleLocked(_ gen: Int) -> Step {
|
|
382
|
+
guard running, generation == gen else { return .stop }
|
|
383
|
+
if inBackground { return .idle }
|
|
384
|
+
if let latched = latchedPing {
|
|
385
|
+
// Still the same hang until the ping we reported on has run.
|
|
386
|
+
guard lastPingAnswered >= latched else { return .idle }
|
|
387
|
+
latchedPing = nil
|
|
388
|
+
}
|
|
389
|
+
lastPingSent += 1
|
|
390
|
+
return .ping(lastPingSent)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private func endCycleLocked(_ gen: Int, ping: UInt64, slept: TimeInterval) -> Bool {
|
|
394
|
+
guard running, generation == gen, ping != 0 else { return false }
|
|
395
|
+
if inBackground { return false }
|
|
396
|
+
if graceCycles > 0 {
|
|
397
|
+
graceCycles -= 1
|
|
398
|
+
return false
|
|
399
|
+
}
|
|
400
|
+
// The watchdog thread was frozen too; the ping proves nothing. The
|
|
401
|
+
// second of slack absorbs timer coalescing, which stretches a
|
|
402
|
+
// utility-QoS sleep by up to ~150 ms whatever the timeout.
|
|
403
|
+
if slept > timeout * 2 + Self.oversleepSlack { return false }
|
|
404
|
+
guard latchedPing == nil, lastPingAnswered < ping else { return false }
|
|
405
|
+
latchedPing = ping
|
|
406
|
+
return true
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private func answer(_ id: UInt64) {
|
|
410
|
+
lock.lock()
|
|
411
|
+
if id > lastPingAnswered { lastPingAnswered = id }
|
|
412
|
+
lock.unlock()
|
|
413
|
+
}
|
|
150
414
|
}
|
|
151
415
|
|
|
152
416
|
// MARK: - Main SDK
|
|
@@ -210,8 +474,18 @@ public final class SKCrashReporting {
|
|
|
210
474
|
// session_event payloads disagree with their own enclosing envelope.
|
|
211
475
|
private var sessionErrorCount: Int = 0
|
|
212
476
|
|
|
213
|
-
// ANR watchdog
|
|
214
|
-
|
|
477
|
+
// ANR watchdog — one per process; a repeated `start(...)` reuses it.
|
|
478
|
+
// Behind `anrLock` because `start(...)` runs on the host's thread and
|
|
479
|
+
// the lifecycle observer on main.
|
|
480
|
+
private let anrLock = NSLock()
|
|
481
|
+
private var _anrWatchdog: ANRWatchdog?
|
|
482
|
+
private var anrWatchdog: ANRWatchdog? {
|
|
483
|
+
get { anrLock.lock(); defer { anrLock.unlock() }; return _anrWatchdog }
|
|
484
|
+
set { anrLock.lock(); _anrWatchdog = newValue; anrLock.unlock() }
|
|
485
|
+
}
|
|
486
|
+
/// Main-actor only: the AppLifecycle subscription that pauses the
|
|
487
|
+
/// watchdog in the background.
|
|
488
|
+
private var anrLifecycleToken: UUID?
|
|
215
489
|
|
|
216
490
|
/// Session-rotation observer token — re-persists crash context on
|
|
217
491
|
/// rotation so signal-path recovery closes the right session.
|
|
@@ -254,10 +528,12 @@ public final class SKCrashReporting {
|
|
|
254
528
|
/// notifies observers — `SwissKnife.configure(appUserID:)` seeds the id
|
|
255
529
|
/// directly via `Identity._seedConfiguredUser`.
|
|
256
530
|
///
|
|
257
|
-
///
|
|
258
|
-
///
|
|
259
|
-
|
|
260
|
-
|
|
531
|
+
/// Stored under the scope lock; the NSException handler takes only a
|
|
532
|
+
/// non-blocking snapshot, and the signal handler never reads Swift state.
|
|
533
|
+
private var identityUserId: String? {
|
|
534
|
+
get { scope.identityUserId }
|
|
535
|
+
set { scope.identityUserId = newValue }
|
|
536
|
+
}
|
|
261
537
|
|
|
262
538
|
/// Identity-change observation token, so a repeated `start()` re-arms
|
|
263
539
|
/// the observer instead of stacking duplicates.
|
|
@@ -522,6 +798,8 @@ public final class SKCrashReporting {
|
|
|
522
798
|
// ANR watchdog
|
|
523
799
|
if configuration.anrTimeoutSeconds > 0 {
|
|
524
800
|
startANRWatchdog()
|
|
801
|
+
} else {
|
|
802
|
+
stopANRWatchdog()
|
|
525
803
|
}
|
|
526
804
|
}
|
|
527
805
|
|
|
@@ -732,16 +1010,9 @@ public final class SKCrashReporting {
|
|
|
732
1010
|
/// - All three nil ⇒ `nil`, preserving today's behaviour for apps that
|
|
733
1011
|
/// never log in and never call `setUser`.
|
|
734
1012
|
///
|
|
735
|
-
///
|
|
736
|
-
///
|
|
737
|
-
private var effectiveUser: SKUser? {
|
|
738
|
-
let explicit = scope.user
|
|
739
|
-
let id = explicit?.id ?? identityUserId
|
|
740
|
-
let email = explicit?.email
|
|
741
|
-
let username = explicit?.username
|
|
742
|
-
guard id != nil || email != nil || username != nil else { return nil }
|
|
743
|
-
return SKUser(id: id, email: email, username: username)
|
|
744
|
-
}
|
|
1013
|
+
/// Ordinary-thread readers use the synchronized cache. The NSException
|
|
1014
|
+
/// hook uses a non-blocking scope snapshot instead.
|
|
1015
|
+
private var effectiveUser: SKUser? { scope.snapshot().effectiveUser }
|
|
745
1016
|
|
|
746
1017
|
private func buildEvent(level: SKErrorLevel) -> SKErrorEvent {
|
|
747
1018
|
var event = SKErrorEvent(level: level)
|
|
@@ -871,11 +1142,12 @@ public final class SKCrashReporting {
|
|
|
871
1142
|
// this snapshot ahead of the crash.
|
|
872
1143
|
refreshIdentityUserId()
|
|
873
1144
|
let session = Session.shared.current
|
|
1145
|
+
let scopeSnapshot = scope.snapshot()
|
|
874
1146
|
let ctx = PersistedContext(
|
|
875
1147
|
breadcrumbs: breadcrumbs.snapshot(),
|
|
876
|
-
tags:
|
|
877
|
-
extra:
|
|
878
|
-
user: effectiveUser,
|
|
1148
|
+
tags: scopeSnapshot.tags,
|
|
1149
|
+
extra: scopeSnapshot.extra,
|
|
1150
|
+
user: scopeSnapshot.effectiveUser,
|
|
879
1151
|
environment: configuration?.environment,
|
|
880
1152
|
release: configuration?.release,
|
|
881
1153
|
sessionId: session.id,
|
|
@@ -1226,12 +1498,12 @@ public final class SKCrashReporting {
|
|
|
1226
1498
|
event.exceptionValue = exception.reason
|
|
1227
1499
|
event.mechanism = "nsexception"
|
|
1228
1500
|
event.stacktrace = reporting.parseCallStackSymbols(exception.callStackSymbols)
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
event.user =
|
|
1501
|
+
// Exceptions can originate on this queue (including flush callbacks).
|
|
1502
|
+
// Never sync back onto it or wait for an interrupted breadcrumb writer.
|
|
1503
|
+
event.breadcrumbs = reporting.breadcrumbs.snapshotIfAvailable()
|
|
1504
|
+
let scopeSnapshot = reporting.scope.snapshotIfAvailable()
|
|
1505
|
+
event.tags = scopeSnapshot?.tags
|
|
1506
|
+
event.user = scopeSnapshot?.effectiveUser
|
|
1235
1507
|
event.environment = reporting.configuration?.environment
|
|
1236
1508
|
event.release = reporting.configuration?.release
|
|
1237
1509
|
reporting.applyDeviceContext(to: &event)
|
|
@@ -1355,23 +1627,49 @@ public final class SKCrashReporting {
|
|
|
1355
1627
|
|
|
1356
1628
|
// MARK: - ANR Watchdog
|
|
1357
1629
|
|
|
1630
|
+
/// Start the watchdog, or keep the one already running. Each call used
|
|
1631
|
+
/// to start ANOTHER polling loop, so every repeated `start(...)` (the RN
|
|
1632
|
+
/// bridge on a JS reload) added a watchdog.
|
|
1358
1633
|
private func startANRWatchdog() {
|
|
1359
1634
|
guard let timeout = configuration?.anrTimeoutSeconds, timeout > 0 else { return }
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
var responded = false
|
|
1364
|
-
DispatchQueue.main.async { responded = true }
|
|
1365
|
-
Thread.sleep(forTimeInterval: timeout)
|
|
1366
|
-
if self?.anrWatchdogRunning == true && !responded {
|
|
1367
|
-
self?.captureANR()
|
|
1368
|
-
}
|
|
1369
|
-
}
|
|
1635
|
+
if let existing = anrWatchdog, existing.timeout == timeout {
|
|
1636
|
+
existing.start() // idempotent; restarts one that was stopped
|
|
1637
|
+
return
|
|
1370
1638
|
}
|
|
1639
|
+
anrWatchdog?.stop()
|
|
1640
|
+
let watchdog = ANRWatchdog(timeout: timeout) { [weak self] in self?.captureANR() }
|
|
1641
|
+
anrWatchdog = watchdog
|
|
1642
|
+
watchdog.start()
|
|
1643
|
+
observeLifecycleForANRWatchdog()
|
|
1371
1644
|
}
|
|
1372
1645
|
|
|
1373
1646
|
private func stopANRWatchdog() {
|
|
1374
|
-
|
|
1647
|
+
anrWatchdog?.stop()
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/// Pause the watchdog between didEnterBackground and willEnterForeground.
|
|
1651
|
+
/// Subscribed once; the handler always talks to the current watchdog.
|
|
1652
|
+
private func observeLifecycleForANRWatchdog() {
|
|
1653
|
+
Task { @MainActor [weak self] in
|
|
1654
|
+
#if canImport(UIKit)
|
|
1655
|
+
// A background launch (silent push, background fetch) never sends
|
|
1656
|
+
// didEnterBackground: start this watchdog paused in that case.
|
|
1657
|
+
if UIApplication.shared.applicationState == .background {
|
|
1658
|
+
self?.anrWatchdog?.appDidEnterBackground()
|
|
1659
|
+
}
|
|
1660
|
+
#endif
|
|
1661
|
+
guard let self, self.anrLifecycleToken == nil else { return }
|
|
1662
|
+
self.anrLifecycleToken = AppLifecycle.subscribe { [weak self] event in
|
|
1663
|
+
switch event {
|
|
1664
|
+
case .didEnterBackground:
|
|
1665
|
+
self?.anrWatchdog?.appDidEnterBackground()
|
|
1666
|
+
case .willEnterForeground:
|
|
1667
|
+
self?.anrWatchdog?.appWillEnterForeground()
|
|
1668
|
+
default:
|
|
1669
|
+
break
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1375
1673
|
}
|
|
1376
1674
|
|
|
1377
1675
|
private func captureANR() {
|
|
@@ -1761,6 +2059,9 @@ public final class SKCrashReporting {
|
|
|
1761
2059
|
|
|
1762
2060
|
internal var _pendingCrashURL: URL { SKCrashReporting.pendingCrashURL }
|
|
1763
2061
|
|
|
2062
|
+
/// Test seam — the process's ANR watchdog, if one was started.
|
|
2063
|
+
internal var _anrWatchdogForTesting: ANRWatchdog? { anrWatchdog }
|
|
2064
|
+
|
|
1764
2065
|
/// Test seam — the on-disk crash-context snapshot the signal path
|
|
1765
2066
|
/// recovers from. Used to assert the subject is baked in AHEAD of the
|
|
1766
2067
|
/// crash, which is the whole reason the handler needs no Identity call.
|
|
@@ -60,8 +60,15 @@
|
|
|
60
60
|
// 2. A server-supplied `Retry-After` is honoured but clamped to
|
|
61
61
|
// `maxHonouredRetryAfter` (5 min), so a buggy or hostile header
|
|
62
62
|
// cannot park the queue for a day.
|
|
63
|
-
// 3. The gate is in-memory
|
|
64
|
-
// recovery flow in the stress suite actually does — starts from open
|
|
63
|
+
// 3. The gate is in-memory. A process restart — which is what every
|
|
64
|
+
// recovery flow in the stress suite actually does — starts from open,
|
|
65
|
+
// with one exception: the hold `IngestClient` arms after a drain the
|
|
66
|
+
// server answered with whole-batch 4xx rejections (at most 5 min) is
|
|
67
|
+
// kept in its `set-aside.json` and put back on the first drain after a
|
|
68
|
+
// relaunch (`restoreWindow(until:)`), unless it ends more than 5 min
|
|
69
|
+
// from then, which only a clock set back can make it do: that one is
|
|
70
|
+
// ignored. Without it every cold start re-sent to a server that was
|
|
71
|
+
// refusing the queue. No stress scenario injects such a 4xx.
|
|
65
72
|
// On top of that, `IngestClient` never arms it while the network is known
|
|
66
73
|
// to be unreachable, because it short-circuits before making the request:
|
|
67
74
|
// an offline stretch costs the gate nothing.
|
|
@@ -152,6 +159,40 @@ internal final class BackendBackoffGate: @unchecked Sendable {
|
|
|
152
159
|
return armLocked(retryAfter: nil)
|
|
153
160
|
}
|
|
154
161
|
|
|
162
|
+
/// When the window ends, or `nil` while the gate is open. What
|
|
163
|
+
/// `IngestClient` persists so a relaunch can put the window back.
|
|
164
|
+
var reopensAt: Date? {
|
|
165
|
+
lock.lock(); defer { lock.unlock() }
|
|
166
|
+
return remainingLocked() > 0 ? notBefore : nil
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/// Keep the gate closed until `date`, a window an earlier process armed.
|
|
170
|
+
/// It only ever extends the window, like `noteRetryable`, and leaves the
|
|
171
|
+
/// escalation alone: this restores a window, it is not a new failure.
|
|
172
|
+
///
|
|
173
|
+
/// A window ending more than `maxHonouredRetryAfter` from now is ignored.
|
|
174
|
+
/// No window this gate arms is longer, so the wall clock has moved back
|
|
175
|
+
/// since it was written (a user setting the time back, an NTP
|
|
176
|
+
/// correction), or the value was never a window. It used to be clamped to
|
|
177
|
+
/// five minutes from now instead. Nothing writes the clamped value back
|
|
178
|
+
/// (a drain the gate holds off ends before it saves anything), so every
|
|
179
|
+
/// relaunch clamped it again from its own launch time, and a device whose
|
|
180
|
+
/// sessions were shorter than five minutes sent nothing until the wall
|
|
181
|
+
/// clock caught up with the stored date: a day, for a clock set back a
|
|
182
|
+
/// day. Ignored, it costs one drain: if the server is still refusing, that
|
|
183
|
+
/// drain arms a hold of its own.
|
|
184
|
+
func restoreWindow(until date: Date) {
|
|
185
|
+
lock.lock(); defer { lock.unlock() }
|
|
186
|
+
let left = date.timeIntervalSince(now())
|
|
187
|
+
guard left > 0, left <= Self.maxHonouredRetryAfter + Self.restoredWindowSlack else { return }
|
|
188
|
+
if let existing = notBefore, existing >= date { return }
|
|
189
|
+
notBefore = date
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// Slack on that bound for the rounding of a window written to disk as
|
|
193
|
+
/// seconds since 1970 and read back.
|
|
194
|
+
private static let restoredWindowSlack: TimeInterval = 1
|
|
195
|
+
|
|
155
196
|
/// Snapshot for tests and diagnostics.
|
|
156
197
|
func snapshot() -> (remaining: TimeInterval, consecutiveFailures: Int) {
|
|
157
198
|
lock.lock(); defer { lock.unlock() }
|