From 26f277893fdee10502dec663c6c09f4f2d254e67 Mon Sep 17 00:00:00 2001 From: riz Date: Thu, 20 Nov 2025 01:14:27 +0000 Subject: [PATCH] Fix Bun.serve port binding and error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add explicit hostname: '0.0.0.0' to bind all interfaces - Add try/catch around server creation with proper error logging - Add server error handler for runtime exceptions - Add verification timeout to confirm server is actually listening - Enhanced debugging to identify port binding issues Fixes issue where server claimed success but wasn't actually listening on port 3000. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- pkgs/server/create.ts | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/pkgs/server/create.ts b/pkgs/server/create.ts index 91c67af..454e097 100644 --- a/pkgs/server/create.ts +++ b/pkgs/server/create.ts @@ -81,10 +81,13 @@ export const createServer = async () => { await ensureNotRunning(); console.log(`[DEBUG] Starting Bun.serve on port ${g.port}...`); - g.server = Bun.serve({ - port: g.port, - maxRequestBodySize: 1024 * 1024 * 128, - async fetch(req) { + + try { + g.server = Bun.serve({ + port: g.port, + maxRequestBodySize: 1024 * 1024 * 128, + hostname: "0.0.0.0", // Explicitly bind to all interfaces + async fetch(req) { const url = new URL(req.url) as URL; url.pathname = url.pathname.replace(/\/+/g, "/"); @@ -183,10 +186,32 @@ export const createServer = async () => { } return handle(req); - }, - }); + }, + error(error) { + console.error(`[ERROR] Server error:`, error); + return new Response(`Internal Server Error: ${error.message}`, { + status: 500, + statusText: "Internal Server Error", + }); + }, + }); - console.log(`[DEBUG] Server object created successfully!`); + console.log(`[DEBUG] Server object created successfully!`); + console.log(`[DEBUG] Server actually listening on ${g.server.hostname}:${g.server.port}`); + + // Verify the server is actually listening + setTimeout(() => { + if (g.server && g.server.port === g.port) { + console.log(`[DEBUG] ✓ Server verified to be listening on port ${g.port}`); + } else { + console.error(`[ERROR] ✗ Server not properly bound to port ${g.port}`); + } + }, 100); + + } catch (error) { + console.error(`[ERROR] Failed to start Bun.serve:`, error); + throw error; + } if (process.env.PRASI_MODE === "dev") { g.log.info(`http://localhost:${g.server.port}`);