Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

OpenPanel.initialize blocks main thread / crashes on background thread due to WKWebView + DispatchSemaphore deadlock in getiOSUserAgent

Đang mở
#6 0 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
swift
Lĩnh vực
mobile

Hướng nghiên cứu

Start with DeviceInfo.getiOSUserAgent and trace how OpenPanel.initialize obtains and uses the user agent. Verify the behavior when initialization is called on both the main and a background thread, then make completion or fallback handling non-blocking while preserving the expected user-agent value.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

OpenPanel.initialize is not safe to call from either the main thread or a background thread due to a deadlock in the internal getiOSUserAgent implementation. This makes it impossible to initialize the SDK without causing either a crash or a ~1 second UI freeze.
Root cause
Inside DeviceInfo.getiOSUserAgent():

let webView = WKWebView(frame: .zero)           // (1) must be on main thread
var userAgent = ""
let semaphore = DispatchSemaphore(value: 0)

DispatchQueue.main.async {                      // (2) queued on main thread
    webView.evaluateJavaScript("navigator.userAgent") { (result, error) in
        if let agent = result as? String { userAgent = agent }
        semaphore.signal()
    }
}

_ = semaphore.wait(timeout: .now() + 1.0)      // (3) blocks calling thread

This creates two mutually exclusive failure scenarios:
Scenario A — Called from main thread:
Step (1) is fine.
Step (3) blocks the main thread via semaphore.wait.
Step (2)'s main.async block is queued but can never execute because the main thread is blocked.
The semaphore always times out after 1 full second, freezing the UI every time.
Scenario B — Called from a background thread:
Step (1) crashes immediately with Main Thread Checker: UI API called on a background thread: -[WKWebView .cxx_construct].

Observed crash (background thread):

Main Thread Checker: UI API called on a background thread: -[WKWebView .cxx_construct]
Queue name: com.apple.root.utility-qos

#5  OpenPanel.DeviceInfo.getiOSUserAgent()
#6  OpenPanel.DeviceInfo.getUserAgent()
#7  OpenPanel.OpenPanel.initialize(options:)

Observed freeze (main thread): OpenPanel.initialize always blocks the calling thread for exactly 1 second (the semaphore timeout), regardless of device speed, because the JS evaluation can never signal the semaphore while the thread is waiting on it.
Expected behavior
OpenPanel.initialize should be safe to call from the main thread without blocking it. The user agent should be fetched asynchronously and either:
Cached lazily on first use after a non-blocking async fetch, or
Fetched during initialize without blocking (e.g. completion-based), or
Simply use getBasicUserAgent() as the default and upgrade to the WKWebView-based UA once it becomes available asynchronously.

Suggested fix:

private static func getiOSUserAgent(completion: @escaping (String) -> Void) {
    DispatchQueue.main.async {
        let webView = WKWebView(frame: .zero)
        webView.evaluateJavaScript("navigator.userAgent") { result, _ in
            let agent = (result as? String) ?? getBasicUserAgent()
            completion(agent + " OpenPanel/\(OpenPanel.sdkVersion)")
        }
    }
}
Ngôn ngữ chính
Swift
Star
5
Fork
8
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Openpanel-dev/swift-sdk

Tất cả issue của Openpanel-dev/swift-sdk

Issue tương tự

Thêm issue về Swift

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.