-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't read/write to the same cosocket from different threads #241
Comments
Actually there is a deeper limitation here in the current implementation, that is, a cosocket object cannot be shared between different requests at all. When a cosocket object is accessed from within another request that did not create, the behavior is actually undefined. Also, please be aware that cosockets also cannot be shared among different Nginx worker processes and you cannot really control which worker process to serve a particular request unless you have only one Nginx worker process. |
Hi! Thanks for the quick reply. User sends a request to nginx - and keeps the connection alive - all the data is above the same request (we'll call the user with nginx socket A) Now we hold 2 sockets - A & B, This is a very simple proxy scenario :) This can be achieved by using nginx sockets & C content handler - but not by using lua-nginx, since the read and write must be performed in the same thread for the same socket in the current implementation. I suggest splitting the co_ctx to read_co_ctx and write_co_ctx, and also split the waiting to read_waiting and write_waiting. this will allow much more flexibility with the cosocket API. Cheers. |
Technically you aren't waiting to read and write, however it does appear that you are bcs you cannot just "schedule IO." Have you tried using "threads" http://wiki.nginx.org/HttpLuaModule#ngx.thread.spawn . or just reading in "chunks" - read 8k (for example) from A and write to B, etc. |
Yes, I am using ngx.thread.spawn - but the threads receive a "socket busy" error - because you can't read in one thread from a socket and write in a different thread. The suggestion of reading in chunks is fine (a patch here is also needed to prevent lua-nginx from closing sockets on time-out, but irrelevant to the subject) - and it works, but it then simple generates a busy-loop instead of using nginx's event based model - which is a very large performance hit... |
@AdallomRoy okay, I see your point now. You're just proxying the downstream cosockets and upstream cosockets. This is technically possible by separating the read and write states in a single cosocket object. Patches welcome! :) |
@AdallomRoy BTW, there is a pending patch on the openresty-en mailing list that makes read timeout errors nonfatal as you've mentioned: https://groups.google.com/group/openresty-en/browse_thread/thread/f65a5fcd77f4e5cc I'm going to review it in the near future. |
Hi! @agentzh , Thanks. Cheers. |
I think I have another use case described here: |
Sorry about closing the issue, this was by mistake :) What I wanted to say is that I already created a patch that does this functionality. It's already tested and runs in production. |
@AdallomRoy Hi, I just found out that @agentzh included lua-resty-websocket in the latest OpenResty bundle. Do you have any news on this front (just being curious, :-))? |
@aviramc added our patch for this issue, as well as other several open issues. |
Now that we already have full-duplex cosocket support in ngx_lua 0.9.9+, I'm closing this. |
It's impossible with the current implementation to read from thread 1 and write from thread 2 to the same socket.
This causes issues in tunnel cases - if I wanted to create a tunnel between 2 connections I'd run 2 threads, 1 reading from socket A and writing to socket B, the other reading from socket B and writing to socket A - which is currently impossible ("socket busy") - even though this supported by nginx itself.
This is caused by the fact that there isn't a separation of read & write state for each socket -
ngx_http_lua_co_ctx_t *co_ctx;
and
unsigned waiting:1;
are for both read & write operations - They should probably be separated.
The text was updated successfully, but these errors were encountered: