Skip to content

Instantly share code, notes, and snippets.

@pjambet
Created September 29, 2020 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjambet/718599bc26c2d743afb1d02655344a08 to your computer and use it in GitHub Desktop.
Save pjambet/718599bc26c2d743afb1d02655344a08 to your computer and use it in GitHub Desktop.
ECONNRESET investigations
require 'socket'
# EPIPE, when the socket is closed by the time we try to read, we can read, but writing fails
# ECONNRESET, when the socket is closed by the time we try to write
def process_events(event)
p 'in proccess'
res = event.read_nonblock(1024, exception: false) # => YET THE FUCKING STACKTRACE POINTS HERE WHEN ECONNRESET
p res
event.puts "FUCK YOU puts" # => EPIPE
# p '==--=='
# event.write "FUCK YOU" # => ECONNRESET
return nil if res.nil?
# p res
1
end
server = TCPServer.new 2000
client = server.accept
loop do
res = IO.select([ client ], [], [], 10)
res.nil?
break if process_events(client).nil?
end
# raise 'done'
server.close
require 'socket'
require 'timeout'
def start
server = TCPServer.new 2000
# @clients = []
p "Server started at: #{ Time.now }"
client = server.accept
loop do
timeout = 1
p "select with a timeout of #{ timeout }"
result = IO.select([client], [], [], timeout)
p result
sockets = result ? result[0] : next
break if process_poll_events(sockets[0]).nil?
end
end
def process_poll_events(socket)
client_command_with_args = socket.read_nonblock(1024, exception: false)
response = "YO!"
p "Response: #{ response }"
socket.puts response
1
# rescue Errno::EPIPE
# p 'epipe'
# clients.delete(socket)
# nil
# rescue Errno::ECONNRESET
# p 'econnreset'
# clients.delete(socket)
# nil
end
start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment