Skip to content

FIRDocumentReference.setData() blocks main thread on __psynch_mutexwait for seconds #15505

@xaphod

Description

@xaphod

Description

Our app makes its Firestore setData() calls on main thread, because we understood that the SDK doesn't do heavy work on the main thread.

However we have multiple reports of the main thread being blocked for several seconds. Here's an example stacktrace:

  libsystem_kernel    0x1eb9c3f18  __psynch_mutexwait
  libsystem_pthread   0x224f42b64  _pthread_mutex_firstfit_lock_wait
  libsystem_pthread   0x224f42c94  _pthread_mutex_firstfit_lock_slow
  libc++.1            0x1ab4db404  std::__1::mutex::lock
  FirebaseFirestoreInternal0x104880568  std::__1::lock_guard<T>::lock_guard[abi:ne200100] (lock_guard.h:33)
  FirebaseFirestoreInternal0x104880568  std::__1::lock_guard<T>::lock_guard[abi:ne200100] (lock_guard.h:32)
  FirebaseFirestoreInternal0x104880568  firebase::firestore::util::AsyncQueue::is_running (async_queue.cc:107)
  FirebaseFirestoreInternal0x1048e15c4  firebase::firestore::core::FirestoreClient::is_terminated (firestore_client.cc:420)
  FirebaseFirestoreInternal0x1048e15c4  firebase::firestore::core::FirestoreClient::VerifyNotTerminated (firestore_client.cc:409)
  FirebaseFirestoreInternal0x1048e15c4  firebase::firestore::core::FirestoreClient::WriteMutations (firestore_client.cc:519)
  FirebaseFirestoreInternal0x1048afa84  firebase::firestore::api::DocumentReference::SetData (document_reference.cc:98)
  FirebaseFirestoreInternal0x1048cb66c  -[FIRDocumentReference setData:merge:completion:] (FIRDocumentReference.mm:168)
  FirebaseFirestore   0x1043574fc  FIRDocumentReference.setData<T> (DocumentReference+WriteEncodable.swift:66)
  <our app>               0x103016464  FirebaseBackedObject.persistChanges (FirebaseObject.swift:89)

Please can you advise me:

  1. is this normal behavior or is something wrong?
  2. if normal, then, what are the best threading practices for keeping main thread unblocked?

Thank you for your help.

Reproducing the issue

No response

Firebase SDK Version

12.5.0

Xcode Version

26.1

Installation Method

CocoaPods

Firebase Product(s)

Firestore

Targeted Platforms

macCatalyst, iOS

Relevant Log Output

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
Replace this line with the contents of your Package.resolved.

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
Replace this line with the contents of your Podfile.lock!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions