Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions twikit/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def __init__(
self._user_id = None
self._user_agent = user_agent or 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15'
self._act_as = None
self._response_headers = None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (bug_risk): Initialization of _response_headers could lead to shared mutable state issues.

Since _response_headers is an instance variable, if the client is used concurrently, multiple requests may overwrite its value. Evaluate whether this state should be tied to individual requests or managed in a thread-safe manner.

Suggested implementation:

        self._act_as = None
        response_headers = response.headers

Make sure any subsequent code in this function or other functions that previously relied on self._response_headers now uses the local variable (response_headers) or otherwise has been refactored to access the response headers as needed.


self.gql = GQLClient(self)
self.v11 = V11Client(self)
Expand Down Expand Up @@ -149,6 +150,8 @@ async def request(
response = await self.http.request(method, url, headers=headers, **kwargs)
self._remove_duplicate_ct0_cookie()

self._response_headers = response.headers
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (bug_risk): Assignment of response headers to a shared state may be problematic in concurrent scenarios.

If the client is expected to handle concurrent requests, consider returning response headers with each request's result rather than storing them in a mutable instance variable. This can prevent potential race conditions.

Suggested implementation:

        # Removed setting shared response headers to avoid race conditions.
        return response_data, response.headers

Ensure that any callers of this function are updated to handle a tuple (data, headers) rather than expecting just the response data.


try:
response_data = response.json()
except json.decoder.JSONDecodeError:
Expand Down