Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) whenever I try to make a network request

I have been getting a weird crash whenever I run my app and use my request utilities.

Here is the code:

Firstly, I have a button that submits the data when clicked

Button(action: { Task { await viewModel.submitData() } } ) {
     Text("Log In")
}

This is the submitData function

func submitData() async -> Void {
    state = .loading
     
    do {
      let result = try await AuthenticationAPI.login(user: user)
       
      self.state = .success
      self.authenticator.authenticate(token: result.value.token)
    } catch let error as APIError {
      self.handleAPIError(error)
    } catch {
      // TODO: Log Error
      self.handleAPIError(.unexpectedError)
    }
  }

This is the AuthenticationAPI.login function

   static func login(user: LoginUser) async throws -> APIService.Response<AuthenticatedUser> {
    let endpoint = Endpoint.login
     
    let data = try? encode(user.toDictionary())
     
    return try await apiService.post(type: AuthenticatedUser.self,
                url: endpoint.url, body: data)
  }

Lastly this is the network call

  func executeRequest<T: Decodable>(_ request: URLRequest) async throws -> Response<T> {
    guard let (data, response) = try? await URLSession.shared.data(for: request) else {
        throw APIError.networkError
    }

     
    if let response = response as? HTTPURLResponse {
      if (200...299).contains(response.statusCode) {
        guard let data = try? decoder.decode(T.self, from: data) else {
          throw APIError.decodingError
        }
         
        return Response(value: data, response: response)
      } else if (400...499).contains(response.statusCode) {
        throw APIError.httpError(data, response)
      } else if response.statusCode == 500 {
        throw APIError.serverError
      }
    }
     
    throw APIError.unexpectedError
  }

  func post<T: Decodable>(
    type: T.Type,
    url: URL,
    headers: Headers = [:],
    body: Data?
  ) async throws -> Response<T> {
    var request = URLRequest(url: url)
     
    request.httpMethod = "POST"
    request.httpBody = body
     
    constructHeaders(request: &request, headers: headers)
     
    return try await executeRequest(request) // This is where the compiler tells me the error occurs
   
  }

The executeRequest function is never called because it never makes the request (I checked my local server).

These are the thread logs:

Thread 1 Queue : com.apple.main-thread (serial)
#0	0x00007fff30bb03f4 in swift::TargetMetadata<swift::InProcess>::isCanonicalStaticallySpecializedGenericMetadata() const ()
#1	0x00007fff30bbc7b1 in swift_checkMetadataState ()
#2	0x00007fff30b713d5 in type metadata completion function for ClosedRange<>.Index ()
#3	0x00007fff30bbf75d in swift::MetadataCacheEntryBase<(anonymous namespace)::GenericCacheEntry, void const*>::doInitialization(swift::ConcurrencyControl&, swift::MetadataCompletionQueueEntry*, swift::MetadataRequest) ()
#4	0x00007fff30bb1941 in _swift_getGenericMetadata(swift::MetadataRequest, void const* const*, swift::TargetTypeContextDescriptor<swift::InProcess> const*) ()
#5	0x00007fff30b8b440 in __swift_instantiateCanonicalPrespecializedGenericMetadata ()
#6	0x00000001017f9e00 in APIService.post<τ_0_0>(type:url:headers:body:) at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/Services/APIService.swift:124
#7	0x00000001017a0dd0 in static AuthenticationAPI.login(user:) at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/Networking/Authentication/AuthenticationAPI.swift:21
#8	0x000000010170b3c0 in LoginViewModel.submitData() at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/App/Login/ViewModels/LoginViewModel.swift:42
#9	0x00000001016317d0 in closure #1 in closure #1 in closure #2 in closure #1 in closure #1 in LoginView.body.getter at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/App/Login/Views/LoginView.swift:26
#10	0x0000000101637960 in partial apply for closure #1 in closure #1 in closure #2 in closure #1 in closure #1 in LoginView.body.getter ()
#11	0x0000000101631ae0 in thunk for @escaping @callee_guaranteed @Sendable @async () -> () ()
#12	0x0000000101637ac0 in partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> () ()
#13	0x00000001016353f0 in thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out τ_0_0) ()
#14	0x0000000101637d70 in partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out τ_0_0) ()

This is on XCode Beta 13.5

I should mention that I am using the Thread Sanitizer too. This error only appears with the Thread Sanitizer turned on.

From the stack trace, you may hit some generics related bug of Swift Concurrency. Have you tried making post and executeRequest non-generic or adding an explicit type parameter to executeRequest?

Just tried it. It worked fine with an explicit type. As you said, it's a bug. Is there any workaround? Thanks

Sorry, I do not know. Please share yours when you find one.

Alright, Thanks. For now I just disabled the Thread Sanitizer and reported it on the Feedback Assistant.

I have been getting a weird crash whenever I run my app and use my request utilities.

Can you post a full Apple crash report for this?

See Posting a Crash Report for advice on how to do this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) whenever I try to make a network request
 
 
Q