Skip to content

[HIGH] Server starts without database connection and runs with degraded functionality #1303

Description

@saidai-bhuvanesh

Description

The server continues to start even when MongoDB connection fails, leading to degraded functionality without proper error handling.

Location

File: backend/server.js:78-88

connectDB()
  .then((success) => {
    if (success) {
      console.log("MongoDB connected successfully");
    } else {
      console.warn("⚠️ Failed to connect to MongoDB - server will run without database connection");
    }
  })
  .catch((err) => {
    console.error("Database connection error:", err.message);
  });

// Server starts here even if DB failed...
const server = app.listen(PORT, "0.0.0.0", () => {
  // Server runs without database
});

Root Cause

  1. Server starts regardless of MongoDB connection status
  2. No health check for database availability
  3. API endpoints return confusing errors when DB is down

Impact

  • Users get cryptic errors instead of clear service unavailable message
  • Background jobs may fail silently
  • Data inconsistency if writes are attempted
  • Difficult debugging in production

Recommended Fix

// Block server startup until DB is ready (or timeout)
async function startServer() {
  try {
    await connectDB();
    console.log("MongoDB connected successfully");
  } catch (err) {
    console.error("FATAL: Cannot connect to MongoDB:", err.message);
    console.error("Exiting - database is required for this service");
    process.exit(1);
  }

  app.listen(PORT, "0.0.0.0", () => {
    console.log(`Server running on port ${PORT}`);
  });
}

startServer();

Severity

HIGH - Operational reliability issue

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