ostinelli / net-http2

NetHttp2 is an HTTP/2 client for Ruby.
MIT License
140 stars 31 forks source link

net-http2 kills ruby interpreter process when it failed by socket failure #23

Closed sayakahojo closed 6 years ago

sayakahojo commented 6 years ago

When socket error occured, net-http2 kills ruby interpreter process. It causes temporary error when it is used for application.

Here is the code. https://github.com/ostinelli/net-http2/blob/1513fb7cb661bfcc6648ef31fbc3b66095dacc51/lib/net-http2/client.rb#L101

    def ensure_open
      @mutex.synchronize do 
        return if @socket_thread
        @socket = new_socket
        @socket_thread = Thread.new do 
          begin 
            socket_loop
          rescue EOFError
            init_vars
            raise SocketError.new('Socket was remotely closed')
          rescue Exception => e
            init_vars
            raise e
          end
        end.tap { |t| t.abort_on_exception = true }
      end
    end

abort_on_exception method is called after socket error and it must be destructive.

↓ I want to know what the purpose of this code is. end.tap { |t| t.abort_on_exception = true }

https://ruby-doc.org/core-1.9.3/Thread.html

ostinelli commented 6 years ago

If you follow your own link you will know what the purpose of that code is: if the loop thread raises an error, to not silently ignore it and raise it in the main thread.

BTW, you are referencing old code (2 versions ago). In recent versions, the code allows to set an error callback on client, so that socket errors do not interrupt the main thread.

From the README:

  • on(event, &block)

Allows to set a callback for the client. The only available event is :error, which allows to set a callback when an error is raised at socket level, hence in the underlying socket thread.

client.on(:error) { |exception| puts "Exception has been raised: #{exception}" }

It is RECOMMENDED to set the :error callback: if none is defined, the underlying socket thread may raise an error in the main thread at unexpected execution times.