Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Last active November 7, 2023 18:52
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 gwpantazes/e4f16f0e5edaab5dbc02ad26b7e573e4 to your computer and use it in GitHub Desktop.
Save gwpantazes/e4f16f0e5edaab5dbc02ad26b7e573e4 to your computer and use it in GitHub Desktop.
Testing for "cannot allocate memory" error in Ruby TCPSocket.open against `elasticloadbalancing.us-west-2.amazonaws.com:443`

Test Ruby TCPSocket.open against elasticloadbalancing.us-west-2.amazonaws.com:443

We are poking around and looking for the following "cannot allocate memory" error message:

Failed to open TCP connection to elasticloadbalancing.us-west-2.amazonaws.com:443 (Cannot allocate memory - connect(2) for \"elasticloadbalancing.us-west-2.amazonaws.com\" port 443)

Target testing Ruby 3.2's TCPSocket.open call here: https://github.com/ruby/net-http/blob/v0.3.2/lib/net/http.rb#L1271-L1273. (Ruby TCPSocket implementation: https://github.com/ruby/ruby/blob/master/ext/socket/tcpsocket.c)

Also test specifically against elasticloadbalancing.us-west-2.amazonaws.com:443 as the target connection in case that's causing the issue.

Experiment - localhost:2000

  1. Run ruby tcp-server.rb. Leave it running.
  2. Run clients ruby tcp-client-loop.rb. Leave it running. Run as many simultaneous clients as you want to stress the server-client connections over the port. For example, run 5 clients simultaneously.
  3. Any errors/exceptions will crash the client script, allowing you to simply run and wait until you see something interesting.

Results: No error when running 5x TCP clients at full blast on loop. Ran for about 10-20 minutes. Observed until an accumulated 500k connections without any error (about 10 minutes). Noticed that there would be some pauses and then bursts of progress, probably due to timeouts and deadlocks for resources, but there were no errors.

Experiment - elasticloadbalancing.us-west-2.amazonaws.com:443

TODO - Probably not going to firehose AWS like I did to my localhost, so I'll investigate the output from the TCPSocket in-depth in case the output is too large and causing the "cannot allocate memory" error.

require 'socket' # Sockets are in standard library
hostname = 'localhost'
port = 2000
count = 0
loop {
s = TCPSocket.open(hostname, port)
print count, ' '
while line = s.gets # Read lines from the socket
puts line.chop # And print with platform line terminator
end
s.close # Close the socket when done
count += 1
}
require 'socket' # Sockets are in standard library
hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)
while line = s.gets # Read lines from the socket
puts line.chop # And print with platform line terminator
end
s.close # Close the socket when done
require 'socket' # Get sockets from stdlib
server = TCPServer.open(2000) # Socket to listen on port 2000
loop { # Servers run forever
client = server.accept # Wait for a client to connect
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close # Disconnect from the client
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment