Skip to content

Connection is not thread-safe #1178

@gloryfromca

Description

@gloryfromca

Version: redis==2.10.6

Platform: Python 3.7 on Ubuntu 18.04

Description:
I encountered the same question with the link: celery/celery#4363

I reproduce the error by inserting time.sleep() in Connection.connect which is trying to initialize self._sock, then start two thread, make both pass self._sock existence check and run into Connection._on_connect (apprently Connection.connnect without any thread-lock).

So when one thread in on_connect() call read_response, it maybe get another thread's response, then raise the error.

def on_connect(self):
    "Initialize the connection, authenticate and select a database"
    self._parser.on_connect(self)

    # if a password is specified, authenticate
    if self.password:
        self.send_command('AUTH', self.password)
        if nativestr(self.read_response()) != 'OK':
            raise AuthenticationError('Invalid Password')

    # if a database is specified, switch to it
    if self.db:
        self.send_command('SELECT', self.db)
        res = self.read_response()
        if nativestr(res) != 'OK':
            raise ConnectionError('Invalid Database')

Is this a bug or something I don't get?

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