First API Call After Idle State Fails on Railway Serverless (Bun + Hono + Drizzle)
aakaul
HOBBYOP

4 months ago

I’m using Bun + Hono + Drizzle + Upstash + Neon on Railway, with the "serverless" option enabled.

The issue: the first API call after the service has been idle (sleep state) always returns a 502 error. When I hit the same endpoint again—after the server wakes up—it works fine.

2025-11-19T22:01:01.000000000Z [inf]  Starting Container
2025-11-19T22:01:03.334066312Z [inf]  api start: {"level":30,"time":"2025-11-19T22:01:01.755Z","service":"reviewsFlow","env":"production","msg":"Server is up"}
2025-11-19T22:01:03.334080881Z [inf]  api start: {"level":30,"time":"2025-11-19T22:01:01.755Z","service":"reviewsFlow","env":"production","msg":"✅ Application started successfully"}
2025-11-19T22:01:03.334089633Z [inf]  api start: {"level":30,"time":"2025-11-19T22:01:01.750Z","service":"reviewsFlow","env":"production","msg":"🚀 Server starting","port":3001}
2025-11-19T22:01:03.334096794Z [inf]  api start: {"level":50,"time":1763589662064,"pid":11,"hostname":"a10e92dff785","type":"console.error","args":["Error occurred:",{"error":"The connection was closed.","stack":"","url":"http://api.reviewsflow.in/v1/whatsapp/webhook/challenge/***","method":"POST","headers":{"accept":"*/*","accept-encoding":"gzip, br","cdn-loop":"cloudflare; loops=1","cf-connecting-ip":"49.36.99.106","cf-ipcountry":"IN","cf-ray":"9a12fbce5e662bd5-FRA","cf-visitor":"{\"scheme\":\"https\"}","content-length":"463","content-type":"application/json","host":"api.reviewsflow.in","postman-token":"1c323dac-15ef","user-agent":"PostmanRuntime/7.43.0","x-apikey":"****","x-forwarded-for":"49.36.99.106, 172.71.247.55","x-forwarded-host":"api.reviewsflow.in","x-forwarded-proto":"https","x-railway-edge":"railway/europe-west4-drams3a","x-railway-request-id":"c5AK8qoDT9CzN5ZaYqdHTg","x-real-ip":"172.71.247.55","x-request-start":"1763589659987"},"timestamp":"2025-11-19T22:01:02.064Z"}],"msg":"console.error"}
2025-11-19T22:01:03.334107722Z [inf]  api start: {"level":50,"time":"2025-11-19T22:01:02.064Z","service":"reviewsFlow","env":"production","req_id":"5eadbd59-a097-421c-9e5e-dd3dc037b614","ip":"49.36.99.106, 172.71.247.55","ua":"PostmanRuntime/7.43.0","msg":"Handled error response","status":500,"response":{"ok":false,"error":"AbortError","message":"The connection was closed.","timestamp":"2025-11-19T22:01:02.064Z","path":"http://api.reviewsflow.in/v1/whatsapp/webhook/challenge/***"}}
Solved

2 Replies

Railway
BOT

4 months 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!


aakaul
HOBBYOP

4 months ago

solved it by rearrnaging connections setup.

Earlier connection was setup at request call.

import { createApp } from "./config/app";
import { initializeFirebase } from "./config/firebase";
import { createLogger, getLoggerConfig } from "./config/logger";
import { getServerPort, startServer } from "./config/server";
import { getConfig } from "./config/validation";

/**
 * Bootstrap application - initialize Firebase and start server
 * Simple and fast for Railway container-based serverless
 */
export function bootstrap(): void {
	const logger = createLogger(getLoggerConfig());
	// Create app
	const app = createApp({ logger, config: getConfig() });

	startServer({
		port: getServerPort(),
		app,
		logger,
	});
	logger.info({ msg: "✅ Application started successfully" });
	initializeFirebase();
}

changed to

export function bootstrap(): void {
	const logger = createLogger(getLoggerConfig());
	// Create app
	const app = createApp({ logger, config: getConfig() });

	startServer({
		port: getServerPort(),
		app,
		logger,
	});
	logger.info({ msg: "✅ Application started successfully" });

	void (async () => {
		try {
			await Promise.all([
				import("./config/firebase"),
				import("./lib/queue"),
				import("@repo/database"),
			]);

			logger.info({ msg: "🔥 All background services initialized" });
		} catch (err) {
			logger.error({ msg: "❌ Background initialization failed", error: err });
		}
	})();
}

Status changed to Solved brody 4 months ago


Loading...