RealtimeDatabase randomly closes connection – Connection interrupted for: connection_idle

Step 2: Describe your environment

  • Android Studio version: 4.1.1
  • Firebase Component: Database
  • Component version: 19.6.0

Step 3: Describe the problem

App randomly loses connection with Realtime Database before trying to get some data from it. When the issue occurs, restarting the app solves the problem but it is annoying for the users. Reproduced on Samsung S10+ (Android 10) and Nexus 5X (Android 8.1).
The strange thing is when get() call fails and the app tries to write something into the Realtime Databases, right away, the set call works.

Steps to reproduce:

It happens randomly, like 3 out of 10 app starts or GET calls.
Log:

2021-01-30 16:12:12.210 9157-9599/rs.fourexample.oab D/PersistentConnection: pc_0 - Connection interrupted for: connection_idle
2021-01-30 16:12:12.221 9157-9599/rs.fourexample.oab D/Connection: conn_0 - closing realtime connection
2021-01-30 16:12:12.221 9157-9599/rs.fourexample.oab D/WebSocket: ws_0 - websocket is being closed
2021-01-30 16:12:12.224 9157-9599/rs.fourexample.oab D/PersistentConnection: pc_0 - Got on disconnect due to OTHER
2021-01-30 16:12:12.372 9157-9599/rs.fourexample.oab D/WebSocket: ws_0 - closed
2021-01-30 16:13:07.094 9157-9166/rs.fourexample.oab I/zygote64: Debugger is no longer active
2021-01-30 16:13:08.682 9157-9599/rs.fourexample.oab D/Persistence: Starting transaction.
2021-01-30 16:13:08.687 9157-9599/rs.fourexample.oab D/Persistence: Saved new tracked query in 3ms
2021-01-30 16:13:08.705 9157-9599/rs.fourexample.oab D/Persistence: Transaction completed. Elapsed: 22ms
2021-01-30 16:13:11.708 9157-9599/rs.fourexample.oab D/PersistentConnection: pc_0 - get 1 timed out waiting for connection
2021-01-30 16:13:11.713 9157-9157/rs.fourexample.oab I/RepoOperation: get for query /bloodRequests/rs falling back to cache after error: Client is offline
2021-01-30 16:13:11.715 9157-9157/rs.fourexample.oab D/Persistence: Starting transaction.
2021-01-30 16:13:11.718 9157-9157/rs.fourexample.oab D/Persistence: Saved new tracked query in 2ms
2021-01-30 16:13:11.726 9157-9157/rs.fourexample.oab D/Persistence: Transaction completed. Elapsed: 9ms
2021-01-30 16:13:11.741 9157-9157/rs.fourexample.oab E/BloodRequestService: java.lang.Exception: Client is offline
        at com.google.firebase.database.connection.PersistentConnectionImpl$2.run(PersistentConnectionImpl.java:432)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

Relevant Code:

Some of the Queries this occurs

// BloodRequestService.kt
val dataSnapshot = firebaseRoutes.getBloodRequestsReference(countryCode)
                .orderByChild("isActive").equalTo(true)
                .limitToFirst(20)
                .get()
                .await()

// UserService.kt
val dataSnapshot = firebaseRoutes.getUsersReference()
                .orderByKey()
                .equalTo(userId)
                .limitToFirst(1)
                .get()
                .await()

Dependencies

    // firebase
    implementation platform('com.google.firebase:firebase-bom:26.4.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-database-ktx'
    implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.1.1'

    // google stuff
    implementation 'com.google.android.gms:play-services-auth:19.0.0'

1 possible answer(s) on “RealtimeDatabase randomly closes connection – Connection interrupted for: connection_idle