Repository navigation
fix(hiredis): raise ConnectionError instead of AttributeError on concurrent disconnect - #4136
Merged
petyaslavova merged 2 commits intoJul 3, 2026
Conversation
β¦urrent disconnect When a Redis client is shared across threads and operations are wrapped in `with redis:`, one thread's __exit__ disconnects the connection while another is mid-read. _HiredisParser.on_disconnect() sets both self._sock and self._reader to None, so the in-flight read dereferences None and crashes with `AttributeError: 'NoneType' object has no attribute 'feed'` (also 'recv_into' and 'gets'). Because that is an AttributeError rather than a ConnectionError, it bypasses the retry/reconnect machinery and surfaces to the caller. Bind self._sock/self._reader to locals in read_from_socket() and read_response(), raise a descriptive ConnectionError when either is None, and read through the locals so a concurrent disconnect can no longer turn a later .gets()/.feed() into an AttributeError. The async parser is unaffected since its on_disconnect() does not null _reader/_stream. Fixes redis#4003
|
Hi, Iβm Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope youβll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
Contributor
Author
|
CI tests pass on my fork! |
Contributor
Author
|
Items 4 & 5 in the checklist are inapplicable to my changes(internal bug fix with no API changes and no new user-facing functionality). Please let me know if there's anything needed! |
Collaborator
|
Hey @Stunned1, thank you for your contribution! I'll review it shortly. |
Contributor
Author
Thank you! :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of change
Fixes #4003.
When a Redis client is shared across threads with per-operation
with redis:blocks, the sync hiredis parser can crash withAttributeError: 'NoneType' object has no attribute 'feed'(alsorecv_into/gets).with redis:disconnects on exit, and_HiredisParser.on_disconnect()setsself._sockandself._readertoNone; a read in flight on another thread then hits theNone. Since it's anAttributeErrorand not aConnectionError, the retry/reconnect path never catches it and it surfaces to the caller.Changes (
redis/_parsers/hiredis.py):read_from_socket: bindself._sock/self._readerto locals, raise aConnectionErrorif either isNone, and use the locals consistently (also fixes an existingself._sockre-read).read_response: bindself._readerto a local and read through it, so a disconnect duringread_from_socket()can't turn a later.gets()into anAttributeError.tests/test_connection.py: regression tests for theNone-socket /None-reader cases and theread_responserace.Notes:
on_disconnect()doesn't null_reader/_stream, so it isn't affected.Pull Request check-list
Please make sure to review and check all of these items:
NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.
Note
Low Risk
Narrow sync hiredis parser change with targeted error-mapping and regression tests; no auth or API surface changes.
Overview
Fixes #4003: when another thread disconnects a shared sync client (e.g. exiting
with redis:) while a read is in flight,on_disconnect()clears_sockand_reader, which used to surface asAttributeErroronrecv_into,feed, orgets()βbypassing retry/reconnect logic.In
redis/_parsers/hiredis.py,read_from_socketnow snapshots_sockand_readerlocally, raisesConnectionErrorif either is alreadyNone, and uses those locals for I/O.read_responsesnapshots_readeronce and parses through it so a disconnect duringread_from_socket()does not break subsequent.gets()calls.Adds regression tests in
tests/test_connection.pyfor disconnected socket/reader at read start and for completing a read after_readeris cleared mid-loop.Reviewed by Cursor Bugbot for commit b70cb39. Bugbot is set up for automated code reviews on this repo. Configure here.