zulip: Reset session on ConnectionError to fix stale connections. - #922
Open
HeetRanpura wants to merge 1 commit into
Open
zulip: Reset session on ConnectionError to fix stale connections.#922HeetRanpura wants to merge 1 commit into
HeetRanpura wants to merge 1 commit into
Conversation
When a network middlebox silently drops an idle TCP connection, the next request raises a ConnectionError. Previously the retry logic would reuse the same dead session, failing all retry attempts because the connection was never refreshed. Now we close and null out the session on ConnectionError so ensure_session() creates a fresh one on the next retry. Fixes zulip#761.
HeetRanpura
force-pushed
the
fix-stale-connection-session-reset
branch
from
July 31, 2026 19:06
c595a57 to
2287d04
Compare
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.
Overview
When a network middlebox silently drops an idle TCP connection,
requests.Sessionkeeps that dead socket in its connection pool. The nextAPI call tries to reuse it and raises a
ConnectionError. Previously theretry loop in
do_api_querycaught this error but never closed the brokensession, so every retry attempt reused the same dead connection and failed.
Root Cause
ensure_session()was called once before thewhile Trueloop, so after aConnectionErrorthe session was never rebuilt on retry. All 10 retriesattempted to send requests over the same stale TCP connection.
Fix
Two minimal changes to
do_api_query:ensure_session()inside thewhile Trueloop so it runs on everyConnectionError, close and null outself.sessionbefore retrying.ensure_session(), seesNone, and creates aThe existing
error_retrymachinery is reused completely unchanged. No newparameters, timeouts, or retry counters were added.
Why This Approach
PR #854 added a parallel retry system with new timeouts and keep-alive
headers - too broad. PR #913 was on the right track (session reset) but
had linting failures and stalled. This PR takes the same minimal approach
with full lint compliance and proper test coverage.
Test Results
Full project suite (437 tests, 0 failures):
Linting (ruff + mypy):
Gitlint:
Tests Added
zulip/tests/test_do_api_query.py:test_session_reset_on_connection_error- First session raisesConnectionError; verifies the stale session isclose()d, a freshtest_session_not_reset_when_never_connected-ConnectionErrorbeforeUnrecoverableNetworkErrorwithoutFixes Improve error handling when a request fails due to the persistent connection being expired #761.