         
         let localCompletionBlock: (URLResponse?, Data?, Error?) -> Void = { (response, data, error) in
             
+            assert(Thread.isMainThread)
+            
+            guard self.schemeTaskIsActive(urlSchemeTask: urlSchemeTask) else {
+                return
+            }
+            
             if response == nil && error == nil {
                 urlSchemeTask.didFailWithError(SchemeHandlerError.unexpectedResponse)
+                self.activeSchemeTasks.remove(urlSchemeTask)
                 return
             }
             
             if let error = error {
                 urlSchemeTask.didFailWithError(error)
+                self.activeSchemeTasks.remove(urlSchemeTask)
                 return
             }
             

           
        default:
            
            guard let defaultURL = defaultHandler.urlForPathComponents(pathComponents, requestURL: requestURL) else {
                urlSchemeTask.didFailWithError(SchemeHandlerError.invalidParameters)
                return
            }
            

    }
    
    func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
        assert(Thread.isMainThread)
        guard let task = self.activeSessionTasks[urlSchemeTask.request] else {
            return
        }
        if task.state == .running {
            task.cancel()
        }
        self.activeSessionTasks.removeValue(forKey: urlSchemeTask.request)
        activeSchemeTasks.remove(urlSchemeTask)
    }
}

