Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom settings for decimal context broke the app #4213

Open
mikhaillazko opened this issue Mar 29, 2025 · 2 comments
Open

Custom settings for decimal context broke the app #4213

mikhaillazko opened this issue Mar 29, 2025 · 2 comments

Comments

@mikhaillazko
Copy link

mikhaillazko commented Mar 29, 2025

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.24.0

Steps to Reproduce

Hi! I have an issue with decimal. I defined custom rules for decimal type like this:

def decimal_setup() -> None:
    BasicContext.traps[Inexact] = True
    BasicContext.traps[FloatOperation] = True
    setcontext(BasicContext)

and call this function before FastAPI initialization

decimal_setup()
fastapi_app: FastAPI = FastAPI(
    middleware=middlewares,
)

@fastapi_app.get('api/init')
def init() -> dict:
    return {'status': 'OK'}

An error occurs when i running the application and calling the API.
Thanks for great tool!

Expected Result

The application should not crash.

Actual Result

This is stack trace that i see

Traceback (most recent call last):
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/integrations/starlette.py", line 408, in _sentry_patched_asgi_app
    return await middleware(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/integrations/asgi.py", line 158, in _run_asgi3
    return await self._run_app(scope, receive, send, asgi_version=3)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/integrations/asgi.py", line 197, in _run_app
    transaction = continue_trace(
                  ^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/api.py", line 431, in continue_trace
    return get_isolation_scope().continue_trace(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/scope.py", line 1161, in continue_trace
    transaction = Transaction.continue_from_headers(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/tracing.py", line 518, in continue_from_headers
    transaction = Transaction(**kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/tracing.py", line 818, in __init__
    self._sample_rand = _generate_sample_rand(self.trace_id)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mika/Dev/optifino/.venv/lib/python3.12/site-packages/sentry_sdk/tracing_utils.py", line 876, in _generate_sample_rand
    return Decimal(sample_rand).quantize(
           ^^^^^^^^^^^^^^^^^^^^
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]

It looks like you need to define a local context for this operation using decimal.localcontext

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Mar 29, 2025
@antonpirker
Copy link
Member

Hey @mikhaillazko thanks for reporting this! I will have a look

@antonpirker
Copy link
Member

I have a PR with a fix up @mikhaillazko. We will probably release this next week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants