Title: gh-123471: Make itertools.chain thread-safe by eendebakpt · Pull Request #135689 · python/cpython · GitHub
Open Graph Title: gh-123471: Make itertools.chain thread-safe by eendebakpt · Pull Request #135689 · python/cpython
X Title: gh-123471: Make itertools.chain thread-safe by eendebakpt · Pull Request #135689 · python/cpython
Description: The itertools.chain has two attributes that are mutated duration iteration, making it non-thread safe. Options to make it thread safe i) Use a lock (simple, but adds a performance penalty for the single-threaded case) ii) Make the iterator thread safe using atomics. For the second option we can stop clearing the lz->source on exhaustion (this is a standard mitigation to avoid setting lz->source to zero (and doing a decref) while another thread is still using lz->source). To signal the iterator that lz->source is exhausted we can either add a new attribute, or replace the while (lz->source != NULL) with while (true). This creates a slower path the the iterator is exhausted, but typically once the iterator is exhausted performance is not relevant any longer. The lz->active is more problematic. Once lz->active is exhausted, we need to update this with a new one (e.g. PyIter_Next(lz->source). But lz->active only has a single reference count. So updating in one thread means having to decref the old lz->active. Possible mitigations: a) atomically load-and-incref the lz->active in chain_next b) add a new attribute that contains all the exhausted values of lz->active and clear this once the chainobject itself is deallocated. The second option is complex and has some performance issues (e.g. more incref/decrefs, keeping references to exhausted iterators longer in memory), so in this PR we pick the first option. Issue: gh-123471
Open Graph Description: The itertools.chain has two attributes that are mutated duration iteration, making it non-thread safe. Options to make it thread safe i) Use a lock (simple, but adds a performance penalty for the s...
X Description: The itertools.chain has two attributes that are mutated duration iteration, making it non-thread safe. Options to make it thread safe i) Use a lock (simple, but adds a performance penalty for the s...
Opengraph URL: https://github.com/python/cpython/pull/135689
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:45e08812-8f64-0d42-af3b-d83b558088f8 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BD12:2691C1:2E2410:3E34F5:6A57FCB6 |
| html-safe-nonce | e08e2505fbe320c2056ce30cc0877175cac08fd26abc0f266bd79595c6d65022 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDEyOjI2OTFDMToyRTI0MTA6M0UzNEY1OjZBNTdGQ0I2IiwidmlzaXRvcl9pZCI6IjYyMTQ5ODEyNTcyMjA1ODY2NzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c40fbb33e94f695eae3d9b732cc35bfb7560f0a42e933c1a23276e4645d7ed21 |
| hovercard-subject-tag | pull_request:2602700590 |
| 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/135689/files |
| twitter:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| og:image:alt | The itertools.chain has two attributes that are mutated duration iteration, making it non-thread safe. Options to make it thread safe i) Use a lock (simple, but adds a performance penalty for the s... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 49c8c15fabcbf356d607a90ca115c13b273e42ff8b74155de050fd229a9b0121 |
| 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 | 3fb1f684e7a833eb1b2d01d39875a2b52cb4fe9b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width