Skip to content

Commit 84f94ad

Browse files
embhornclaude
andcommitted
Fix port collision with ephemeral range in broker.test
generate_port previously produced ports in [49152, 65535), which overlaps ~72% of the Linux default ephemeral port range (32768-60999). Under CI load the generated port could already be in use, causing bind() to fail with EADDRINUSE and the broker to exit silently. Dual-port tests (10a/10b/10c) needed two simultaneous free ports, doubling the exposure. Changes: - generate_port: use od -tu2 (unsigned decimal) and range [15000,32000) which is safely below both Linux and macOS ephemeral ranges - check_broker: emit a WARNING when the broker does not become ready within the timeout, so failures are visible rather than silent - start_broker / start_broker_dual: redirect broker output to a named log file in TMP_DIR so the CI "Show logs on failure" step captures the broker's error messages Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a1639db commit 84f94ad

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

scripts/broker.test

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,29 @@ do_cleanup() {
3939
[ ! -x ./$sub_bin ] && echo "$sub_bin not found" && exit 1
4040

4141
generate_port() {
42+
# Generate a port in [15000, 32000) to stay below the Linux ephemeral
43+
# port range (default 32768-60999) and macOS ephemeral range (49152-65535).
44+
# Use -tu2 to get unsigned decimal output and avoid the octal-vs-decimal
45+
# ambiguity that the legacy od output format creates in bash arithmetic.
46+
local n
4247
if [[ "$OSTYPE" == "linux"* ]]; then
43-
port=$(($(od -An -N2 /dev/urandom) % (65535-49152) + 49152))
48+
n=$(od -An -tu2 -N2 /dev/urandom | tr -d ' \n')
4449
elif [[ "$OSTYPE" == "darwin"* ]]; then
45-
port=$(($(od -An -N2 /dev/random) % (65535-49152) + 49152))
50+
n=$(od -An -tu2 -N2 /dev/random | tr -d ' \n')
4651
else
4752
port=11883
53+
return
4854
fi
55+
port=$((n % (32000 - 15000) + 15000))
4956
}
5057

5158
check_broker() {
5259
local check_port=${1:-$port}
53-
timeout 5 sh -c 'until nc -z $0 $1 2>/dev/null; do sleep 0.1; done' localhost $check_port
60+
if ! timeout 5 sh -c 'until nc -z $0 $1 2>/dev/null; do sleep 0.1; done' \
61+
localhost $check_port; then
62+
echo "WARNING: broker did not become ready on port $check_port" >&2
63+
return 1
64+
fi
5465
}
5566

5667
# Wait for a file to appear (used for subscriber ready synchronization)
@@ -89,7 +100,8 @@ start_broker() {
89100
if [ "$has_tls" -eq 1 ] && [ "$has_s_port" -eq 0 ]; then
90101
tls_args="-s $port"
91102
fi
92-
./$broker_bin "$@" -p $port $tls_args &
103+
broker_log="${TMP_DIR}/broker_p${port}.log"
104+
./$broker_bin "$@" -p $port $tls_args >"$broker_log" 2>&1 &
93105
broker_pid=$!
94106
check_broker
95107
}
@@ -107,7 +119,8 @@ start_broker_dual() {
107119
generate_port
108120
port_tls=$port
109121
port=$plain_port
110-
./$broker_bin "$@" -p $port -s $port_tls &
122+
broker_log="${TMP_DIR}/broker_dual_p${port}_t${port_tls}.log"
123+
./$broker_bin "$@" -p $port -s $port_tls >"$broker_log" 2>&1 &
111124
broker_pid=$!
112125
check_broker $port
113126
check_broker $port_tls

0 commit comments

Comments
 (0)