[BUG] AsyncTwilioHttpClient returns another request's response under concurrency
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 35/100
Piste de recherche
Start with twilio/http/async_http_client.py and inspect AsyncTwilioHttpClient.request, especially how it stores and returns the response around session.close(). Run the concurrency example in the issue, then add or run a regression test that confirms each concurrent request returns its own response. Done means the test returns /first and /second for their matching requests; a pull request is already open for this fix.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
AsyncTwilioHttpClient.request stores the response in self._test_only_last_response and returns that attribute instead of a local variable. When the client has no pooled session (pool_connections=False), it awaits session.close() after the store. A second request that starts during that await resets the attribute to None and later overwrites it with its own response. The first request then returns None or the second request's response.
With None, Version.request fails with 'NoneType' object has no attribute 'status_code'. With the wrong response, the caller gets another request's body, for example the SID of a different message.
One client is the normal case: Client(...) builds one AsyncTwilioHttpClient and reuses it for every call. Any application that awaits two Twilio calls at the same time on one client can hit this. We hit it in production at about three messages.create_async calls per second.
The synchronous TwilioHttpClient has the same shape but never yields between the store and the return, so it is not affected.
Code snippet
No network needed. A fake stands in for ClientSession. It answers with the request path and yields to the event loop on close, as a real session does.
import asyncio
from unittest.mock import patch
from twilio.http.async_http_client import AsyncTwilioHttpClient
class FakeResponse:
def __init__(self, text):
self._text = text
self.status = 200
self.headers = {}
async def text(self):
return self._text
class FakeSession:
async def request(self, **kwargs):
return FakeResponse(kwargs["url"])
async def close(self):
await asyncio.sleep(0)
async def main():
with patch("twilio.http.async_http_client.ClientSession", FakeSession):
client = AsyncTwilioHttpClient(pool_connections=False)
responses = await asyncio.gather(
client.request("GET", "/first"), client.request("GET", "/second")
)
print([r.text if r is not None else None for r in responses])
asyncio.run(main())
With real credentials the same happens with two client.messages.create_async(...) calls in an asyncio.gather on a Client built with http_client=AsyncTwilioHttpClient(pool_connections=False).
Actual behavior
['/second', '/second']
The first request returns the second request's response. Depending on timing the first request returns None instead, and the SDK raises:
AttributeError: 'NoneType' object has no attribute 'status_code'
Expected behavior
['/first', '/second']
Each request returns its own response.
twilio-python version
9.4.6 in production. 9.12.0 and main carry the same code.
Python version
3.13.4
Logs or error messages
Failed to send SMS via Twilio: 'NoneType' object has no attribute 'status_code'
Additional context
The fix is to keep the response in a local variable and return it. The method can still set _test_only_last_response for the tests that read it. I have a pull request ready with the fix and a regression test.
- Langage dominant
- Python
- Étoiles
- 2.1k
- Forks
- 821
- Merge moyen
- 9 h 33 min
- PR mergées (30 j)
- 3
Préparer son environnement
- Fournit un Dockerfile ou un fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de twilio/twilio-python
-
Docs: "Handling Exceptions" should mention that network errors are not `TwilioRestException`Peut-être pris @henricop l’a pris il y a 13 jours. Ouverte
Difficulté 1/5 1-3 heures Accessibilité débutants 90/100
twilio/twilio-python#986 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Clearer diagnostic when an API Key SID is passed without `account_sid`Peut-être pris @saryeljaramany l’a pris il y a 5 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
twilio/twilio-python#985 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
TokenPagination.next_page_async() makes a sync request and fails with an async http clientPeut-être pris @sebxt l’a pris il y a 31 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
twilio/twilio-python#976 ·
Les mainteneurs répondent en général sous 1 jour
-
AsyncTwilioHttpClient does not JSON-serialize request bodies for application/json endpoints (sync client does) → 400 on JSON-body v2 endpointsPeut-être pris @Steve0x2a l’a pris il y a 77 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
twilio/twilio-python#932 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
twilio/twilio-python#930 ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de twilio/twilio-python
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 64/100
Les mainteneurs répondent en général sous 1 jour
-
Harden tools/pr_threads.sh: pass all printed thread fields through the deterministic sanitizerOuvertearea:cli area:security enhancement priority:medium size:small
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
RonaldHensbergen/composable-data-stack#1338 ·
Les mainteneurs répondent en général sous 1 jour
-
area:stdlib effort:S impact:med platform:linux type:bug
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
Les mainteneurs répondent en général sous 1 jour
-
automated endpoint-dead needs-curation
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
almanac-data/climate-almanac#139 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
UKGovernmentBEIS/inspect_ai#6695 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour