Github Action builds with spark failing with java.net.BindException with spark commands

We started seeing this failure sporadically with our spark builds ubuntu 18.04, but it shows up all the time now. Fixed the spark 3 tests by explicitly setting SPARK_LOCAL_IP=127.0.0.1 in spark_env. But, the spark 2 builds are still broken even with the env explicitly set and calling spark-submit with --conf "spark.driver.bindAddress=127.0.0.1. Running lsof -iTCP -sTCP:LISTENe before spark-submit show all the 4K ports to be free. See this broken build for instance.

Jenkins builds seem to be fine.

2021-04-19T17:42:28.3311657Z 21/04/19 17:42:27 WARN Utils: Service 'SparkUI' could not bind on port 4040. Attempting port 4041.
...
2021-04-19T17:42:28.3342144Z 21/04/19 17:42:27 ERROR SparkUI: Failed to bind SparkUI
2021-04-19T17:42:28.3344354Z java.net.BindException: Cannot assign requested address: Service 'SparkUI' failed after 16 retries (starting from 4040)! Consider explicitly setting the appropriate port for the service 'SparkUI' (for example spark.ui.port for SparkUI) to an available port or increasing spark.port.maxRetries.

Any pointers to working around this issue for spark 2?

Also see

The solution was to cleanup /etc/hosts that had numerous aliases for 127.0.0.1 and to assign both spark master and local ip to 127.0.0.1 in spark_env.sh, setting the shell env was not sufficient.

Anyone know why these exist in /etc/hosts for ubuntu 18.04 images?

127.0.0.1 stratum.antpool.com
127.0.0.1 gratified.approvalbureau.com
127.0.0.1 inpervious.choppedgreen.com
127.0.0.1 nonretractile.choppedgreen.com
127.0.0.1 asia.cryptonight-hub.miningpoolhub.com
...
127.0.0.1 hub.miningpoolhub.com

Most likely attempt from github to break mining attacks by messing up hosts table.