FastAPI App Crashing on Startup - Environment Variables Not Injected

baejuwan5316
FREEOP

a month ago

Dear Railway Support Team,

My FastAPI application (service name: gureum) in the project disciplined-recreation is consistently crashing on startup. The issue appears to be a Pydantic ValidationError related to missing environment variables.

The specific error from the deployment logs is as follows:

pydantic_core._pydantic_core.ValidationError: 4 validation errors for ConnectionConfig
MAIL_USERNAME: Input should be a valid string [type=string_type, input_value=None, input_type=NoneType] 
MAIL_PASSWORD: Input should be a valid string [type=string_type, input_value=None, input_type=NoneType] 
MAIL_SERVER: Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]   
MAIL_FROM: Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]     

This clearly indicates that the application is not receiving the MAIL_USERNAME, MAIL_PASSWORD, MAIL_SERVER, and MAIL_FROM environment variables, as they are being read as None.

Here is a summary of the troubleshooting steps I have already taken:

  • Verified Variables: The variables are correctly set in the 'Variables' tab for my service on the Railway dashboard.

  • Checked Formatting: I have confirmed there are no quotes or extra leading/trailing spaces in the variable names and values.

  • Re-added Variables: I have tried deleting and re-adding all the mail-related variables by typing them manually.

  • Refactored Code: The application code (main.py) has been refactored to defer all configuration (including email and database setup) into the @app.on_event("startup") function. This should ensure variables are available when the configuration is loaded.

  • Local Success: The application works perfectly fine in my local environment when run with a .env file containing the same variables.

  • Database Variable: The DATABASE_URL variable is also set, but the application crashes due to the mail configuration error before it can even attempt to connect to the database.

It appears that Railway is not injecting these specific environment variables into the application's runtime environment before the startup event, causing the app to crash immediately upon initialization.

Could you please investigate why the environment variables might not be passed correctly to my application service?

Thank you for your assistance.

Solved$10 Bounty

6 Replies

Railway
BOT

a month ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


baejuwan5316
FREEOP

a month ago

nooooooo whyyyyyyyyyy please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


baejuwan5316

nooooooo whyyyyyyyyyy please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

a month ago

Hey, can you show how your code reads the environment variables? Please also show a screenshot of your variables page to confirm that you have actually deployed them.


baejuwan5316
FREEOP

a month ago

Of course. Here is the requested information.

1. How the code reads the environment variables:

I'm using Python's standard os.getenv() function within the FastAPI startup event to read the variables. I've also added a debug print statement to log the values at the exact moment the application starts.

Here is the relevant code snippet from my main.py:

# --- App Lifespan Events (Startup/Shutdown) ---
@app.on_event("startup")
async def startup():
    # --- Email configuration moved into startup event ---
    app.state.email_config = {
        "MAIL_FROM": os.getenv("MAIL_FROM"),
        "MAIL_SERVER": os.getenv("MAIL_SERVER"),
        "MAIL_PORT": int(os.getenv("MAIL_PORT", 587)),
        "MAIL_USERNAME": os.getenv("MAIL_USERNAME"),
        "MAIL_PASSWORD": os.getenv("MAIL_PASSWORD"),
    }
    
    # --- Crucial Debugging Code ---
    # This line prints the variable values as they are read at startup.
    print(f"DEBUG: Variables loaded at startup: {app.state.email_config}")
    
    # --- Database configuration follows... ---

2. Screenshot of the Variables Page:

Here is a screenshot of my service's Variables tab, confirming that all required variables have been set.

3. The Definitive Proof of the Issue:

Despite the variables being correctly set in the dashboard, the debug log from the latest deployment proves that all variables are being passed as None to the application's runtime environment.

This is the output from the print statement in the code above:

DEBUG: Variables loaded at startup: {'MAIL_FROM': None, 'MAIL_SERVER': None, 'MAIL_PORT': 587, 'MAIL_USERNAME': None, 'MAIL_PASSWORD': None}

This clearly indicates that the issue is not with the code itself but with the variable injection mechanism of the platform. I've already tried all standard troubleshooting steps (re-deploying, changing restart policy, ensuring load_dotenv() is not called in production), but the problem persists.

Could you please investigate why the environment variables are not being correctly injected into my service?

Thank you for your help.


a month ago

You haven't deployed the changes after adding your variables, click the Deploy button.


uxuz

Hey, can you show how your code reads the environment variables? Please also show a screenshot of your variables page to confirm that you have actually deployed them.

baejuwan5316
FREEOP

a month ago

THANK YOUUUUUUUUUUUUUUUUUUUUU I LOVE YOUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU


Status changed to Solved brody about 1 month ago


Loading...