From 917d84ee675d0bb78586bd77576caa9e62941a53 Mon Sep 17 00:00:00 2001 From: Dhiogo Ramos Brustolin Date: Thu, 19 Jan 2023 10:18:53 +0100 Subject: [PATCH 1/4] read App Windows in the main thread for view hierarchy fetch --- Sources/Sentry/SentryViewHierarchy.m | 4 ++-- .../SentryViewHierarchyTests.swift | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Sources/Sentry/SentryViewHierarchy.m b/Sources/Sentry/SentryViewHierarchy.m index b846c7b6a54..0f08f8324d5 100644 --- a/Sources/Sentry/SentryViewHierarchy.m +++ b/Sources/Sentry/SentryViewHierarchy.m @@ -48,11 +48,11 @@ - (BOOL)saveViewHierarchy:(NSString *)filePath - (NSData *)fetchViewHierarchy { - NSArray *windows = [SentryDependencyContainer.sharedInstance.application windows]; - __block NSMutableData *result = [[NSMutableData alloc] init]; void (^save)(void) = ^{ + NSArray *windows = [SentryDependencyContainer.sharedInstance.application windows]; + if (![self processViewHierarchy:windows addFunction:writeJSONDataToMemory userData:(__bridge void *)(result)]) { diff --git a/Tests/SentryTests/SentryViewHierarchyTests.swift b/Tests/SentryTests/SentryViewHierarchyTests.swift index 797b5de7e33..10a4061cb23 100644 --- a/Tests/SentryTests/SentryViewHierarchyTests.swift +++ b/Tests/SentryTests/SentryViewHierarchyTests.swift @@ -172,14 +172,37 @@ class SentryViewHierarchyTests: XCTestCase { wait(for: [ex], timeout: 1) } + func test_fetch_usesMainThread() { + let sut = TestSentryViewHierarchy() + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10)) + fixture.uiApplication.windows = [window] + + let ex = expectation(description: "Running on background Thread") + let dispatch = DispatchQueue(label: "background") + dispatch.async { + let _ = sut.fetch() + ex.fulfill() + } + + wait(for: [ex], timeout: 1) + XCTAssertTrue(fixture.uiApplication.calledOnMainThread, "fetchViewHierarchy is not using the main thread to get UI windows") + } + class TestSentryUIApplication: SentryUIApplication { private var _windows: [UIWindow]? + private var _calledOnMainThread = true + + var calledOnMainThread : Bool { + return _calledOnMainThread + } override var windows: [UIWindow]? { get { + _calledOnMainThread = Thread.isMainThread return _windows } set { + _calledOnMainThread = Thread.isMainThread _windows = newValue } } From 32f97070e2d584f52b3d2771f8dd52478423117c Mon Sep 17 00:00:00 2001 From: Dhiogo Ramos Brustolin Date: Thu, 19 Jan 2023 10:23:46 +0100 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 711f4acc7e4..f3746058117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Gather profiling timeseries metrics for CPU usage and memory footprint, and thermal and memory pressure events (#2493) +### Fixes + +- Reads UI Windows in the main thread while fetching view hierarchy (#2629) + ## 8.0.0 ### Features From 38f2d89f4ad8789b7314a893cadb0abc3910ab00 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Thu, 19 Jan 2023 09:24:18 +0000 Subject: [PATCH 3/4] Format code --- Sources/Sentry/SentryViewHierarchy.m | 3 ++- Tests/SentryTests/SentryViewHierarchyTests.swift | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Sentry/SentryViewHierarchy.m b/Sources/Sentry/SentryViewHierarchy.m index 0f08f8324d5..b55155be5bc 100644 --- a/Sources/Sentry/SentryViewHierarchy.m +++ b/Sources/Sentry/SentryViewHierarchy.m @@ -51,7 +51,8 @@ - (NSData *)fetchViewHierarchy __block NSMutableData *result = [[NSMutableData alloc] init]; void (^save)(void) = ^{ - NSArray *windows = [SentryDependencyContainer.sharedInstance.application windows]; + NSArray *windows = + [SentryDependencyContainer.sharedInstance.application windows]; if (![self processViewHierarchy:windows addFunction:writeJSONDataToMemory diff --git a/Tests/SentryTests/SentryViewHierarchyTests.swift b/Tests/SentryTests/SentryViewHierarchyTests.swift index 10a4061cb23..318d1f6ce3b 100644 --- a/Tests/SentryTests/SentryViewHierarchyTests.swift +++ b/Tests/SentryTests/SentryViewHierarchyTests.swift @@ -192,7 +192,7 @@ class SentryViewHierarchyTests: XCTestCase { private var _windows: [UIWindow]? private var _calledOnMainThread = true - var calledOnMainThread : Bool { + var calledOnMainThread: Bool { return _calledOnMainThread } From f9a0af5fc711345f0c56c8eab0991e8c481928f1 Mon Sep 17 00:00:00 2001 From: Dhiogo Brustolin Date: Fri, 20 Jan 2023 11:40:43 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md Co-authored-by: Philipp Hofmann --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3746058117..42306c2efc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixes -- Reads UI Windows in the main thread while fetching view hierarchy (#2629) +- Always fetch view hierarchy on the main thread (#2629) ## 8.0.0