#1 2026-04-13 17:36:26

Kabiri
Member
Registered: 2024-06-22
Posts: 70

mORMot + PostgreSQL: "lost synchronization with server" when switching

I'm using mORMot with PostgreSQL, and I suddenly started getting this error:

Project Vi.exe raised exception class ESqlDBPostgres with message:
TSqlDBPostgresLib Exec failed:
[lost synchronization with server: got message type "lost synchronization with server: got message type"]

After this error occurs, the application can no longer connect to the database unless restarted.

Here is how I initialize the connection:

Model := CreateViModel;
DBProps := TSqlDBPostgresConnectionProperties.Create(
  Host, DBName, DBUser, DBPass
);

DBProps.UseCache := False;
DBProps.ReconnectAfterConnectionError := True;

VirtualTableExternalRegister(Model, TOrmTenant, DBProps);
...

Additional details:

The issue seems to happen when rapidly switching between menus in the client (HTMX-based UI).
It looks like multiple concurrent requests may be involved.
After the error, even reconnection does not recover properly.

Questions:

Could this be related to connection reuse or thread safety in mORMot?
Should I be using a connection pool or per-thread connection instead?
Is there a known issue with PostgreSQL driver synchronization in such scenarios?

Any guidance or similar experiences would be appreciated.

Last edited by Kabiri (2026-04-14 07:24:36)

Offline

#2 2026-04-13 22:27:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,440
Website

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

If I understand correctly https://www.postgresql.org/message-id/1 … nabble.com
it sounds like the server closed the connection due to too big/slow request.
Look at the request itself, and the full error message.

It may be more tied to the request and server itself that with mORMot.

There is a per-thread connection pool in mORMot.
So nothing is to be configured here.
Just don't create too many threads in your code.

Offline

#3 2026-04-14 08:49:12

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

I was using PostgreSQL 17.6-2. I’ll upgrade to PostgreSQL 18.3-2 and test it.
Maybe the issue was related to this: https://www.postgresql.org/message-id/2 … .pgh.pa.us

Offline

#4 2026-04-14 12:42:57

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

@ab
I updated it and everything was working fine.
After some time—without rapidly switching between menus and without any errors—everything stopped working.

The application reaches this line but never returns from it:

u := TOrmUser.Create;
try
  if not Self.Server.Orm.Retrieve('Email=?',[],[Email],u) then

Offline

#5 2026-04-15 18:11:45

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Isn’t there any solution?
It’s really frustrating. Right now, even during testing with no users, it keeps getting stuck and I have to restart the service. What should I do when there are many users?

Offline

#6 2026-04-16 08:33:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,440
Website

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Could you try with the Zeos client and see if it is a mormot direct access unit problem?

I am AFK those days, so I can't test anything.

Offline

#7 2026-04-16 10:12:58

danielkuettner
Member
From: Germany
Registered: 2014-08-06
Posts: 406

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

With Zeos we never had such an issue. So it seems to be mormot direct access unit problem.

Offline

#8 2026-04-16 10:44:24

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

ab wrote:

Could you try with the Zeos client and see if it is a mormot direct access unit problem?

I am AFK those days, so I can't test anything.

Ok, thanks. I’ll check it and let you know.

Offline

#9 2026-04-16 12:28:26

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

The `Zeos.inc` file used in `mormot.db.sql.zeos` cannot be found.

Offline

#10 2026-04-16 13:12:05

Prometeus
Member
Registered: 2020-11-20
Posts: 62

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Kabiri wrote:

The `Zeos.inc` file used in `mormot.db.sql.zeos` cannot be found.

  Have you installed 'Zeos' before using it as a DB connection? 'Zeos.inc' is in the 'src' folder of the 'Zeos' installation.

Offline

#11 2026-04-16 13:26:18

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Prometeus wrote:

  Have you installed 'Zeos' before using it as a DB connection?

No, I haven’t installed it. I thought it didn’t require installation. I’ll install it now.

Offline

#12 Yesterday 17:24:16

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

After several hours of working with the application, and repeatedly opening and closing menus, the following error suddenly appeared:

Project Vi.exe raised exception class EZSQLException with message 
'SQL Error: insufficient data in "T" message

Code: 7 
SQL: select ID,Name,Slug,Plan,OwnerID,IsActive,MaxMembers,CreatedAt 
from public.Tenant where ID=?'.

After that, the application no longer responds to any requests.

This is how I established the connection:

DBProps := TSqlDBZeosConnectionProperties.Create('zdbc:postgresql://' + Host, DBName, DBUser, DBPass);

Offline

#13 Yesterday 18:08:15

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

It happened again. This time the error was:

Project Vi.exe raised exception class EZSQLException with message 
'SQL Error: message contents do not agree with length in message type "T"
server sent data ("D" message) without prior row description ("T" message)

Code: 7 
SQL: select ID,TenantID,Email,FullName,PasswordHash,Role,AvatarUrl,CreatedAt 
from public.User where ID=?'.

After that, the application raised the following exception:

Project Vi.exe raised exception class $C0000005 with message 
'c0000005 ACCESS_VIOLATION'.

The client is using HTMX.

Offline

#14 Yesterday 19:02:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,440
Website

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Are you sure the libpq library is in the same version than the server?

Offline

#15 Today 07:41:57

danielkuettner
Member
From: Germany
Registered: 2014-08-06
Posts: 406

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Wether Zeos nor mORMot2 error.
Something in your own project went wrong.

I would guess libpq is out of sync with server because of using it in a multi-threaded application in a absolutely wrong way.

Offline

#16 Today 09:35:58

flydev
Member
From: France
Registered: 2020-11-27
Posts: 158
Website

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

...in message type "T" server sent data "D" message without prior row description ("T" message)

the message itself really give hint about what @daniel is suggesting: wrong multi-thread usage.

you really need to give more information and eliminate causes. First answer @ab and tell us:

- version of libpq in use

then,
- check if is zeos-specific by running the same sql:
  1. psql,
  2. pgAdmin,
  3. a tiny test executable using only one connection and one thread

- eliminate the dll ambiguity:
  1. copy the chosen libpq.dll to the EXE folder (keep only the dependent DLLs required by that build)
  2. temporarily rename any other libpq.dll reachable through PATH


But again, it seem to be a thread issue:

inference from the protocol behavior and Tom Lane’s explanation wrote:

Same connection used concurrently from multiple threads
If two threads share one PostgreSQL connection or one query object and interleave reads/writes on the same socket, the message stream becomes corrupted from the client’s perspective. The symptom often looks exactly like “unexpected message type while idle” or “DataRow without prior RowDescription,” because packets are being attributed to the wrong command.

https://www.postgresql.org/docs/current … -flow.html

Offline

#17 Today 10:35:36

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

ab wrote:

Are you sure the libpq library is in the same version than the server?

Yes.
After installing the new version, I copied it from
C:\Program Files\PostgreSQL\18\pgAdmin 4\runtime
and placed it next to my application.

Offline

#18 Today 10:53:11

Kabiri
Member
Registered: 2024-06-22
Posts: 70

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

@flydev @danielkuettner

I removed the DLL files from next to the application and the error appeared, so it seems it is using those DLLs. Maybe it’s related.

I registered the services like this:

Server.ServiceDefine(TViAuthService, [IViAuthService], sicShared);

Would `sicPerThread` help?

In my application I don’t have multiple threads, but the client can send asynchronous requests. I assumed that on the server side it would be handled internally.

Last edited by Kabiri (Today 11:13:52)

Offline

#19 Today 13:17:40

danielkuettner
Member
From: Germany
Registered: 2014-08-06
Posts: 406

Re: mORMot + PostgreSQL: "lost synchronization with server" when switching

Now you are on the right way.
Client requests are handled in threads on service side. Every thread needs its own (separate) connection to postgres to work properly.
Or you use only one connection to db and synchronize threads with critical sections.
Or you handle all db access in one thread (e.g. MainThread). There are options on mORMot for that.
You have options and all depends on what you want.

Offline

Board footer

Powered by FluxBB