Title: GH-141148: Ensure decimal.getcontext() returns fresh copy. by nascheme · Pull Request #151953 · python/cpython · GitHub
Open Graph Title: GH-141148: Ensure decimal.getcontext() returns fresh copy. by nascheme · Pull Request #151953 · python/cpython
X Title: GH-141148: Ensure decimal.getcontext() returns fresh copy. by nascheme · Pull Request #151953 · python/cpython
Description: Ensure that decimal.getcontext() returns a per-task copy of the decimal.Context so that mutations are isolated between asyncio tasks and threads when sys.flags.thread_inherit_context is set. This change is required because decimal.Context instances are mutable. The contextvars.Context object uses an immutable data structure (HAMT) to store variable bindings. That ensures each task has its own set of contextvar bindings. However, it doesn't help if the contextvar value itself is mutated. This is an alternative to GH-146482. In this PR, we add a "depth" counter to PyContext, incremented every time we copy the HAMT. Then, the decimal.Context() instance can use this to determine that getcontext() needs to copy the instance. This ensures each thread or asyncio task has it's own copy of the decimal.Context() instance. This differs from GH-146482 as follows: it avoids the potential problem of keeping context vars alive longer, via the ctx_vars_origin reference. Instead, we are just using a 64-bit integer counter here to track depth. it avoids copying decimal.Context in the case that the context is reused (entered) many times, e.g. for asyncio tasks it creates slightly more work for the _pydecimal and _decimal.c code, since it has to track the depth on its Context instance Issue: gh-141148
Open Graph Description: Ensure that decimal.getcontext() returns a per-task copy of the decimal.Context so that mutations are isolated between asyncio tasks and threads when sys.flags.thread_inherit_context is set. This c...
X Description: Ensure that decimal.getcontext() returns a per-task copy of the decimal.Context so that mutations are isolated between asyncio tasks and threads when sys.flags.thread_inherit_context is set. This c...
Opengraph URL: https://github.com/python/cpython/pull/151953
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:a7d91272-f5d8-29ea-32fa-e696c689f285 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9FC8:2052BE:EF5074:14B1317:6A53B3F4 |
| html-safe-nonce | 050649176ea0339e3e620e28736de96429622a6449f5dbc5a4fa5cb7bfa49a06 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkM4OjIwNTJCRTpFRjUwNzQ6MTRCMTMxNzo2QTUzQjNGNCIsInZpc2l0b3JfaWQiOiI1Mjc0NTgzODEyODU1MzY3NTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1212fc3a07ad9e2c5f9a139295936ce1c669e18763b93c447d30d2b0f5368f71 |
| hovercard-subject-tag | pull_request:3915847403 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/151953/files |
| twitter:image | https://avatars.githubusercontent.com/u/690853?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/690853?s=400&v=4 |
| og:image:alt | Ensure that decimal.getcontext() returns a per-task copy of the decimal.Context so that mutations are isolated between asyncio tasks and threads when sys.flags.thread_inherit_context is set. This c... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width