Skip to content

Why does Pipeline.close() ends the connection? #3571

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

Closed
Frank995 opened this issue Mar 25, 2025 · 2 comments
Closed

Why does Pipeline.close() ends the connection? #3571

Frank995 opened this issue Mar 25, 2025 · 2 comments

Comments

@Frank995
Copy link

I was asked to post the discussion here from here.

I have a class written to interact with my Redis DB. In it, I thought about using a method for session handling (as I would do with other DBs) (exception handling is handled elsewhere):

    @contextlib.contextmanager
    def _make_session(self) -> Generator[redis.client.Pipeline]:
        session = self.client.pipeline()
        try:
            yield session
        finally:
            session.close()

Imagine that I perform a with istruction to open the Pipeline, running one or more commands and executing them. e.g.:

            with self._make_session() as session:
                return method(self, session, *args, **kwargs)

I noticed that the code above only works the first time in my backend, the latters raise a NoneType error. So I looked into the Pipeline code and found out that close calls the reset method that has this part at the end:

        if self.connection:
            self.connection_pool.release(self.connection)
            self.connection = None

Was this done for some reason in particular? It seems like that since the connection closes, from the second time I create a session it simply points to None. I had to quickly fix it like this:

    @contextlib.contextmanager
    def _make_session(self) -> Generator[redis.client.Pipeline]:
        yield self.client.pipeline()

Do you have any ideas of why?

@petyaslavova
Copy link
Collaborator

Hi @Frank995,
Could you please share more details about how your client is initialized? Are you using a synchronous or asynchronous client?

Additionally, please provide more information on what can be included in this method(self, session, *args, **kwargs).

Typically, when creating a pipeline object, the same instance is used to add all the commands that should be executed together. The actual communication with the server occurs when pipe.execute() is called. At this point, a connection is retrieved from the connection pool and assigned to the self.connection field for communication. At the end of the method, the reset method is executed, returning self.connection to the pool and resetting both the connection and the pipeline object's state.

If your code adds commands to the pipeline but does not call execute() before reset() is invoked, the accumulated list of commands will be lost, preventing execution.

@petyaslavova
Copy link
Collaborator

Closing this issue due to inactivity. Please reopen if needed.

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

No branches or pull requests

2 participants