Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

read kernel socket table for connections - #1002

Merged
johrstrom merged 1 commit into
masterfrom
fix-926-used-port
Oct 13, 2026
Merged

johrstrom merged 1 commit into
masterfrom
fix-926-used-port

Conversation

@Oglopf

@Oglopf Oglopf commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Please review our Contributing Guide before submitting a pull request.

What does this PR do?

port_used returned on the first strategy that gave an answer. nc runs first and is installed nearly everywhere, and it checks a port by connecting to it rather than asking whether it's bound. That misses ports held by outgoing connections, and it can hang on a listener that never closes the connection. So find_port could hand out a port that was already in use.

This replaces the chain with checks that read the kernel's socket table:

  • port_used_proc reads /proc/net/tcp and /proc/net/tcp6 directly. It needs nothing installed, sees every user's sockets, and handles hosts without tcp6.
  • port_used_ss reads the same data through iproute2. It tests the output rather than the exit status, since ss exits 0 even when nothing matches.
  • port_used_lsof is kept as a last resort, but it only sees the calling user's sockets.

The connect-based strategies (nc, bash /dev/tcp, python) are removed, and the "no strategy available" message in wait_until_port_used is updated to match.

Please see #926 for results as to why the above are proposed to be removed.

Related issue

Closes #926

Testing

  • Tests included

New minitest in test/batch_connect/template_test.rb. It renders the real helpers and runs them in bash against real sockets: a listening port, a port held by an outgoing connection, a free port, tcp6, ss, and find_port in both directions.

  • Each helper call is capped at 10s, so a hanging check fails instead of stalling CI.
  • The "free" port is chosen from below the kernel's ephemeral range. An ephemeral port can be taken by another process's outgoing connection mid-test, which made an early version flaky on a busy login node.
  • 20 consecutive clean runs of the minitest suite on an Ascend login node, with no skips.
  • Against master's template.rb, the outgoing-connection tests fail, and the listening-port test times out on nc.

Checklist

  • Follows project code style and conventions
  • Documentation provided (if new feature, adapter or behavior change)
  • This is a large feature and was discussed in an issue first (if applicable)

Anything else?

Which fallbacks to keep is still open. This removes the connect-based strategies, which was option 1 in the #926 thread. If we'd rather keep them behind the new checks (option 2), it's a small change to restore the three functions and add them back to the end of port_strategies. Nothing else changes.

ss and lsof will rarely run. /proc/net/tcp is readable on any Linux host, so they only matter where /proc/net is restricted.

Not in this PR: wait_until_port_used still uses port_used, which counts a socket on the port in any state. For that caller, "listening" (state 0A) would be the more exact test. I haven't seen it cause a problem, so I've left it out of this change.

Docs follow-up: the requirements page lists nmap-ncat as a compute node requirement for interactive apps. After this change, OnDemand's own helpers no longer call nc, so that entry can be dropped or reworded. I'll open a PR in ood-documentation once the fallback question above is settled, since option 2 would keep nc in use. The web node's lsof requirement is unrelated.

@Oglopf Oglopf changed the title read kernel socket table for connections and test read kernel socket table for connections Sep 29, 2026
@johrstrom johrstrom self-assigned this Oct 13, 2026

@johrstrom johrstrom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, thanks!

@johrstrom
johrstrom merged commit 78c40b8 into master Oct 13, 2026
6 checks passed
@johrstrom
johrstrom deleted the fix-926-used-port branch October 13, 2026 18:32
Oglopf added a commit that referenced this pull request Oct 20, 2026
The hang Jeff saw was the old port_used trying nc first. On RHEL and
Fedora nc is ncat, where -w only limits the connect, so checking the
port the skip test held open (accepting nothing) waited forever. shuf
picked that port first about half the time. Rebasing onto #1002, which
reads /proc/net/tcp instead, removes it.

The tests now mostly check the generated script, as Jeff suggested.
Two still run the startup, to show the picked port reaches vncserver
and is the one the job reports. Those run under a 30s timeout, with
BASH_ENV unset (Lmod makes every bash source its init), and the fake
vncserver waits for its Xvnc like the real one does, so pgrep can't
miss it. Skipping a port in use is find_port's job and is already
tested in template_test.rb, so that test is gone.
@Oglopf Oglopf mentioned this pull request Oct 20, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Port checking in batch_connect wrongly identifying ports as available

2 participants