ECONNREFUSED when calling another service in a project

8 months ago

I've a backend service (fastAPI) that I've verified is up and running.

INFO: Started server process [1]

INFO: Waiting for application startup.

INFO: Application startup complete.

INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

From my frontend (next.js), I would like to call this API. However, I'm getting this error:

Starting optimization process with http://label-hpo.railway.internal:8000/optimize

Error starting optimization: TypeError: fetch failed

at node:internal/deps/undici/undici:12618:11

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

at async l (/app/.next/server/app/api/optimize/route.js:1:1167)

at async /app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36938

at async eC.execute (/app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:27552)

at async eC.handle (/app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:38272)

at async doRender (/app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1345:42)

at async cacheEntry.responseCache.get.routeKind (/app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1567:28)

at async NextNodeServer.renderToResponseWithComponentsImpl (/app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1475:28)

at async NextNodeServer.renderPageComponent (/app/node_modules/.pnpm/next@14.2.13_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1901:24) {

cause: Error: connect ECONNREFUSED fd12:cf85:406c::e5:807b:e99b:8000

at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)

at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:128:17) {

errno: -111,

code: 'ECONNREFUSED',

syscall: 'connect',

address: 'fd12:cf85:406c::e5:807b:e99b',

port: 8000

}

}

A few things I've tried:
- Confirmed that the fastAPI app can connect to frontend on local when calling http://localhost:8000
- Tried calling by backend via label-hpo.railway.internal and label-hpo

What should I do to enable the connection between these two services in the same project?

Solved

2 Replies

8 months ago

Your fastapi app needs to listen on IPv6 since the private network is IPv6 only, this is done by binding to ::

Please note that uvicorn doesn't support dual stack binding (IPv6 & IPv4) so while binding to :: will allow it to work across the private network, you will lose the ability to access it publicly, if you need both, please use hypercorn instead.


8 months ago

Thank you for the fast response
Confirmed the fix works.

Resolved.


Status changed to Solved brody 8 months ago


ECONNREFUSED when calling another service in a project - Railway Help Station