On 2016-02-16 17:31, Jacob Carlborg wrote:

I'm trying to use the Postgres client ddb [1] in a vibe.d application. I'm using it according to the documentation [2] with a connection pool from vibe.d. I store in the PostgresDB instance as a TLS variable.

As soon as I do concurrent requests to the application I get the error below. Any ideas?

Task terminated with unhandled exception: Acquiring reader of already owned connection.
core.exception.AssertError@../../../d/vibe.d/source/vibe/core/drivers/libevent2_tcp.d(412): Acquiring reader of already owned connection.
----------------
4 admin 0x000000010bff7032 _d_assert_msg + 142
5 admin 0x000000010bf721ef void vibe.core.drivers.libevent2_tcp.Libevent2TCPConnection.checkReader() + 115
6 admin 0x000000010bf72209 void vibe.core.drivers.libevent2_tcp.Libevent2TCPConnection.acquireReader() + 21
7 admin 0x000000010bf71774 void vibe.core.drivers.libevent2_tcp.Libevent2TCPConnection.read(ubyte[]) + 216
8 admin 0x000000010be55279 ddb.postgres.Message ddb.postgres.PGConnection.getMessage() + 181
9 admin 0x000000010be5659b void ddb.postgres.PGConnection.prepare(immutable(char)[], immutable(char)[], ddb.postgres.PGParameters) + 127
10 admin 0x000000010be58d16 void ddb.postgres.PGCommand.checkPrepared(bool) + 70

[1] https://github.com/pszturmaj/ddb
[2] https://github.com/pszturmaj/ddb/blob/master/source/ddb/postgres.d#L71-L92

I found a fix for this problem. I'm still not sure what the original
problem was though. I did something like this:

auto pdb = new PostgresDB( ... );

auto executeQuery(string query)
{

 scope cmd = new PGCommand(pdb.lockConnection(), query);
 return cmd.executeQuery();

}

The fix was to store the connection in a local variable:

auto executeQuery(string query)
{

 auto conn = pdb.lockConnection();
 scope cmd = new PGCommand(conn, query);
 return cmd.executeQuery();

}

Could it be an issue related to how the reference counting in the
connection pool works?

/Jacob Carlborg