Skip to content

Preserve source addresses when pickling adapters - #415

Open
Rajatendu1 wants to merge 1 commit into
requests:mainfrom
Rajatendu1:fix/source-adapter-pickle
Open

Rajatendu1 wants to merge 1 commit into
requests:mainfrom
Rajatendu1:fix/source-adapter-pickle

Conversation

@Rajatendu1

Copy link
Copy Markdown

Fixes #394.

SourceAddressAdapter needs to include source_address in the attributes saved by HTTPAdapter.__getstate__. Without it, restoring a pickled adapter or Session raises AttributeError when Requests recreates the pool manager.

Use the same __attrs__ extension already used by SSLAdapter and FingerprintAdapter.

This picks up the fix proposed in #395, which gyanu2507 closed unmerged after narrowing their projects and explicitly invited others to pick up. Credit to that contribution for the existing diagnosis and fix. This PR adds broader regression coverage rather than claiming a new discovery.

Tests cover string and tuple source addresses (including a nonzero port), pickle protocols 0 and 2, adapters and Sessions, shared HTTP/HTTPS mounts, retained retry/pool settings, rebuilt pool managers and proxy source addresses. Two additional tests make a real loopback HTTP request through an unpickled Session.

Validation on Linux:

  • CPython 3.11.17: python -m pytest -W error::DeprecationWarning, 176 passed, 5 skipped. Requests 2.34.2, urllib3 2.8.0, pyOpenSSL 24.2.1, cryptography 43.0.3. Unchanged baseline: 166 passed, 5 skipped.
  • Ten new cases fail on unchanged main with the missing source_address error and pass with the patch.
  • CPython 3.12.15: 176 passed, 5 skipped, with 31 third-party deprecation warnings. The full warnings-as-errors run has the same 27 warning failures on unchanged baseline; affected tests pass with warnings treated as errors.
  • Python 2.7.18 on PyPy 8.0.1: source/related-adapter/Session tests, 23 passed; broader suite excluding multipart encoder tests, 147 passed, 7 skipped, with deprecation warnings treated as errors. The complete PyPy suite was killed during multipart tests and is not claimed as passing.
  • Flake8 on changed Python files passed on Python 2.7 and 3.12.
  • Sphinx documentation build succeeded with one existing theme deprecation warning.

No Windows/macOS or complete interpreter matrix run is claimed. No TLS-policy change is included.

AI assistance was used for investigation, implementation and testing.

Requests restores pool managers using serialized adapter attributes.
Include the source address so adapter and Session round-trips retain their
binding configuration. Follow the existing SSLAdapter pattern and add
direct, proxy and loopback regression coverage.

Pick up the fix from the closed, unmerged PR requests#395, whose author explicitly
invited others to continue it.

Closes requests#394
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unpickling a session with source adapter fails

1 participant