mardi 12 mai 2020

Thread.list won't show thread with websocket in it randomly

I can't figure out one thing. I'll explain step by step.

Using ruby 2.1.5p273, rails 3.2.13 and nginx (latest) on production server.

With one request i am starting websocket in new thread as follows:

main_thread = Thread.new {

   main_thread.thread_variable_set(:foo, 'foo')
   main_thread.thread_variable_set(:bar, 'bar')

   EM.stop if EM.reactor_running?
   EM.run {
      ws = Faye::WebSocket::Client.new(...)
      ws.on :open {...}
      ws.on :message {...}
      ws.on :close {...}
   }

   main_thread.thread_variable_set(:ws, ws)
}

Then with another request, if i wish, i want to close websocket on demand as follows:

Thread.list do |thread|
   if thread.thread_variable_get(:foo) == 'foo' && thread.thread_variable_get(:bar) == 'bar'
      thread.thread_variable_get(:ws).close

      sleep 1

      thread.exit
   end
end

What buggers me is a fact, sometimes i can't list main_thread in Thread.list even tho i see it is active in logger. Most of the time, it works ( i can list and exit it ). What i do when i can't? I F5 the page and try it with another request ( click the button again). Sometimes it doesn't work on the first F5 refresh, but eventually it works. I was unable to search anything about invisible thread or so. Is there any filter Thread.list won't show all of running threads in application?

What is the catch ?

Any insight is most welcome.

Aucun commentaire:

Enregistrer un commentaire