GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-112075: Make instance attributes stored in inline "dict" thread safe by DinoV · Pull Request #114742 · python/cpython

Conversation

@DinoV

@DinoV DinoV commented Jan 30, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Adds some locking around when we de-materialize a dictionary.

Also adds a comment where we'll need qsbr support for materialization.

  • Issue: Make dict objects thread-safe in --disable-gil builds #112075

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@DinoV DinoV added skip news topic-free-threading labels Jan 30, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Jan 30, 2024
Make dict objects thread-safe in --disable-gil builds #112075
Closed
@DinoV
DinoV force-pushed the nogil_dict_pydictvalues branch from 75c4227 to 4ac4f8b Compare January 30, 2024 01:11
@DinoV
DinoV marked this pull request as ready for review January 30, 2024 17:32
@DinoV
DinoV requested review from markshannon and methane as code owners January 30, 2024 17:32
@bedevere-app bedevere-app Bot added the awaiting core review label Jan 30, 2024
colesbury
colesbury reviewed Jan 31, 2024
View reviewed changes
Comment thread Objects/dictobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@DinoV
DinoV marked this pull request as draft February 3, 2024 01:04
@bedevere-app bedevere-app Bot removed the awaiting core review label Feb 3, 2024
@DinoV
DinoV force-pushed the nogil_dict_pydictvalues branch 4 times, most recently from 57327a2 to 0b7b010 Compare February 6, 2024 07:09
@markshannon

markshannon commented Feb 6, 2024

Copy link
Copy Markdown
Member

This looks inefficient, we really don't want locks in the fast path for attribute lookup.

Rather than putting locks around things, maybe we can revisit the way we handle objects with managed dictionaries to consider free-threading?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV

DinoV commented Feb 6, 2024

Copy link
Copy Markdown
Contributor Author

This looks inefficient, we really don't want locks in the fast path for attribute lookup.

Rather than putting locks around things, maybe we can revisit the way we handle objects with managed dictionaries to consider free-threading?

I assume you're referring to the latest unfinished version (as previous one only added a lock for dematerialization which as @colesbury pointed out isn't correct)? If so there aren't actually any locking for lookups in the fast path... There is an read w/ acquire semantics, but on strongly-ordered systems like x64 that's no different than a normal read anyway. It does introduce an incref/decref when reading from the dictionary. Also FWIW I think I don't need the spinning in _PyDictOrValues_TryGetDict and _PyDictOrValues_TryGetValues or the usage of _PYDICTORVALUES_UPDATING as I have in the current version.

I think we could get rid of the incref though if we disabled dematerialization in free-threading, but there's still going to be some ref count checks to see if the object is local, and if not to mark it as shared so that we'll free the values via QSBR. I'm not sure that can be avoided.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV

DinoV commented Feb 6, 2024

Copy link
Copy Markdown
Contributor Author

I've run into an existing complication with the existing solution I've been trying. The limitation in the byte code generator " Until the last DEOPT_IF, no objects may be allocated, INCREFed, or DECREFed." makes this leak the dict on de-opt. We could of course follow the error pattern of if (cond) { Py_DECREF(dict); DEOPT_IF(true); } but maybe that's a non-starter...

It also occurred to me that we're not just racing with de-materialization of the dict, but we're also racing with someone assigning to __dict__ and freeing the existing dict.

So an alternate plan might be:

  1. Don't incref the dicts for read
  2. Don't de-materialize shared dicts (but still de-materialize thread-local dicts)
  3. On assignment to __dict__ if the existing dict is shared then scheduled it to be freed via QSBR, otherwise free it immediately.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Feb 7, 2024

Copy link
Copy Markdown
Member

faster-cpython/ideas#651

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV
DinoV force-pushed the nogil_dict_pydictvalues branch 3 times, most recently from eadcc3e to 3c3537e Compare February 16, 2024 21:53
@DinoV
DinoV marked this pull request as ready for review February 16, 2024 22:55
@DinoV
DinoV requested a review from gvanrossum as a code owner February 16, 2024 22:55
@bedevere-app bedevere-app Bot added the awaiting core review label Feb 16, 2024
colesbury
colesbury reviewed Feb 20, 2024
View reviewed changes
Comment thread Objects/dictobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_object.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_object.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@DinoV
DinoV force-pushed the nogil_dict_pydictvalues branch 4 times, most recently from f896695 to b5a3231 Compare February 21, 2024 21:09
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 19, 2024
@colesbury colesbury added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@bedevere-bot

bedevere-bot commented Apr 19, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @colesbury for commit fc7d1a4 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@DinoV
DinoV force-pushed the nogil_dict_pydictvalues branch from fc7d1a4 to 4b85255 Compare April 19, 2024 18:22
@DinoV DinoV added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@bedevere-bot

bedevere-bot commented Apr 19, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @DinoV for commit 4b85255 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@DinoV DinoV added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@bedevere-bot

bedevere-bot commented Apr 19, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @DinoV for commit fac83b7 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
DinoV added 8 commits April 19, 2024 14:49
@DinoV
Make instance attributes stored in inline "dict" thread safe
048daa7
@DinoV
Insert into dict on store if dict is materialized and other feedback
322914f
@DinoV
_PyObject_TryStoreInstanceAttribute -> _PyObject_StoreInstanceAttribute
106685a
Fix issue where critical section isn't released
Make _PyObject_TryGetInstanceAttribute return a bool
@DinoV
Cleanup _PyObject_SetManagedDict, use cst for inline valid flag, fix …
11d450c
…issue w/ deleted dict
@DinoV
Refactor del/set into set_or_del_lock_held, and avoid locking on newl…
29968e0
…y materialized dict

Fix duplicate incref
Fix comment
Remove redundant if check on detach
@DinoV
Handle dict replacement case better
0023395
@DinoV
Decref materialized dict on fail to set
8433d25
@DinoV
Fix missing decref when storing and dict is detached but has been del…
ac503e8
…eted
@DinoV DinoV added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@bedevere-bot

bedevere-bot commented Apr 19, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @DinoV for commit fac83b7 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@DinoV
DinoV force-pushed the nogil_dict_pydictvalues branch from fac83b7 to ac503e8 Compare April 19, 2024 21:50
@DinoV DinoV added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@bedevere-bot

bedevere-bot commented Apr 19, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @DinoV for commit ac503e8 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 19, 2024
@DinoV
DinoV merged commit 8b541c0 into python:main Apr 22, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 22, 2024
@colesbury colesbury mentioned this pull request Apr 26, 2024
test_repl's test_no_memory: AssertionError: -6 not found in (1, 120) #118331
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@colesbury colesbury colesbury approved these changes
@methane methane Awaiting requested review from methane methane is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@gvanrossum gvanrossum Awaiting requested review from gvanrossum

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@DinoV @markshannon @gvanrossum @bedevere-bot @colesbury
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.