Skip to content

@init Doesn't seem to run locally before function execution #251

@Bradley-McCallion

Description

@Bradley-McCallion

According to the documentation:

The init function can be used as a hook for cold-start initialization code - it is a way to register code that should run once, during cold start, right after the Functions Framework loads your module and before it starts handling requests.

From some local testing however, I can't get this function to run. When I spin up the emulator locally with firebase emulators:start --project testing --only functions I can see that the function discovery is executed but the @init function is not executed (as expected). However, after calling one of the functions I would have expected the @init function to run but it doesn't seem to.

My example code is something like this:

from firebase_functions.core import init
from firebase_admin import initialize_app

print("-----------------Main module loaded-----------------------------", flush=True)

@init
def initialize() -> None:
    print("---------------------Initializing----------------------------------", flush=True)

app = initialize_app()


from functions import *

On starting the emulator I can see:

 * Running on http://127.0.0.1:8083
Press CTRL+C to quit

-----------------Main module loaded-----------------------------

127.0.0.1 - - [19/Sep/2025 10:26:15] "GET /__/functions.yaml HTTP/1.1" 200 -

127.0.0.1 - - [19/Sep/2025 10:26:15] "GET /__/quitquitquit HTTP/1.1" 200 -

Terminated

✔  functions: Loaded functions definitions from source: contact_us.

Confirming that the main module gets called. But after executing a function I would have expected to see debug line within the initialize() statement. However, after executing all I see is:

i  functions: Loaded environment variables from .env, .env.demo-abcd.
>  -----------------Main module loaded-----------------------------
>   * Serving Flask app 'contact_us'
>   * Debug mode: on
>  WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
>   * Running on http://127.0.0.1:8825
>  Press CTRL+C to quit
>   * Restarting with watchdog (inotify)
>  -----------------Main module loaded-----------------------------
>   * Debugger is active!
>   * Debugger PIN: 136-720-496
>  127.0.0.1 - - [19/Sep/2025 10:26:33] "GET /__/health HTTP/1.1" 400 -
i  functions: Beginning execution of "europe-west2-contact_us"
>  127.0.0.1 - - [19/Sep/2025 10:26:33] "POST / HTTP/1.1" 200 -
i  functions: Finished "europe-west2-contact_us" in 621.762172ms

The documentation does say something about in production so does this mean the @init won't work locally?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions