Connection Lost on self-hosted instance

Hi,

I’m not able to fix a Connection Lost issue on my self-hosted instance, would appreciate any help.

Thanks!

Describe the problem/error/question

“Connection Lost” blinks on the top right corner of the n8n page

Firefox console show a WS connection issue as soon as I open a workflow. This is displayed every second

The Network tab of the browser does not show any error, every http request is returning a 200 status, and the ws requests are returning 101 status

When looking into one of the WS connections, we can see the server accepted it

And data is exchanged both ways before the connection being closed.

Docker logs:

Initializing n8n process
n8n ready on ::, port 5678
n8n Task Broker ready on 127.0.0.1, port 5679
Failed to start Python task runner in internal mode. because Python 3 is missing from this system. Launching a Python runner in internal mode is intended only for debugging and is not recommended for production. Users are encouraged to deploy in external mode. See: https://docs.n8n.io/hosting/configuration/task-runners/#setting-up-external-mode
[license SDK] Skipping renewal on init: license cert is not due for renewal
Registered runner "JS Task Runner" (VkVAkoh7lA58iPNtk0XNr) 
Version: 2.16.2
Building workflow dependency index...
Start Active Workflows:
Activated workflow "workflow_name" (ID: workflow_id)

Editor is now accessible via:
http://home-server:3002
dbTime.getTime is not a function
dbTime.getTime is not a function
dbTime.getTime is not a function
dbTime.getTime is not a function
dbTime.getTime is not a function

What I tried so far

  • different n8n versions
  • reboot both client and server
  • disable firewall on both machines
  • use firefox and chrome
  • use inconito mode
  • disable browser’s extensions
  • use N8N_PUSH_BACKEND=sse

Information on your n8n setup

  • n8n version: 2.16.2
  • Database (default: SQLite): PostgreSQL 17.9
  • n8n EXECUTIONS_PROCESS setting (default: own, main): not set
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Ubuntu 24.04.4 LTS
  • Client and server are on the same LAN
  • Docker command:

docker run -d \
 --name n8n \
 --net n8n_net \
 --restart always \
 -p 3002:5678 \
 -e N8N_HOST=0.0.0.0 \
 -e N8N_EDITOR_BASE_URL=http://home-server:3002 \
 -e WEBHOOK_URL=http://home-server:3002 \
 -e DB_TYPE=postgresdb \
 -e DB_POSTGRESDB_DATABASE=n8n_db \
 -e DB_POSTGRESDB_HOST=postgres \
 -e DB_POSTGRESDB_PORT=5432 \
 -e DB_POSTGRESDB_USER=n8n_db_user \
 -e DB_POSTGRESDB_SCHEMA=public \
 -e DB_POSTGRESDB_PASSWORD=n8n_db_passwd not my real password ;) \
 -e N8N_SECURE_COOKIE=false \
 -e GENERIC_TIMEZONE="Europe/Paris" \
 -e N8N_DIAGNOSTICS_ENABLED=false \
 -e EXECUTIONS_DATA_PRUNE=true \
 -e EXECUTIONS_DATA_MAX_AGE=24 \
 -e N8N_ENABLE_EXECUTE_COMMAND=true \
 -e NODES_EXCLUDE="[]" \
 -v /path:/home/node/.n8n \
 n8nio/n8n:2.16.2

1 Like

Make sure n8n and task-runner version are the same

@randoum that dbTime.getTime is not a function spamming your logs is the actual problem, it’s a confirmed bug in n8n with PostgreSQL — the clock repository returns a raw string instead of a Date object and it crashes the internal poller every few seconds, which kills your websocket connections. exec into your container with docker exec -it n8n sh and edit /usr/local/lib/node_modules/n8n/node_modules/@n8n/db/dist/repositories/clock.repository.js, find the postgres branch in getDbTime() and wrap the return value with new Date(), then restart the container. tracked here dbTime.getTime is not a function repeated in idle logs on n8n 2.16.1 · Issue #28640 · n8n-io/n8n · GitHub

1 Like

Hey, thanks for the help!

So I followed instructions

The error does not appear in the logs anymore, but the rest of the symptoms remain the same sadly

Hey, thanks for your message.

I’m not sure how those 2 could be different. Are they not both running from the docker image? How can I check versions, and fix any related issue?

Type docker ps in your terminal. What does it show?

Only one container related to n8n

e02788151230   n8nio/n8n:2.16.2           "tini -- /docker-ent…"   2 hours ago    Up 19 minutes   0.0.0.0:3002->3002/tcp, [::]:3002->3002/tcp, 5678/tcp   n8n_websites

Ok, so you are not running task-runners. You may ignore my earlier posts

@randoum your docker ps output shows 0.0.0.0:3002->3002/tcp and container name n8n_websites, but your docker run command maps -p 3002:5678 and names it n8n — that’s a completely different container. kill n8n_websites and make sure you’re running the docker command you actually posted, the port mapping mismatch (3002->3002 vs 3002->5678) would explain why WS drops after handshake.

1 Like

Well spotted :slight_smile:

But they are actually synchronized. If you see a difference it’s because I’m keep doing tests.

I tried:

  • using 5678 directly (no mapping)
  • using 3002 directly (through N8N_HOST)
  • mapping 5678 to 3002

Same result in, every cases

The fact that N8N_PUSH_BACKEND=sse also didn’t help is a useful clue — if both WebSocket and SSE drop, it’s not protocol-specific but something in n8n’s push system itself. Worth verifying the dbTime fix actually survived: run docker exec n8n grep -c 'new Date' /usr/local/lib/node_modules/n8n/node_modules/@n8n/db/dist/repositories/clock.repository.js — if it returns 0, the edit got wiped (it’s lost on container restart). Also try N8N_LOG_LEVEL=debug and capture logs right at the moment the connection drops, that should surface whatever is actually triggering the disconnect.

Usually, ws stop working when you are reaching n8n container with a reverse proxy/load balancer. This extra infrastructure piece could contains time-out rules that are disconnecting you.
Can you please note if you are reaching your n8n container via a reverse proxy?

I’ve noticed something: the Connection Lost issue happens only when 2 or more browser’s tab are opened simultaneously. If only one is opened, there is no issue. That must be something, wdyt?


@Benjamin_Behrens

The fact that N8N_PUSH_BACKEND=sse also didn’t help is a useful clue — if both WebSocket and SSE drop, it’s not protocol-specific but something in n8n’s push system itself. Worth verifying the dbTime fix actually survived: run docker exec n8n grep -c 'new Date' /usr/local/lib/node_modules/n8n/node_modules/@n8n/db/dist/repositories/clock.repository.js — if it returns 0, the edit got wiped (it’s lost on container restart). Also try N8N_LOG_LEVEL=debug and capture logs right at the moment the connection drops, that should surface whatever is actually triggering the disconnect.

Hey. The changes did survived:

$ docker exec n8n_websites cat /usr/local/lib/node_modules/n8n/node_modules/@n8n/db/dist/repositories/clock.repository.js | grep -A 2 -B 2 "new Date"
        if (this.databaseConfig.type === 'postgresdb') {
            const [{ now }] = await this.dataSource.query('SELECT CURRENT_TIMESTAMP(3) AS now');
            return new Date(now);
        }
        const [{ now }] = await this.dataSource.query("SELECT STRFTIME('%Y-%m-%dT%H:%M:%fZ', 'NOW') AS now");
        const date = new Date(now);
        if (Number.isNaN(date.getTime())) {
            throw new n8n_workflow_1.UnexpectedError(`Invalid DB server time: ${now}`);

@Simosimi

Usually, ws stop working when you are reaching n8n container with a reverse proxy/load balancer. This extra infrastructure piece could contains time-out rules that are disconnecting you.
Can you please note if you are reaching your n8n container via a reverse proxy?

Hi. No reverse proxy or LB, the set-up is straight forward:

  • Server running the docker container
  • Client on the same LAN accessing n8n using the server’s IP

Yes, I saw this behaviour. Can you please try to downgrade to a version before 2.16.x and see if this still happens?

1 Like