         guard schemeTaskIsActive(urlSchemeTask: urlSchemeTask) else {
             return
         }
-        
-        let callback = Session.Callback(response: {  response in
-            DispatchQueue.main.async {
+       
+        // IMPORTANT: Ensure the urlSchemeTask is not strongly captured by the callback blocks.
+        // Otherwise it will sometimes be deallocated on a non-main thread, causing a crash https://phabricator.wikimedia.org/T224113
+        let callback = Session.Callback(response: {  [weak urlSchemeTask] response in
+            DispatchQueue.main.async {
+                guard let urlSchemeTask = urlSchemeTask else {
+                    return
+                }
                 guard self.schemeTaskIsActive(urlSchemeTask: urlSchemeTask) else {
                     return
                 }

        
        removeSchemeTask(urlSchemeTask: urlSchemeTask)
        
        guard let task = activeSessionTasks[urlSchemeTask.request] else {
            return
        }
        
        removeSessionTask(request: urlSchemeTask.request)
        
        switch task.state {
        case .canceling:
            fallthrough
        case .completed:
            break
        default:
            task.cancel()
        }
    }
}

                removeSchemeTask(urlSchemeTask: urlSchemeTask)
                return
            }
            // IMPORTANT: Ensure the urlSchemeTask is not strongly captured by this block operation
            // Otherwise it will sometimes be deallocated on a non-main thread, causing a crash https://phabricator.wikimedia.org/T224113
            let op = BlockOperation { [weak urlSchemeTask] in
                if let cachedResponse = self.defaultHandler.cachedResponseForURL(defaultURL) {
                    DispatchQueue.main.async {
                        guard let urlSchemeTask = urlSchemeTask else {
                            return
                        }
                        self.activeCacheOperations.removeValue(forKey: urlSchemeTask.request)
                        urlSchemeTask.didReceive(cachedResponse.response)
                        urlSchemeTask.didReceive(cachedResponse.data)
                        urlSchemeTask.didFinish()
