DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
510 stars 401 forks source link

UnboundLocalError: cannot access local variable 'result' where it is not associated with a value #9548

Closed iamlikeme closed 2 weeks ago

iamlikeme commented 1 month ago

Summary of problem

Datadog is occasionally hitting UnboundLocalError. This happens because local variable result is undefined in this finally clause:

https://github.com/DataDog/dd-trace-py/blob/3b81c75f34645a587e9c5696d0213145e42d8466/ddtrace/contrib/redis_utils.py#L77-L79

Which version of dd-trace-py are you using?

2.9.1

Which version of pip are you using?

24.0

Which libraries and their versions are you using?

`pip freeze` aiofiles==23.2.1 aiohttp==3.9.5 aiosignal==1.3.1 alembic==1.13.1 anyio==4.3.0 arq==0.25.0 asttokens==2.4.1 async-cache==1.1.1 async-lru==2.0.4 asyncache==0.3.1 asyncpg==0.29.0 asyncssh==2.14.2 attrs==23.2.0 azure-core==1.30.1 azure-identity==1.16.1 azure-storage-blob==12.20.0 backoff==2.2.1 bcrypt==4.1.3 Brotli==1.1.0 bytecode==0.15.1 cachetools==5.3.3 cattrs==23.2.3 certifi==2024.2.2 cffi==1.16.0 chardet==5.2.0 charset-normalizer==3.3.2 click==8.1.7 cryptography==42.0.8 cssselect2==0.7.0 datadog==0.49.1 ddsketch==3.0.1 ddtrace==2.9.1 decorator==5.1.1 Deprecated==1.2.14 dnspython==2.6.1 docstring_parser==0.16 email_validator==2.1.1 emoji==2.12.1 envier==0.5.1 et-xmlfile==1.1.0 executing==2.0.1 fastapi==0.111.0 fastapi-cli==0.0.4 fonttools==4.51.0 frozenlist==1.4.1 gcloud-aio-auth==5.3.1 gcloud-aio-storage==9.3.0 greenlet==3.0.3 h11==0.14.0 hiredis==2.3.2 html5lib==1.1 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 idna==3.7 importlib-metadata==6.11.0 ipython==8.25.0 isodate==0.6.1 jedi==0.19.1 Jinja2==3.1.4 lxml==5.2.2 Mako==1.3.5 markdown-it-py==3.0.0 MarkupSafe==2.1.5 matplotlib-inline==0.1.7 mdurl==0.1.2 msal==1.28.0 msal-extensions==1.1.0 multidict==6.0.5 mypy-extensions==1.0.0 netsgiro==2.0.1 numpy==1.26.4 openpyxl==3.1.4 opentelemetry-api==1.24.0 orjson==3.10.3 packaging==24.0 pandas==2.2.2 parso==0.8.4 passlib==1.7.4 pdfkit==1.0.0 pexpect==4.9.0 phonenumbers==8.13.38 pillow==10.3.0 platformdirs==4.2.2 portalocker==2.8.2 prompt-toolkit==3.0.43 protobuf==4.25.3 psutil==5.9.8 psycopg2==2.9.9 ptyprocess==0.7.0 pure-eval==0.2.2 pyasn1==0.6.0 pyasn1_modules==0.4.0 pycparser==2.22 pydantic==1.10.16 pydyf==0.10.0 Pygments==2.18.0 PyJWT==2.8.0 pypdf==4.2.0 pyphen==0.15.0 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 python-json-logger==2.0.7 python-multipart==0.0.9 pytz==2024.1 PyYAML==6.0.1 rapidfuzz==3.9.1 redis==5.0.5 requests==2.32.3 requests-file==2.1.0 requests-toolbelt==1.0.0 rich==13.7.1 rsa==4.9 ruff==0.4.8 sentry-sdk==2.5.1 setuptools==70.0.0 shellingham==1.5.4 six==1.16.0 sniffio==1.3.1 SQLAlchemy==2.0.30 sqlparse==0.5.0 stack-data==0.6.3 starlette==0.37.2 thefuzz==0.22.1 tinycss2==1.3.0 tornado==6.2 tqdm==4.66.4 traitlets==5.14.3 typed-argument-parser==1.10.0 typer==0.12.3 typing-inspect==0.9.0 typing_extensions==4.12.2 tzdata==2024.1 ua-parser==0.18.0 ujson==5.10.0 urllib3==2.2.1 user-agents==2.2.0 uvicorn==0.30.1 uvloop==0.19.0 watchfiles==0.21.0 wcwidth==0.2.13 weasyprint==62.2 webencodings==0.5.1 websockets==12.0 wrapt==1.16.0 xmlsec==1.3.14 xmltodict==0.13.0 yarl==1.9.4 zeep==4.2.1 zipp==3.18.2 zopfli==0.2.3

How can we reproduce your problem?

:thinking:

What is the result that you get?

I see this exception traceback in logs:

UnboundLocalError: cannot access local variable 'result' where it is not associated with a value
  File "__main__.py", line 53, in main
    worker.run()
  File "arq/worker.py", line 301, in run
    self.loop.run_until_complete(self.main_task)
  File "asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
  File "ddtrace/contrib/asyncio/patch.py", line 50, in traced_coro
    return await coro
  File "arq/worker.py", line 354, in main
    await self._poll_iteration()
  File "arq/worker.py", line 379, in _poll_iteration
    job_ids = await self.pool.zrangebyscore(
  File "ddtrace/contrib/redis/asyncio_patch.py", line 20, in traced_async_execute_command
    return await _run_redis_command_async(span=span, func=func, args=args, kwargs=kwargs)
  File "ddtrace/contrib/redis_utils.py", line 79, in _run_redis_command_async
    rowcount = determine_row_count(redis_command=redis_command, result=result)

What is the result that you expected?

UnboundLocalError not being raised.

emmettbutler commented 1 month ago

Thanks for catching this, @iamlikeme. I've opened #9552 fixing it.