     static func clearAll()
         {
         Self.requestInitialization.await
-            { stubs = [] }
+            {
+            synchronized { stubs = [] }
+            }
         }
 
     override class func canInit(with request: URLRequest) -> Bool


    override func stopLoading()
        { }
    }

class RequestStub
    {
    let matcher: RequestPattern
    let response: NetworkStubResponse


    override class func canonicalRequest(for request: URLRequest) -> URLRequest
        {
        defer { Self.requestInitialization.decrement() }

        let body = extractBody(from: request)

        let stubs = synchronized { Self.stubs }
        guard let stub = stubs.first(where: { $0.matcher.matches(request, withBody: body) }) else
            {
            fatalError(
                """
                Unstubbed network request:
                    \(request.httpMethod ?? "<nil method>") \(request.url?.absoluteString ?? "<nil URL>")
                    headers: \(request.allHTTPHeaderFields ?? [:])
                    body: \(body?.description ?? "nil")

                Available stubs:
                    \(stubs.map { $0.description }.joined(separator: "\n    "))

                Halting tests
                """)
            }

        let mutableRequest = request as! NSMutableURLRequest
        URLProtocol.setProperty(stub, forKey: stubPropertyKey, in: mutableRequest)
        return mutableRequest as URLRequest
        }

    private static func extractBody(from request: URLRequest) -> Data?
