diff --git a/apple/Sources/Logseq/LGRuntime.swift b/apple/Sources/Logseq/LGRuntime.swift index d660f6e2..9a2fa886 100644 --- a/apple/Sources/Logseq/LGRuntime.swift +++ b/apple/Sources/Logseq/LGRuntime.swift @@ -464,9 +464,9 @@ public final class LGRuntime { } private func drainEffects() async { - #if os(iOS) - LogseqCrashReporting.runtime(.drainBegin) - defer { LogseqCrashReporting.runtime(.drainEnd) } + #if os(iOS) && LOGSEQ_ENABLE_SENTRY + var drain = LogseqCrashReporting.RuntimeDrain() + defer { drain.finish() } #endif defer { isDrainingEffects = false } let epoch = patchApplyEpoch @@ -489,6 +489,9 @@ public final class LGRuntime { return } + #if os(iOS) && LOGSEQ_ENABLE_SENTRY + drain.recordWork() + #endif let effect: LGEffect do { let data = Data(encoded.utf8) diff --git a/apple/Sources/Logseq/LogseqCrashReporting.swift b/apple/Sources/Logseq/LogseqCrashReporting.swift index 71b55828..d7e33796 100644 --- a/apple/Sources/Logseq/LogseqCrashReporting.swift +++ b/apple/Sources/Logseq/LogseqCrashReporting.swift @@ -111,12 +111,35 @@ enum LogseqCrashReporting { return result } - nonisolated static func runtime(_ state: RuntimeState) { + nonisolated static func runtime(_ state: RuntimeState, at timestamp: Date? = nil) { let breadcrumb = Breadcrumb(level: .info, category: "runtime") breadcrumb.data = ["state": state.rawValue] + if let timestamp { breadcrumb.timestamp = timestamp } SentrySDK.addBreadcrumb(breadcrumb) } + struct RuntimeDrain { + private let start: ContinuousClock.Instant + private let timestamp = Date() + private var began = false + + init(start: ContinuousClock.Instant = .now) { + self.start = start + } + + mutating func recordWork() { + guard !began else { return } + began = true + LogseqCrashReporting.runtime(.drainBegin, at: timestamp) + } + + mutating func finish(at end: ContinuousClock.Instant = .now) { + guard began || start.duration(to: end) >= .milliseconds(100) else { return } + recordWork() + LogseqCrashReporting.runtime(.drainEnd) + } + } + private struct UIState: Equatable { var screen = Screen.unknown var nativeSearchActive = false diff --git a/apple/Tests/UI/sentry-callback-isolation.swift b/apple/Tests/UI/sentry-callback-isolation.swift index 8f8ae718..a8f1a4af 100644 --- a/apple/Tests/UI/sentry-callback-isolation.swift +++ b/apple/Tests/UI/sentry-callback-isolation.swift @@ -23,6 +23,26 @@ internal import _SentryPrivate print("PASS: crash handler enabled in \(expectedEnvironment)") fflush(stdout) LogseqCrashReporting.presentation(.core, kind: .createGraph, screen: .page, editorActive: true) + let start = ContinuousClock.now + for _ in 0..<30 { + var empty = LogseqCrashReporting.RuntimeDrain(start: start) + empty.finish(at: start) + } + var withWork = LogseqCrashReporting.RuntimeDrain(start: start) + withWork.recordWork() + withWork.recordWork() + withWork.finish(at: start) + var slow = LogseqCrashReporting.RuntimeDrain(start: start) + slow.finish(at: start.advanced(by: .milliseconds(100))) + SentrySDK.configureScope { scope in + let breadcrumbs = scope.serialize()["breadcrumbs"] as? [[String: Any]] ?? [] + precondition(breadcrumbs.contains { $0["category"] as? String == "ui.state" }) + let drains = breadcrumbs.compactMap { + ($0["data"] as? [String: String])?["state"] + }.filter { $0 == "drain_begin" || $0 == "drain_end" } + precondition(drains == ["drain_begin", "drain_end", "drain_begin", "drain_end"]) + } + print("PASS: fast empty drains preserve UI breadcrumbs; work and slow drains retain both markers") for _ in 0..<30 { LogseqCrashReporting.runtime(.drainBegin) LogseqCrashReporting.runtime(.drainEnd)