lapis icon indicating copy to clipboard operation
lapis copied to clipboard

Crashed while using PostgreSQL in ngx.thread.

Open uudiin opened this issue 6 years ago • 2 comments

I use multi-thread(ngx.thread.spawn) to get data and store thems to PostgreSQL. But lapis cached the psql tcpsock to ngx.ctx.pgmoon. So the whole threads use the one tcpsock to backend psql. It is illegal for lua-nginx-module that read or write the same tcpsock in multi threads, It get error 'socket busy reading'. So, How to get it ? Thanks a lot

uudiin avatar Dec 28 '17 11:12 uudiin

I haven't experimented with ngx.thread.spawn. It would appear that both threads are trying to use the socket at the same time, causing invalid messages to be sent to the database server. We would need a mechanism to detect the thread and pull a fresh connection from the connection pool for it.

If you're just trying to do something asynchronous, and don't need the full support of threads, you can try using ngx.timer.at, since that will get a new context and a fresh db connection.

leafo avatar Dec 28 '17 18:12 leafo

I moved psql's task to one thread and synchronized it with ngx.semaphore. It works fine

uudiin avatar Jan 03 '18 03:01 uudiin