Skip to content

AttributeError when using Unix sockets with OTel metrics (v7.2.0) #3958

@Bojun-Feng

Description

@Bojun-Feng

After upgrading to redis-py 7.2.0 (released Feb 16, 2026), any Redis operations using Unix domain socket connections crash with:

AttributeError: 'UnixDomainSocketConnection' object has no attribute 'port'

Seems like a regression introduced by the new OpenTelemetry native metrics support added in #3954. The OTel instrumentation added accesses conn.host and conn.port unconditionally when recording metrics:

# redis/client.py - multiple locations
record_operation_duration(
    command_name=command_name,
    duration_seconds=time.monotonic() - start_time,
    server_address=conn.host,
    server_port=conn.port,  # <-- AttributeError for UnixDomainSocketConnection - no port attribute
    db_namespace=str(conn.db),
)

Environment

  1. Redis server listening on a Unix socket (not TCP)
  2. redis-py 7.2.0
  3. Execute any Redis command (e.g., PING)

Reproduction Script

Here's the Python script to reproduce the issue:

"""
Requirements:
- Redis server running with: redis-server --unixsocket /tmp/redis-test.sock --unixsocketperm 777 --port 0 --daemonize yes
- redis-py 7.2.0: pip install redis==7.2.0
"""
import redis
def main():
    r = redis.Redis(
        unix_socket_path='/tmp/redis-test.sock',
        encoding='utf-8',
        decode_responses=True
    )
    
    print(f'redis-py version: {redis.__version__}')
    print(f'Connection class: {r.connection_pool.connection_class.__name__}')
    print()
    print('Executing PING command...')
    
    # This triggers _execute_command() which crashes at line 748:
    #   server_port=conn.port,  # AttributeError!
    result = r.ping()
    print(f'Result: {result}')
if __name__ == '__main__':
    main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions