Skip to content

Conversation

oil-oil
Copy link
Contributor

@oil-oil oil-oil commented Jun 10, 2025

Description

When using consul as a service registry, if a user registers a service without filling in the port number, the default value is not set in apisix, resulting in svc_port possibly being nil, which in turn results in local service_id = svc_address ... “:” ... svc_port cannot be spliced.

Which issue(s) this PR fixes:

Fixes #11134

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@oil-oil oil-oil marked this pull request as ready for review June 10, 2025 02:07
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Jun 10, 2025
membphis
membphis previously approved these changes Jun 10, 2025
Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

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

LGTM


::RETRY::
local watch_result, watch_err = client:get(consul_server.consul_watch_catalog_url)
local watch_error_info = (watch_err ~= nil and watch_err)
Copy link
Member

Choose a reason for hiding this comment

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

shorter is clearer, you can take a look. if you would like my way, you can make a try

    local res, err = client:get(consul_server.consul_watch_catalog_url)
    err = err or (res and res.status ~= 200 and res.status)

nic-6443
nic-6443 previously approved these changes Jun 11, 2025
ronething
ronething previously approved these changes Jun 11, 2025
@bzp2010 bzp2010 self-requested a review June 11, 2025 04:33

local svc_address, svc_port = node.Service.Address, node.Service.Port
-- Handle nil or 0 port case - default to 80 for HTTP services
if not svc_port or svc_port == 0 then
Copy link
Contributor

@bzp2010 bzp2010 Jun 11, 2025

Choose a reason for hiding this comment

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

TCP and UDP ports can be 0.

Although it stands for "special use", it should be clearly distinguished from port is null. If this value is explicitly configured as 0, then we should accept it and not change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

We have verified that nginx/openresty does not allow 0 as a port, even though TCP/UDP is designed to allow it.
As verified by @oil-oil, Consul's default behavior on Port has changed; it used to be null when registered without entering it; now it auto-populates with 0 as the default.
Since APISIX is primarily oriented towards HTTP scenarios, we fallback to 80 (the HTTP default) if the Port is null (the old behavior) or 0 (the new behavior, but not allowed by nginx).

This behavior needs to be documented.

@Baoyuantop Baoyuantop moved this to 🏗 In progress in ⚡️ Apache APISIX Roadmap Jun 13, 2025
@Baoyuantop Baoyuantop moved this from 🏗 In progress to 👀 In review in ⚡️ Apache APISIX Roadmap Jun 13, 2025
@oil-oil oil-oil dismissed stale reviews from ronething, nic-6443, and membphis via ce5235e June 16, 2025 02:08

local svc_address, svc_port = node.Service.Address, node.Service.Port
-- Handle nil or 0 port case - default to 80 for HTTP services
if not svc_port or svc_port == 0 then
Copy link
Contributor

Choose a reason for hiding this comment

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

We have verified that nginx/openresty does not allow 0 as a port, even though TCP/UDP is designed to allow it.
As verified by @oil-oil, Consul's default behavior on Port has changed; it used to be null when registered without entering it; now it auto-populates with 0 as the default.
Since APISIX is primarily oriented towards HTTP scenarios, we fallback to 80 (the HTTP default) if the Port is null (the old behavior) or 0 (the new behavior, but not allowed by nginx).

This behavior needs to be documented.

@bzp2010 bzp2010 requested a review from kayx23 June 16, 2025 06:48
@Baoyuantop Baoyuantop merged commit 5ac6264 into apache:master Jun 17, 2025
24 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in ⚡️ Apache APISIX Roadmap Jun 17, 2025
Crazy-xyr pushed a commit to Crazy-xyr/apisix that referenced this pull request Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Discover service error by Consul for apisix version 3.9

6 participants