In September our network team moved the CI runners into a new subnet whose only way out to the internet is a Squid proxy. They set HTTPS_PROXY, HTTP_PROXY and NO_PROXY in the runners' environment, ran a few curl commands through it, and told us nothing should notice.
Almost nothing did. curl, npm and pip all read those variables. Maven took its dependencies from our Nexus, which sits inside the network and needs no proxy. The build stage of our label service went green. The contract test stage, which calls each carrier's sandbox from Java, ran for forty one minutes and failed with nineteen connection timeouts.
The JVM does not read HTTPS_PROXY. Java's HTTP clients take their proxy from system properties, https.proxyHost and https.proxyPort, and without them they connect directly. The firewall drops direct traffic leaving that subnet instead of refusing it, so every attempt waited for the kernel to give up. Linux retries a SYN six times by default, doubling the wait from one second, which comes to about a hundred and twenty seven seconds per connection, and our tests set no connect timeout of their own.
Our first fix did nothing. We put the properties in MAVEN_OPTS and the tests still went direct. MAVEN_OPTS configures the JVM that runs Maven. Surefire runs the tests in a forked JVM, which takes its arguments from argLine and inherits nothing from Maven's.
What works is JAVA_TOOL_OPTIONS. Every JVM reads it from its environment at startup, forked or not, and says so on stderr with the line Picked up JAVA_TOOL_OPTIONS. The runner image now builds it from the same values as HTTPS_PROXY. The exclusions needed translating as well. NO_PROXY is a comma separated list in which .internal.example.com covers a whole domain. Java's http.nonProxyHosts, which it also uses for HTTPS, is separated by pipes and wants *.internal.example.com.
The contract tests now set a five second connect timeout, so a network that drops packets fails the stage in seconds, not in forty minutes. The first step of every Java job prints the proxy properties the JVM actually sees, so the next change to the network shows up in a log line before it shows up as a timeout.
An environment variable is a convention, and only the programs that agreed to it follow it. Ask each runtime where it gets its proxy from, not the shell.
– Sergey Shinder