Skip to content

feat: add SOCKS proxy support via aiohttp-socks ProxyConnector#23798

Open
MagicAbdel wants to merge 1 commit into
OpenAPITools:masterfrom
MagicAbdel:master
Open

feat: add SOCKS proxy support via aiohttp-socks ProxyConnector#23798
MagicAbdel wants to merge 1 commit into
OpenAPITools:masterfrom
MagicAbdel:master

Conversation

@MagicAbdel
Copy link
Copy Markdown

@MagicAbdel MagicAbdel commented May 15, 2026

fixes #23797

This PR adds SOCKS proxy support to the asyncio Python client template.
When a kubeconfig cluster entry contains a proxy-url with a SOCKS
scheme (e.g. socks5://localhost:1080), the async client currently
fails with:

aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected

This happens because aiohttp.TCPConnector does not support SOCKS
schemes at the request level — a dedicated ProxyConnector from
aiohttp-socks is required at the session level instead.

Changes

modules/openapi-generator/src/main/resources/python-asyncio/rest.mustache

  • Add SUPPORTED_SOCKS_PROXIES frozenset constant for the four SOCKS
    schemes (socks4, socks4a, socks5, socks5h)
  • Detect SOCKS scheme via partition("://") — no extra dependencies
  • Route SOCKS proxies to aiohttp_socks.ProxyConnector instead of
    aiohttp.TCPConnector
  • Lazy import of ProxyConnector so non-SOCKS users are unaffected
  • Set self.proxy = None when using SOCKS connector to avoid double
    proxy application per-request

Notes

  • HTTP/HTTPS proxy code path completely unchanged
  • All four SOCKS schemes supported: socks4, socks4a, socks5, socks5h
  • Requires aiohttp>=3.10.0 and aiohttp-socks>=0.9.0
  • Complements the urllib3 SOCKS fix for the sync Python client template
  • Non-SOCKS users are completely unaffected — aiohttp-socks is never
    imported unless a SOCKS proxy URL is configured
  • Users with a SOCKS proxy who don't have aiohttp-socks installed will
    receive a clear ImportError instead of the previous cryptic
    ServerDisconnectedError
  • self.proxy is set to None when using ProxyConnector to avoid
    double proxy application — the connector handles routing at the
    connection level, not per-request

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Add SOCKS proxy support to the asyncio Python client using aiohttp-socks ProxyConnector, fixing failures when proxy-url uses socks* schemes. Fixes #23797.

  • New Features

    • Support socks4, socks4a, socks5, socks5h via session-level ProxyConnector.
    • Auto-detect proxy scheme; aiohttp HTTP/HTTPS behavior unchanged.
    • Lazy import; avoids double proxy by clearing per-request proxy/header fields.
    • Requires aiohttp>=3.10.0 and aiohttp-socks>=0.9.0 (install when using SOCKS).
  • Refactors

    • Simplified SOCKS detection in the sync template: use partition("://") and a frozenset of schemes (no helper function).

Written for commit c0cd45c. Summary will update on new commits. Review in cubic

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 7 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache:82">
P2: Configured SOCKS proxy may be conflicted by environment proxy settings because `self.proxy = None` removes the explicit proxy override while `trust_env=True` remains active on the session.</violation>
</file>

<file name="samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py">

<violation number="1" location="samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py:82">
P1: Connector instantiated in synchronous `__init__` may fail on aiohttp 3.10+ outside a running event loop</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Re-trigger cubic

proxy = configuration.proxy
proxy_scheme = (proxy or "").partition("://")[0].lower()

if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: Connector instantiated in synchronous __init__ may fail on aiohttp 3.10+ outside a running event loop

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py, line 82:

<comment>Connector instantiated in synchronous `__init__` may fail on aiohttp 3.10+ outside a running event loop</comment>

<file context>
@@ -74,8 +76,27 @@ def __init__(self, configuration) -> None:
+        proxy = configuration.proxy
+        proxy_scheme = (proxy or "").partition("://")[0].lower()
+
+        if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
+            # SOCKS proxies require ProxyConnector - aiohttp TCPConnector
+            # does not support SOCKS schemes
</file context>

limit=self.maxsize,
ssl=self.ssl_context,
)
self.proxy = None # connector handles proxy
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: Configured SOCKS proxy may be conflicted by environment proxy settings because self.proxy = None removes the explicit proxy override while trust_env=True remains active on the session.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache, line 82:

<comment>Configured SOCKS proxy may be conflicted by environment proxy settings because `self.proxy = None` removes the explicit proxy override while `trust_env=True` remains active on the session.</comment>

<file context>
@@ -65,8 +67,27 @@ class RESTClientObject:
+                limit=self.maxsize,
+                ssl=self.ssl_context,
+            )
+            self.proxy = None          # connector handles proxy
+            self.proxy_headers = None  # not applicable for SOCKS
+        else:
</file context>

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.

[REQ] Feature Request Description

1 participant