Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions apple/Sources/Logseq/LGRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
25 changes: 24 additions & 1 deletion apple/Sources/Logseq/LogseqCrashReporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions apple/Tests/UI/sentry-callback-isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading