lapis
lapis copied to clipboard
Crashed while using PostgreSQL in ngx.thread.
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
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.
I moved psql's task to one thread and synchronized it with ngx.semaphore. It works fine