firebase / firebase/firebase-cpp-sdk

[Bug] Deadlock in firebase::auth::IdTokenRefreshThread

Abierto
#1,445 1 comentario 0 reacciones 0 asignados Ver en GitHub
api: auth new type: question
Lenguaje dominante
C++
Estrellas
326
Forks
137
Merge medio
3 d 9 h
PR fusionados (30 d)
5

Descripción

### [REQUIRED] Please fill in the following fields:

* Pre-built SDK from the [website](https://firebase.google.com/download/cpp) or open-source from this repo: open-source from this repo
* Firebase C++ SDK version: 11.4.0
* Problematic Firebase Component: auth
* Other Firebase Components in use: database
* Platform you are using the C++ SDK on: Mac, Windows
* Platform you are targeting: desktop

### [REQUIRED] Please describe the issue here:
Deadlock in IdTokenRefreshThread which causes the entire app to hang, e.g., Database cannot be safely deleted since it is deadlocked on destruction by waiting IdTokenRefreshThread to finish

1. IdTokenRefreshThread holds `refresh_thread->ref_count_mutex_` and `auth->auth_data_->future_impl.mutex()`. Then it accesses `refresh_thread->token_refresh_listener_.GetTokenTimestamp()` which has its own mutex
```
#0 firebase::auth::IdTokenRefreshThread::Initialize(firebase::auth::AuthData*)::$_4::operator()(firebase::auth::IdTokenRefreshThread*) const at /Users/venhe/tmp/Firebase/Firebase/auth/src/desktop/auth_desktop.cc:818
#1 firebase::auth::IdTokenRefreshThread::Initialize(firebase::auth::AuthData*)::$_4::__invoke(firebase::auth::IdTokenRefreshThread*) at /Users/venhe/tmp/Firebase/Firebase/auth/src/desktop/auth_desktop.cc:805
```

2. Another worker thread fires an event about token change. `IdTokenRefreshListener::OnIdTokenChanged` locks its own mutex first and then `auth->auth_data_->future_impl.mutex()`
```
#0 firebase::auth::IdTokenRefreshListener::OnIdTokenChanged(firebase::auth::Auth*) at /Users/venhe/tmp/Firebase/Firebase/auth/src/desktop/auth_desktop.cc:135
#1 firebase::auth::NotifyIdTokenListeners(firebase::auth::AuthData*) at /Users/venhe/tmp/Firebase/Firebase/auth/src/auth.cc:362
#2 0firebase::auth::AuthenticationResult::SetAsCurrentUser(firebase::auth::AuthData*) const at /Users/venhe/tmp/Firebase/Firebase/auth/src/desktop/authentication_result.cc:64
#3 void firebase::auth::PerformSignInFlow(firebase::auth::AuthDataHandle*) at /Users/venhe/tmp/Firebase/Firebase/auth/src/desktop/sign_in_flow.h:125
```

Deadlock happens if IdTokenRefreshThread just about to call `GetTokenTimestamp` and if IdTokenRefreshListener::OnIdTokenChanged successfully acquired its mutex.

#### Steps to reproduce:

Hard to reproduce, but happens quite often.

What happened? How can we make the problem occur?

Simulate it, e.g., call IdTokenRefreshListener::OnIdTokenChanged in a loop, and it will definitely deadlock at some point

If you have a downloadable sample project that reproduces the bug you're reporting, you will
likely receive a faster response on your issue.

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Start in auth/src/desktop/auth_desktop.cc around IdTokenRefreshThread::Initialize at lines 805-818 and IdTokenRefreshListener::OnIdTokenChanged at line 135, then trace the related calls in auth/src/auth.cc, authentication_result.cc, and sign_in_flow.h. Reproduce the reported ordering with repeated OnIdTokenChanged calls and inspect the competing mutex acquisitions. Done means the simulated race no longer deadlocks and application or Database destruction can complete.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
cpp
Área
authentication, databases
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.