         }
 
         client?.urlProtocolDidFinishLoading(self)
+        session.finishTasksAndInvalidate()
     }
 }
 

           originalRequest.url != currentRequest.url {
                let response = URLResponse()
                client?.urlProtocol(self, wasRedirectedTo: currentRequest, redirectResponse: response)
                return
        }


                  let url = request.url,
                  url == request.mainDocumentURL else {
                client?.urlProtocol(self, didFailWithError: error)
                session.finishTasksAndInvalidate()
                return
            }

