Skip to content

SentryDependencyContainer.init uses UIApplication.sharedApplication #6265

@philipphofmann

Description

@philipphofmann

Description

The SentryDependencyContainer.init can be called from a background thread. Before #5900 this was not problem, but since then the SentryDependencyContainer.initialize calls [[SentryDependencyContainer alloc] init]

+ (void)initialize
{
if (self == [SentryDependencyContainer class]) {
// We use two locks, because we don't want the dependencies to block the instance lock.
// Using two locks speeds up the accessing the dependencies around 5%, which is worth having
// the extra lock. Measured with self.measure in unit tests.
sentryDependencyContainerInstanceLock = [[NSObject alloc] init];
sentryDependencyContainerDependenciesLock = [[NSObject alloc] init];
instance = [[SentryDependencyContainer alloc] init];
}
}

The init method uses the UIApplication.sharedApplication here

_application = UIApplication.sharedApplication;

If somebody calls SenrySDK.start from a BG thread, we access the SentryDependencyContainer.initialize also on a BG thread.

+ (void)startWithOptions:(SentryOptions *)options
{
// We save the options before checking for Xcode preview because
// we will use this options in the preview
startOption = options;
if ([SentryDependencyContainer.sharedInstance.processInfoWrapper
.environment[SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY] isEqualToString:@"1"]) {
// Using NSLog because SentryLog was not initialized yet.
NSLog(@"[SENTRY] [WARNING] SentrySDK not started. Running from Xcode preview.");
return;
}

Test to reproduce

func testStartSDK_BGThread() {

    let expectation = self.expectation(description: "Start on background thread")

    DispatchQueue.global().async {
        SentrySDK.start { options in
            options.dsn = "whatever"
        }

        expectation.fulfill()
    }

    wait(for: [expectation], timeout: 5.0)
}

Xcode stacktrace

Image

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions