pythonfirebasegoogle-cloud-firestorefirebase-admin

Firebase admin taking an infinite time to work


I recently started using firebase admin in python. I created this example script:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate("./services.json")
options = {
    "databaseURL": 'https://not_revealing_my_url.com'
}
app = firebase_admin.initialize_app(cred, options)

client = firestore.client(app)
print(client.document("/").get())

I already activated firestore and I placed services.json (which I genrated from "Service Accounts" on my firebase project) in the same directory as my main.py file.

From all sources I could find, this should've allowed me to use firestore, but for some reason the app takes an infinite long time to respond.

I tried looking through the stack after Interrupting the script, and the only major thing I could find was:

grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses; last error: UNKNOWN: ipv6:%5B::1%5D:8081: tcp handshaker shutdown"
        debug_error_string = "UNKNOWN:Error received from peer  {grpc_message:"failed to connect to all addresses; last error: UNKNOWN: ipv6:%5B::1%5D:8081: tcp handshaker shutdown", grpc_status:14, created_time:"2025-05-11T08:47:32.8676384+00:00"}"

I am assuming this is a common issue, but I failed to find any solution online, can someone help me out?

EDIT: I had firebase emulator working from a previous job, It seems firebase_admin tried using firebase emulator which was inactive. I just had to remove it from my PATH


Solution

  • Yes, you're on the right track with setting up Firebase Admin in Python. The error you're seeing:

    grpc._channel._MultiThreadedRendezvous: 
    status = StatusCode.UNAVAILABLE
    details = "failed to connect to all addresses; last error: UNKNOWN: ipv6:[::1]:8081: tcp handshaker shutdown"
    

    strongly suggests that the client is trying to connect to Firestore Emulator, not the actual Firestore production database.

    Root Cause

    This specific port 8081 and the loopback address ([::1]) are the default for the Firestore Emulator, not the production Firestore service. So your environment is likely set to use the emulator without the emulator actually running.

    Fix

    You likely have one of the following environment variables set (either globally or in your shell/session):

    If either of these is set, the SDK will try to connect to a local emulator instead of the actual Firestore instance.

    Solution Steps

    1. Check and Unset the Environment Variable(s):

    In your terminal (Linux/macOS):

    unset FIRESTORE_EMULATOR_HOST
    unset FIREBASE_FIRESTORE_EMULATOR_ADDRESS
    

    In PowerShell (Windows):

    Remove-Item Env:FIRESTORE_EMULATOR_HOST
    Remove-Item Env:FIREBASE_FIRESTORE_EMULATOR_ADDRESS
    

    Or, if these are set in your IDE or .env file, remove them from there.

    1. Restart Your Application after unsetting the variables.

    2. Verify you're not connecting to the emulator:

    In your Python script, you should not manually configure emulator settings unless you're developing against the emulator.

    Your firebase_admin.initialize_app() call is correct for connecting to the live Firestore.


    Also: Your Script Has a Small Issue

    This line:

    print(client.document("/").get()) 
    

    Is not valid Firestore usage. client.document("/") is not a valid document path. Firestore document paths must include both collection and document ID. E.g.:

    doc_ref = client.document("test_collection/test_document")
    print(doc_ref.get().to_dict())