multithreading - Multi-Threading in Ruby -
i have tcpserver made in ruby, server seems work, can see 2 or more clients can connect , served server, but, sometime stuck (as in need wait other client disconnect or unresponsive), after "pass_ok" bit, when connecting 1 client don't see issue.
here code:
  def self.main_server     begin       server = tcpserver.open(@port)     rescue exception => e       corelogging.syslog_error("cant start server: #{e}")     end     @main_pid = process.pid     # main loop     thread.abort_on_exception = true     while true       thread.fork(server.accept) |client|         @client = client         sock_domain, remote_port, remote_hostname, remote_ip = @client.peeraddr # info on incoming connection         corelogging.syslog_error("got new connection #{@client.peeraddr[3]} handeled thread: #{thread.current}") # log incoming connection          @client.puts "please enter password: " # password testing (later config file or db)         action = @client.gets(4096).chomp # client password response 'chomp' super important          if action == @password            # when password right           pass_ok           thread.exit         else            # when password wrong           pass_fail           thread.exit         end       end        begin         corelogging.syslog_error("thread ended (soft)")       rescue exception => e         corelogging.syslog_error("thread killed (hard)")       end     end   end 
i'll leave here future reference , hope in close situation find useful.
the issue global @client variable, got overwritten every new thread , inherited subclasses inside thread.
using local client variable (without '@') got work supposed.
Comments
Post a Comment