Skip to content

AttributeError: module 'socket' has no attribute 'TCP_KEEPINTVL' on Windows and macOS #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ksaurabh02 opened this issue Apr 8, 2025 · 3 comments

Comments

@ksaurabh02
Copy link
Contributor

I'm trying to use the anthropic-sdk on both Windows and macOS, but I'm encountering the following error on both platforms:

anthropic\_base_client.py:795 Message: AttributeError: module 'socket' has no attribute 'TCP_KEEPINTVL'

After investigating, I found that the socket.TCP_KEEPINTVL attribute isn't available on all platforms, particularly Windows and macOS.

To resolve the issue locally, I modified _base_client.py as follows:

if "transport" not in kwargs:
    socket_options = [
        (socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
    ]

    if getattr(socket, 'TCP_KEEPINTVL', None) is not None:
        socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 60)]
    elif sys.platform == 'darwin':
        TCP_KEEPALIVE = getattr(socket, 'TCP_KEEPALIVE', 0x10)
        socket_options += [(socket.IPPROTO_TCP, TCP_KEEPALIVE, 60)]

    if getattr(socket, 'TCP_KEEPCNT', None) is not None:
        socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)]

    TCP_KEEPIDLE = getattr(socket, "TCP_KEEPIDLE", None)
    if TCP_KEEPIDLE is not None:
        socket_options.append((socket.IPPROTO_TCP, TCP_KEEPIDLE, 60))

With this change, the SDK works as expected on both platforms.

Would this be a valid fix to submit as a PR? Happy to open one if this approach looks good.

@RobertCraigie
Copy link
Collaborator

I'm surprised you're seeing that error on macos, I was under the impression it was always available on macos 🤔

PR would be great, thank you!

@ksaurabh02
Copy link
Contributor Author

@RobertCraigie Sure, created a PR #935

@ksaurabhAparavi
Copy link
Contributor

@RobertCraigie Please have a look at the PR.

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

No branches or pull requests

3 participants