fix: add configurable HTTP timeout and preserve error context#318
fix: add configurable HTTP timeout and preserve error context#318skyc1e wants to merge 1 commit intoPolymarket:mainfrom
Conversation
The default httpx timeout (~5s) causes silent order duplication when the server is slow under load: requests time out client-side, the SDK raises a generic PolyApiException, callers retry, but the original order was already matched server-side. Add an optional timeout parameter to ClobClient so users can increase the HTTP timeout for their use case (e.g. timeout=30 for volatile markets). Also chain the original httpx exception so callers can distinguish timeouts from connection errors. Closes Polymarket#273
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| Reconfigure the module-level HTTP client with a custom timeout (in seconds). | ||
| """ | ||
| global _http_client | ||
| _http_client = httpx.Client(http2=True, timeout=timeout) |
There was a problem hiding this comment.
Old HTTP client not closed, leaking connections
Medium Severity
set_http_timeout replaces the module-level _http_client with a new httpx.Client without calling .close() on the old one. Since the client is created with http2=True, the old instance holds open HTTP/2 connections and associated resources (sockets, TLS state) that are never properly released, causing a resource leak.
| self.mode = self._get_client_mode() | ||
|
|
||
| if timeout is not None: | ||
| set_http_timeout(timeout) |
There was a problem hiding this comment.
Per-instance timeout parameter actually mutates global state
High Severity
The timeout parameter on ClobClient.__init__ suggests per-instance configuration, but set_http_timeout replaces a module-level global _http_client shared by all instances. Creating a second ClobClient with a different (or no) timeout silently overrides the first client's timeout. Ironically, this can reintroduce the exact duplicate-order problem the PR aims to fix — a user sets timeout=30 on one client, then another component instantiates a default client, resetting the timeout to httpx's default.


Summary
Fixes #273.
The default httpx timeout (~5s) causes silent order duplication under load: requests time out client-side, the SDK raises a generic PolyApiException, callers assume failure and retry, but the original order was already matched server-side. Multiple users reported duplicate fills from this behavior.
Changes
timeoutparameter toClobClient.__init__()— when provided, reconfigures the module-level httpx client with the specified timeout in secondsUsage
Test plan
Note
Medium Risk
Changes HTTP client configuration and error propagation for all SDK requests, which could alter runtime behavior (timeouts) and exception handling in consumers.
Overview
Adds an optional
timeoutargument toClobClient.__init__that reconfigures the module-levelhttpxclient via newset_http_timeout, allowing callers to override default request timeouts.Improves HTTP error reporting by including the original
httpx.RequestErrormessage and chaining the exception (raise ... from exc) so consumers can distinguish timeouts and connection failures.Written by Cursor Bugbot for commit b69208f. This will update automatically on new commits. Configure here.