Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assuming netcat exists can cause problems #153

Closed
ericfranz opened this issue Sep 30, 2019 · 3 comments
Closed

Assuming netcat exists can cause problems #153

ericfranz opened this issue Sep 30, 2019 · 3 comments
Labels
Milestone

Comments

@ericfranz
Copy link
Contributor

See:

# Check if port $1 is in use
port_used () {
local port="${1#*:}"
local host=$((expr "${1}" : '\\(.*\\):' || echo "localhost") | awk 'END{print $NF}')
nc -w 2 "${host}" "${port}" < /dev/null &> /dev/null
}
export -f port_used

The assumption here is that netcat is available. If it is not (because the command doesn't exist or the user is not allowed to execute the command) the result will be nonzero but will be reported as the port not yet being in use.

If we are going to use netcat, we should check for its existence and handle the error case that, then use fallback solutions.

Or not use netcat at all. See https://listsprd.osu.edu/pipermail/ood-users/2018-October/000269.html for discussion and some options and Trey's comment:

I’ve used this trick in the past to check if ports are available when bootstrapping MySQL instances in jobs that shared the same node:

if (: < /dev/tcp/127.0.0.1/${MYSQL_PORT}) 2>/dev/null; then
echo "MySQL port ${MYSQL_PORT} already taken."
return 1
fi

@ericfranz ericfranz added the bug label Sep 30, 2019
@ericfranz
Copy link
Contributor Author

ericfranz commented Sep 30, 2019

One solution if lsof is available on the compute nodes to add to the before.sh.erb before find_port is executed:

# override
port_used () {
  local port="${1#*:}"
  local host=$((expr "${1}" : '\\(.*\\):' || echo "localhost") | awk 'END{print $NF}')
  lsof -i :"${port}" &> /dev/null
}
export -f port_used

and the diff of the file looks something like this:

 # Export the module function if it exists
 [[ $(type -t module) == "function" ]] && export -f module

+# override
+port_used () {
+  local port="${1#*:}"
+  local host=$((expr "${1}" : '\\(.*\\):' || echo "localhost") | awk 'END{print $NF}')
+  lsof -i :"${port}" &> /dev/null
+}
+export -f port_used
+
 # Find available port to run server on
 port=$(find_port ${host})

@ericfranz
Copy link
Contributor Author

If there is a reliable way to check if the pseudo-device /dev/tcp is available, we would check if that exists first, then use it if it does; if not then try something else.

@ericfranz
Copy link
Contributor Author

Addressed by #159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant