Skip to content

fix(hiredis): raise ConnectionError instead of AttributeError on concurrent disconnect - #4136

Merged
petyaslavova merged 2 commits into
redis:masterfrom
Stunned1:fix/hiredis-parser-concurrent-disconnect-4003
Jul 3, 2026
Merged

petyaslavova merged 2 commits into
redis:masterfrom
Stunned1:fix/hiredis-parser-concurrent-disconnect-4003

Conversation

@Stunned1

@Stunned1 Stunned1 commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

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 with AttributeError: 'NoneType' object has no attribute 'feed' (also recv_into/gets). with redis: disconnects on exit, and _HiredisParser.on_disconnect() sets self._sock and self._reader to None; a read in flight on another thread then hits the None. Since it's an AttributeError and not a ConnectionError, the retry/reconnect path never catches it and it surfaces to the caller.

Changes (redis/_parsers/hiredis.py):

  • read_from_socket: bind self._sock/self._reader to locals, raise a ConnectionError if either is None, and use the locals consistently (also fixes an existing self._sock re-read).
  • read_response: bind self._reader to a local and read through it, so a disconnect during read_from_socket() can't turn a later .gets() into an AttributeError.
  • tests/test_connection.py: regression tests for the None-socket / None-reader cases and the read_response race.

Notes:

  • Binding to a local also closes the race. Re-checking the attribute each time would still be a TOCTOU race.
  • Sync hiredis only: the async parser's on_disconnect() doesn't null _reader/_stream, so it isn't affected.
  • This doesn't make sharing one connection across threads safe (still unsupported); it just turns the crash into a proper, retryable error.

Pull Request check-list

Please make sure to review and check all of these items:

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)? -NA
  • Is there an example added to the examples folder (if applicable)? -NA

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 _sock and _reader, which used to surface as AttributeError on recv_into, feed, or gets()β€”bypassing retry/reconnect logic.

In redis/_parsers/hiredis.py, read_from_socket now snapshots _sock and _reader locally, raises ConnectionError if either is already None, and uses those locals for I/O. read_response snapshots _reader once and parses through it so a disconnect during read_from_socket() does not break subsequent .gets() calls.

Adds regression tests in tests/test_connection.py for disconnected socket/reader at read start and for completing a read after _reader is cleared mid-loop.

Reviewed by Cursor Bugbot for commit b70cb39. Bugbot is set up for automated code reviews on this repo. Configure here.

…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
@jit-ci

jit-ci Bot commented Jun 25, 2026

Copy link
Copy Markdown

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.

@Stunned1 Stunned1 changed the title fix(hiredis): raise ConnectionError instead of AttributeError on concurrent disconnect fix(hiredis): raise ConnectionError instead of AttributeError on concurrent disconnect (#4003) Jun 25, 2026
@Stunned1

Copy link
Copy Markdown
Contributor Author

CI tests pass on my fork!

@Stunned1

Copy link
Copy Markdown
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!

@Stunned1 Stunned1 changed the title fix(hiredis): raise ConnectionError instead of AttributeError on concurrent disconnect (#4003) fix(hiredis): raise ConnectionError instead of AttributeError on concurrent disconnect Jun 26, 2026
@petyaslavova

Copy link
Copy Markdown
Collaborator

Hey @Stunned1, thank you for your contribution! I'll review it shortly.

@Stunned1

Copy link
Copy Markdown
Contributor Author

Hey @Stunned1, thank you for your contribution! I'll review it shortly.

Thank you! :)

@petyaslavova petyaslavova left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@petyaslavova
petyaslavova merged commit ef83cd8 into redis:master Jul 3, 2026
678 of 680 checks passed
@petyaslavova petyaslavova added the maintenance Maintenance (CI, Releases, etc) label Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Maintenance (CI, Releases, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: 'NoneType' object has no attribute 'feed' in hiredis parser with connection pool

2 participants