Net::HTTP and the simplest of explanations

This blog post exists purely to remind myself that Ruby’s Net::HTTP expects a host and a port as parameters when creating a new connection and not a url string.

This is a story about how many layers of abstractions and indirections one works through on a daily basis as a developer and the effort required to dive through these layers, all the while ignoring the simplest of explanations of why things may have gone wrong in the first place.

At work, we have a homegrown orchestration tool that brings up Docker containers and configures dependencies and network access between them - essentially an abstraction over docker-compose. In our continuous integration environment, this tool sets up all the different components of our system and then starts another container in which RSpec based integration tests that exercise various inetractions between the components of the system are run.

We added a new component and wanted a library used by the specs to use an API provided by this component. This essentially meant this for us:

Change our configurations so that the Docker container with the new component is started before the test container is started.

Ensure that the test container can talk to the new component container.

Tell the test container, through environment variables the host and the port of the new component.

So, off we went and configured everything:

env.NEWAPP_HOST=http://new-app.local
env.NEWAPP_PORT=1313

We ran the tests and boom:

SocketError (Failed to open TCP connection to http://new-app.local:1313 (getaddrinfo: nodename nor servname provided, or not known))

Well, that does not look right. We double check all the configurations and run the test again. Same result. May be we should be using a Ruby Symbol instead of a String in that particular config? May be. We try that, same result.

At this point, we hop on to the test container and ping new-app.local. It can connect.
What if the app is not available? We should totally telnet it. Well, this container does not have telnet. We can totally install it, right? Right. What kind of distro is this running? Well cat /etc/*release*. Debian, huh? apt-get install telnet. Wooh. Back to telnet then. That looks good.

At this point, the attention turns to the RSpec tests. What if the tests have some environment variables? Let’s debug it and look for things in Ruby’s ENV. Hmmm, nothing interesting there. Can the Ruby process even connect to new-app? We have Faraday, let’s try that:

At this point, we are covered in a mix of disappointment and excitement. We are annoyed that things are not working. But, may be, may be we have uncovered some obscure bug somewhere in the toolset? We would learn later that this is the point where we should have known what’s up? But we didn’t and the story continues.

At this point, we ping the Slack channels of the teams involved in building the library we consume. They have not seen this before. Someone suggests that they have had issues with Ruby inside Docker containers. We finally find a GitHub issue for a different project where someone encountered their container setting the HTTP_PROXY env variable and that causing Net::HTTP to fail. We pore over everything to make sure that there is no proxy set. What now?

What if we attempt to connect to the new app from a Ruby process running on one of the other 10 containers we run?

At this point, light bulbs are beginning to go off. Let’s try this on our laptops? Same result. And then it struck us. It says it can’t open a TCP connection to a URL with http in it. Of course, it cant. It should be looking for google.com, should not it? Yes:

And with a lot of excitement and some shame, we realize that the library really meant host when it asked for the NEWAPP_HOST environment variable.

I don’t think there are any big lessons in this story other than that, sometimes the simplest explanation of a problems makes a lot more sense than you would think it does. Also, if you ever use Net::HTTP.new, remember that it expects you to provide a host as the first param, not a URL.

If you have questions or comments about this blog post, you can get in touch with me on Twitter @sdqali.