Skip to content

shard_connection: Support abstract Unix socket clients - #257

Merged
fcostaoliveira merged 8 commits into
redis:masterfrom
LINKIWI:abstract-unix-socket
Oct 17, 2026
Merged

fcostaoliveira merged 8 commits into
redis:masterfrom
LINKIWI:abstract-unix-socket

Conversation

@LINKIWI

@LINKIWI LINKIWI commented May 26, 2024 •

Copy link
Copy Markdown
Contributor

Problem

Support servers that expose only Linux abstract Unix sockets, including environments with no writable socket directory. --unix-socket @NAME selects that namespace; ordinary filesystem sockets remain supported.

Root cause

Upstream constructs only filesystem socket addresses. Abstract addresses instead require a leading NUL and an exact address length without a trailing terminator. The original PR also copied a source-sized string into the fixed sun_path buffer; one byte over the limit reproducibly aborted in the fortified build.

Fix

  • On Linux, translate @NAME to the abstract namespace, including the leading NUL in the address length and excluding a terminating NUL.
  • Accept up to 107 name bytes after @, including a completely filled sun_path. Reject longer names with exit 1 before creating a socket; never truncate to another abstract endpoint.
  • Preserve existing filesystem address construction. Use ./@NAME for a Linux relative filesystem path beginning with @. On other platforms a bare @NAME remains a filesystem path.
  • Document the Linux-only syntax in help and the generated man page; retain full length, platform and filesystem-escape semantics in README. No TCP backlog/retry behavior is changed; Retry full Unix socket listen queues without blocking the event loop #258 remains separate.
  • Preserve the original author commits and integrate upstream master bc8e3161fc1a04b4204361b06ec5a66793e213ea with normal merges.

Tests

  • On the original PR code merged with master d3006c0, the new overlong-name regression fails with SIGABRT / buffer overflow detected. Four other cases pass.
  • Final build against bc8e316: format checks pass; make check passes 13/13 unit tests.
  • Unix integration suite passes 5/5 both through RLTest and standalone Python. Private Unix RESP peers verify AUTH, 16 SET replies, and JSON operation counts. Cases cover normal abstract names, the full 107-byte UTF-8 boundary, one-byte and much longer overflow, ordinary filesystem sockets, and escaped filesystem names beginning with @. A listener at the truncated prefix verifies oversized abstract names never connect there.
  • Independent semantics review passes 22 head/baseline probes, including empty abstract names, raw byte names, reconnect AUTH/operation counts, existing filesystem truncation parity, and fail-once rejection with unlimited reconnect enabled. The owner ASan/LSan suite passes 5/5.
  • Native macOS CI passed all three OpenSSL matrix cells: each runs two filesystem cases (including a bare @ pathname), skips three Linux-only cases, and passes 13/13 unit tests.

Compatibility decision

The Linux @NAME syntax is deliberate: it follows the familiar abstract-socket display convention and reuses the existing endpoint option. A separate flag would add another permanent endpoint configuration; a new textual prefix would also reserve pathname syntax. The explicit ./@NAME escape preserves access to relative filesystem sockets. Filesystem truncation and non-Linux path interpretation retain their existing behavior. An empty abstract name remains valid and is covered by an independently bound endpoint.

Affected versions

This is a new feature, not a regression fix in a released version. Linux abstract Unix sockets are unavailable in upstream master bc8e316; the unmerged feature therefore has no released introducing commit. The unbounded copy is confined to the original unmerged PR implementation. Existing filesystem Unix sockets keep their prior behavior apart from reserving a leading @ for abstract names on Linux; ./@NAME retains filesystem access.


Note

Medium Risk
Changes connection address construction for --unix-socket on Linux (new @NAME semantics) but rejects overlong names instead of truncating; filesystem behavior is preserved aside from reserving @ on Linux.

Overview
Adds Linux abstract Unix socket support so --unix-socket @NAME connects in the abstract namespace without a filesystem path, while filesystem sockets and ./@NAME for a path starting with @ stay unchanged.

shard_connection now builds the correct sockaddr_un length (leading NUL for abstract names, no silent truncation) and fails with a clear error when the name exceeds 107 bytes after @. Help text, man page, and README document the Linux-only @ syntax and security note.

CI smoke tests fall back to a default image matrix when repo variables are missing on fork PRs, and macOS jobs run the new tests/test_unix_socket.py regression suite (filesystem, escaped @ paths, abstract names, and overlong-name rejection).

Reviewed by Cursor Bugbot for commit e620dc8. Bugbot is set up for automated code reviews on this repo. Configure here.

CI compatibility: fork PRs can receive an empty vars.SMOKE_TEST_IMAGES; that prevented the distro matrix from being created while all visible jobs passed. The workflow now falls back to the same seven images currently configured upstream. An explicit repository variable still takes precedence. Independent focused build review and actionlint pass; fresh matrix execution is required.

🤖 Generated with Claude Code

@LINKIWI
LINKIWI force-pushed the abstract-unix-socket branch from cd61c5f to 862e5cc Compare January 22, 2025 14:14
@LINKIWI

LINKIWI commented Jan 22, 2025

Copy link
Copy Markdown
Contributor Author

@filipecosta90 This is still a patch we are interested in. Could you help review this? Thanks.

@LINKIWI
LINKIWI force-pushed the abstract-unix-socket branch from 862e5cc to 86637a8 Compare March 12, 2026 23:24

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Comment thread shard_connection.cpp Resolved
@LINKIWI
LINKIWI force-pushed the abstract-unix-socket branch from 86637a8 to bccd703 Compare March 12, 2026 23:39
@LINKIWI

LINKIWI commented Mar 12, 2026

Copy link
Copy Markdown
Contributor Author

@fcostaoliveira While this PR is quite old, we have been running this patch in production for some time, and are still interested in having this upstream. Can you review? Thanks.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this, and for picking the original 2024 commit back up rather than starting over. I went through it and have a few comments below.

The address construction looks right to me. name_len counts the @, so the <= sizeof(sun_path) check allows exactly 107 name bytes after the leading NUL, the memcpy stays in bounds, and the socklen is offsetof + name_len with no trailing terminator. The overlong-name test, with a listener bound at the truncated prefix, is a good way to show the name is rejected and never truncated to another endpoint. The 4096-byte case also covers the heap overwrite in the original version.

  • Compat: on Linux, -S @foo used to mean a relative filesystem socket named @foo and now means an abstract socket. The PR description covers this, and ./@NAME is a reasonable escape. I also think extending --unix-socket beats adding a new flag (same reasoning as "did you consider extending --protocol instead of adding another flag?" on PR#182). It's still a silent change in what an existing argument means, though, and someone with a relative @-prefixed socket would only find out from a connect failure. Since @ is the usual way abstract names are written, I think the break is acceptable. Worth a sentence in the release notes, I guess.

  • Where the length check runs: the constructor quietly sets m_unix_sockaddrlen = 0, and the error only shows up later in setup_socket(). With the default 4 threads × 50 clients, could that print "abstract UNIX socket name is too long" once per connection before the process exits? Why not validate once in main(), next to the existing "UNIX domain socket and TCP cannot be used together" check? sizeof(((struct sockaddr_un *) 0)->sun_path) is available there too. You'd get a single error and wouldn't need the zero sentinel in shard_connection. That said, I may be missing a reason it has to happen per connection.

  • Truncation: abstract names that are too long are now rejected, but filesystem paths that are too long still get cut short by strncpy, which can also end up connecting to a different socket. Keeping that as it was makes sense for this PR. Did you consider rejecting there too in a follow-up, so both paths behave the same?

  • CI scope: the SMOKE_TEST_IMAGES fallback in ci.yml isn't related to Unix sockets. The hardcoded image list will also drift from the repository variable over time. Would you be OK splitting it into its own PR? The macOS test_unix_socket.py step does belong here.

Nitpicking: m_unix_sockaddrlen could be socklen_t to document what it holds, though bufferevent_socket_connect() takes an int anyway, so this is optional.

The core change looks solid to me. I'd just like the validation-location question answered and the CI change split out before this merges.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator

Addressed these against final head 3793976 (runtime unchanged from 0d0fa08):

  • The suggested repeated-error/retry scenario does not occur. Independent semantics probes show an overlong name with multiple clients/threads exits 1 in milliseconds and emits exactly one length diagnostic, including with unlimited --reconnect-on-error. Initial preparation aborts on the first failed socket setup before launching workers. The local guard also prevents invalid addresses from creating a descriptor. I am retaining that bounded setup guard rather than duplicating platform-dependent length validation in main.
  • Filesystem truncation is pre-existing behavior, and paired baseline/head probes preserve it. Changing that contract would broaden this abstract-socket feature into a separate compatibility fix. Abstract names must not be truncated because address bytes are the endpoint identity.
  • Reserving Linux's leading @ is deliberate and now explicit in the body/README. ./@NAME preserves access to an existing relative filesystem socket, and non-Linux bare @NAME remains a filesystem name.
  • Fixed the generated-man concern in 3793976. Its socket section matches actual help2man output; full length/platform/escape details remain in README.
  • An empty abstract name is valid. An independent owned peer bound to the single leading NUL accepted AUTH and 20 SETs from -S @, with JSON Count20 and exit 0. Rejecting it would unnecessarily forbid a valid endpoint.

The semantics lens passes 22 independent probes, 13 unit tests and all five committed Unix tests. Owner ASan/LSan tests also pass 5/5. The remaining independent lenses and fresh CI still gate merge.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator

Rechecked the edited 05:57:59Z review on 3793976:

  1. Keeping validation next to sockaddr construction keeps its platform-specific byte capacity and exact length in one place. The sentinel is rejected before socket creation. Initial preparation stops at the first failure, before worker threads launch: independent multi-client/thread probes exit1 in milliseconds with one diagnostic, including unlimited reconnect. Moving the same check into main would duplicate address-construction knowledge without changing that result.
  2. Filesystem truncation remains explicitly out of this feature's compatibility scope; paired master/head tests preserve it.
  3. The empty abstract endpoint is valid and tested, not just a theoretically accepted string: a peer bound to a single leading NUL received AUTH and20 SETs from -S @, JSON Count20, exit0.
  4. The body now explains the syntax alternatives: the familiar @ convention reuses the existing endpoint option; a separate flag adds permanent endpoint configuration, and another textual prefix also reserves pathname syntax. The documented ./@NAME escape preserves filesystem access. The Linux interpretation change is deliberate.
  5. int matches libevent's connect API. The computed Linux address is bounded to offsetof(sun_path)+108 (110 bytes here), so conversion cannot overflow. No type change is needed.

All seven independent lenses are MERGE. Native macOS evidence is now in the body: all three OpenSSL cells pass the two filesystem tests, skip three Linux-only cases and pass13 units. The combined master+verification+JSON tree also passes14 units and10 focused interaction cases. Fresh final-head CI still gates merging.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator

Follow-up on the latest review:

  • The Linux @NAME interpretation is deliberate and documented as a compatibility change in the PR release notes, README, help and man page. ./@NAME keeps the filesystem meaning. A separate flag/prefix was considered; the established @ notation keeps one Unix endpoint option. An automatic fallback could connect to a different endpoint and is not appropriate.
  • Overlong-name validation runs during preparation, before worker launch. The default 4-thread/50-client case was independently tested: it exits 1 with exactly one length diagnostic. The constructor sentinel lets the socket layer reject it before creating a descriptor; duplicating the same platform-specific address rule in main is unnecessary. Filesystem truncation is pre-existing and tracked separately.
  • The CI fallback directly fixes the gating failure encountered on this fork PR (run 39008083816: matrix fromJSON received empty input). Keeping this small integration fix here lets the required seven distro jobs execute. Its literal exactly matches today's upstream configuration, while configured values always win; the default list is maintained with the workflow. Independent BUILD recheck and actionlint pass.
  • m_unix_sockaddrlen remains int because that is libevent's API parameter type and the address length is bounded by the small sockaddr_un structure.

@fcostaoliveira
fcostaoliveira merged commit 21a7879 into redis:master Oct 17, 2026
131 checks passed
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.

2 participants