[BUG] AsyncTwilioHttpClient returns another request's response under concurrency

オープン
#974 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@andret13pinto がすでに取り組んでいます。

2026年10月19日 から。

  • #975 @andret13pinto による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
api, backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
2.1k
フォーク
821
平均マージ
9時間 33分
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

twilio/twilio-python のほかの issue

twilio/twilio-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。