websockets cleanup

188 views
Skip to first unread message

Scott Cameron

unread,
Jul 18, 2016, 12:47:18 PM7/18/16
to openresty-en
Hello,

I have a fairly busy websocket server.  I could receive multiple requests on the websocket, before the clients initiates a close(), but there's no guarantee that the client does a close().

My error log is full of lines like this:

2016/07/18 16:43:28 [error] 10044#10044: *24127 lua tcp socket read timed out, client: 1.2.3.4, server: websocket, request: "GET / HTTP/1.1", host: "websocket"

and when I try to reload nginx, the old workers appear to be stuck in shut-down state pending the stale/old websocket connections.

proctitle:  nginx: worker process is shutting down

when I strace the process, it's printing out lines to the error log about the socket read timed out.

In my lua code, I initialize the websocket as:

local ws, err = ws:new { timeout = 3000, max_payload_len = 1310700 }
while true do
  local data, t, err = ws:recv_frame()
  ...
end

ws:send_close()

Is there a way to force cleanup of old websocket connections?

Bogdan Irimia

unread,
Jul 22, 2016, 4:33:34 AM7/22/16
to openre...@googlegroups.com
I would be interested in this one too, because I encountered the same phenomenon.

Thank you

Scott Cameron wrote:
--
You received this message because you are subscribed to the Google
Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to openresty-en...@googlegroups.com
<mailto:openresty-en...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

Yichun Zhang (agentzh)

unread,
Jul 29, 2016, 4:00:02 PM7/29/16
to openresty-en
Hello!

On Mon, Jul 18, 2016 at 9:47 AM, Scott Cameron wrote:
> I have a fairly busy websocket server. I could receive multiple requests on
> the websocket, before the clients initiates a close(), but there's no
> guarantee that the client does a close().
>
> My error log is full of lines like this:
>
> 2016/07/18 16:43:28 [error] 10044#10044: *24127 lua tcp socket read timed
> out, client: 1.2.3.4, server: websocket, request: "GET / HTTP/1.1", host:
> "websocket"

It's recommended to put the following line in your nginx.conf:

lua_socket_log_errors off;

See https://github.com/openresty/lua-nginx-module/#lua_socket_log_errors
fore more details.

>
> and when I try to reload nginx, the old workers appear to be stuck in
> shut-down state pending the stale/old websocket connections.
>
> proctitle: nginx: worker process is shutting down
>

Well, you can 1. always check the return value of ngx.worker.exiting()
in your main websocket processing loop, so that you can close the
websocket connection upon nginx worker shutdown. See

https://github.com/openresty/lua-nginx-module/#ngxworkerexiting

Hmm, I think we should add it to the docs of lua-resty-websocket
though. Pull requests welcome :)

Regards,
-agentzh

Scott Cameron

unread,
Aug 2, 2016, 8:03:00 PM8/2/16
to openresty-en

On Friday, July 29, 2016 at 4:00:02 PM UTC-4, agentzh wrote:
Hello!


It's recommended to put the following line in your nginx.conf:

     lua_socket_log_errors off;

See https://github.com/openresty/lua-nginx-module/#lua_socket_log_errors
fore more details.


You are of course right.  I'm glad it wasn't off though, as it did present the symptom of the problem very clearly.
 
>
> and when I try to reload nginx, the old workers appear to be stuck in
> shut-down state pending the stale/old websocket connections.
>
> proctitle:  nginx: worker process is shutting down
>

Well, you can 1. always check the return value of ngx.worker.exiting()
in your main websocket processing loop, so that you can close the
websocket connection upon nginx worker shutdown. See

    https://github.com/openresty/lua-nginx-module/#ngxworkerexiting

Hmm, I think we should add it to the docs of lua-resty-websocket
though. Pull requests welcome :)


This works like a charm.  I'll try to make a pull request at least on the README for you this week. 
Reply all
Reply to author
Forward
0 new messages