Skip to content

fix(services/memcached): TCP address resolution#7701

Merged
erickguan merged 3 commits into
apache:mainfrom
flip1995:tcp-address-resolve-fix
Jun 22, 2026
Merged

fix(services/memcached): TCP address resolution#7701
erickguan merged 3 commits into
apache:mainfrom
flip1995:tcp-address-resolve-fix

Conversation

@flip1995

@flip1995 flip1995 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

PR #7112 added support for Unix sockets to the memcached backend. As a side effect, it switched the TCP address parsing from being done inside of TcpStream::connect to SocketAddr::parse. The major difference is that SocketAddr::parse cannot resolve addresses like localhost:1234, as it errors out if it sees non-octal numbers in the address.

I didn't create an issue, but can do so if required.

cc @zenyanle @tisonkun

Rationale for this change

#7112 claimed:

Existing TCP configurations (e.g., 127.0.0.1:11211) remain unaffected.

But it broke TCP configurations like tcp://localhost:11211.

So, this seemed like an unintended breaking change to me. I also can't see the benefit of doing that, as TcpStream::connect already calls to_socket_addrs internally.

What changes are included in this PR?

Remove the additional and more restrictive SocketAddr parsing.

Are there any user-facing changes?

This restores the behavior from before the 0.56.0 release. This is required to bump the opendal dependency in sccache:

which fixes a transitive GCS issue that was fixed in opendal in version 0.56.0:

AI Usage Statement

I used Gemini to find the PR #7112 that introduced this change, but came up and implemented the fix myself.

@flip1995 flip1995 requested a review from Xuanwo as a code owner June 5, 2026 10:58
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. releases-note/fix The PR fixes a bug or has a title that begins with "fix" labels Jun 5, 2026
@flip1995 flip1995 changed the title [memcached] Fix TCP address resolution fix(memcached): TCP address resolution Jun 5, 2026
@flip1995 flip1995 changed the title fix(memcached): TCP address resolution fix(services/memcached): TCP address resolution Jun 5, 2026
@flip1995

flip1995 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Both of those CI failures seem unrelated to this PR.

@flip1995 flip1995 force-pushed the tcp-address-resolve-fix branch from 38488ef to 8f7ee39 Compare June 8, 2026 17:48
PR apache#7112 added support for Unix sockets to the memcached backend. As a
side effect, it switched the TCP address parsing from being done inside
of `TcpStream::connect` to `SocketAddr::parse`. The major difference is
that `SocketAddr::parse` cannot resolve addresses like `localhost:1234`,
as it errors out if it sees non-octal numbers in the address.

This seemed like an unintended breaking change to me. I also can't see
the benefit of doing that, as `TcpStream::connect` already calls
`to_socket_addrs` internally.
@flip1995 flip1995 force-pushed the tcp-address-resolve-fix branch from 8f7ee39 to 0cfaa3c Compare June 9, 2026 17:37
@flip1995

Copy link
Copy Markdown
Contributor Author

Nice, CI is passing now 🎉

Friendly ping @Xuanwo, could you take a look at this please? This is blocking a fix in sccache for the GCS backend.

@flip1995

Copy link
Copy Markdown
Contributor Author

Bump @Xuanwo: This is a really small 5 line PR, can you please take a look?

@erickguan erickguan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Can we add regression tests?

Something similar to:

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::net::TcpListener;

    #[tokio::test]
    async fn connect_tcp_socket() -> std::io::Result<()> {
        # test a few other use cases
        let listener = TcpListener::bind("127.0.0.1:0").await?;
        let addr = listener.local_addr()?.to_string();

        let accepted = tokio::spawn(async move {
            let _ = listener.accept().await?;
            Ok::<_, std::io::Error>(())
        });

        let _stream = SocketStream::connect_tcp(&addr).await?;
        accepted.await.unwrap()?;

        Ok(())
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn connect_unix_socket() -> std::io::Result<()> {
        use std::time::{SystemTime, UNIX_EPOCH};
        use tokio::net::UnixListener;

        # can use tempfile instead.
        let path = std::env::temp_dir().join(format!(
            "opendal-memcached-{}.sock",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));

        let listener = UnixListener::bind(&path)?;

        let accepted = tokio::spawn(async move {
            let _ = listener.accept().await?;
            Ok::<_, std::io::Error>(())
        });

        let _stream = SocketStream::connect_unix(path.to_str().unwrap()).await?;
        accepted.await.unwrap()?;

        let _ = std::fs::remove_file(path);
        Ok(())
    }
}

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jun 22, 2026
@flip1995

flip1995 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Good idea, I added regression tests for IpV4, IpV6 and the localhost version that was fixed by this PR, as well as the unix_socket test you suggested.

There's no direct dependency on tempfile yet, so I used the env::temp_dir.

@flip1995 flip1995 requested a review from erickguan June 22, 2026 13:07

@erickguan erickguan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some final comment updates and we are good to go!

Comment thread core/services/memcached/src/core.rs
Comment thread core/services/memcached/src/core.rs
Comment thread core/services/memcached/src/core.rs Outdated
@flip1995

Copy link
Copy Markdown
Contributor Author

Thanks for getting back to this quickly! Improved the comments, as you suggested.

@flip1995 flip1995 requested a review from erickguan June 22, 2026 15:32

@erickguan erickguan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 22, 2026
@erickguan erickguan merged commit 931baac into apache:main Jun 22, 2026
105 checks passed
@flip1995 flip1995 deleted the tcp-address-resolve-fix branch June 22, 2026 16:36
@flip1995

Copy link
Copy Markdown
Contributor Author

Thanks for the review 🙏

Do you have a release schedule? Or are you just releasing whenever enough features/fixes have accumulated?

// In the future, we could set up a webdav server to test TCP connection properly.
#[tokio::test]
async fn connect_tcp_socket() -> std::io::Result<()> {
for addr in &["127.0.0.1:11211", "localhost:11211", "[::1]:11211"] {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I usually don't use hard-coded port number -- 11211 seems to be a well-known port number for memcached.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is fine in a test case. And all other tests I saw here and in sccache use the hard-coded 11211 port number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer releases-note/fix The PR fixes a bug or has a title that begins with "fix" size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants