8000 Custom settings for decimal context broke the app · Issue #4213 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

Custom settings for decimal context broke the app #4213

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

Closed
mikhaillazko opened this issue Mar 29, 2025 · 6 comments · Fixed by #4231
Closed

Custom settings for decimal context broke the app #4213

mikhaillazko opened this issue Mar 29, 2025 · 6 comments · Fixed by #4231
Assignees
Labels

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

@slafs
Copy link
slafs commented Apr 17, 2025

Hello! Is there any update on this one? I saw #4231 got closed, but nothing new seems to be opened yet? 🤔

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Apr 17, 2025
@sentrivana
Copy link
Contributor

We'll revive the closed PR and fix the issue this week folks, sorry for the wait.

In the longer term we're planning on moving away from Decimal in SDK internals altogether (just an fyi -- we're still fixing this bug before that happens).

B867
@sentrivana
Copy link
Contributor

Fix will be out in 2.27.0 in a bit. Let us know if anything is still not as it should be

@stephanie-anderson stephanie-anderson added the Python SDK label Apr 25, 2025 — with Linear
@JohnStrunk
Copy link

I'm still having problems w/ 2.27.0:

Traceback (most recent call last):
  File "tkinter\__init__.py", line 2068, in __call__
  File "wahoo_results.py", line 377, in <lambda>
    "write", lambda *_: icast.publish(model.scoreboard.get())
                        ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "imagecast.py", line 197, in publish
    with sentry_sdk.start_transaction(
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        op="publish_image", name="Publish image"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ) as txn:
    ^
  File "sentry_sdk\api.py", line 384, in start_transaction
  File "sentry_sdk\scope.py", line 1051, in start_transaction
  File "sentry_sdk\tracing.py", line 830, in __init__
  File "sentry_sdk\tracing_utils.py", line 878, in _generate_sample_rand
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]

For reference, my code sets:

decimal.DefaultContext.traps[decimal.FloatOperation] = True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants
0