Commit Graph

693 Commits

Author SHA1 Message Date
Peter Hawkins 9ad0c7b0f1
gh-132641: fix race in `lru_cache` under free-threading (#133787)
Fix race in `lru_cache` by acquiring critical section on the cache object itself and call the lock held variant of dict functions to modify the underlying dict.
2025-05-13 17:38:57 +00:00
Inada Naoki 92337f666e
gh-133703: dict: fix calculate_log2_keysize() (GH-133809) 2025-05-11 14:44:21 +09:00
Angela Liss 421ba589d0
gh-132762: Fix underallocation bug in `dict.fromkeys()`(gh-133627)
The function `dict_set_fromkeys()` adds elements of a set to an existing
dictionary. The size of the expanded dictionary was estimated with
`PySet_GET_SIZE(iterable)`, which did not take into account the size of the
existing dictionary.
2025-05-08 13:13:11 -04:00
Mae Hood 0119791326
dict: Remove redundant incref of immortal object Py_EMPTY_KEYS (GH-133200) 2025-05-01 08:39:26 +09:00
Mark Shannon 44e4c479fb
GH-124715: Move trashcan mechanism into `Py_Dealloc` (GH-132280) 2025-04-30 11:37:53 +01:00
Shantanu 7e8b153fef
gh-116436: Improve error message when TypeError occurs during dict update (#116443) 2025-04-29 22:18:06 -07:00
Victor Stinner 926ff69f3f
gh-132825: Fix typo in dict_unhashable_type() name (#132847) 2025-04-23 18:54:13 +02:00
Victor Stinner 426449d983
gh-132825: Enhance unhashable error messages for dict and set (#132828) 2025-04-23 17:10:09 +02:00
Kumar Aditya 4c3d187d9f
gh-126366: fix thread safety of dict key iterator (#132693) 2025-04-18 21:43:49 +05:30
Jelle Zijlstra 9a6b702f3a
dictobject.c: Fix incorrect function name in errors (#132193) 2025-04-07 07:48:55 -07:00
Bénédikt Tran edbf7fb129
gh-111178: remove redundant casts for functions with correct signatures (#131673) 2025-04-01 17:18:11 +02:00
Dino Viehland 2984ff9e51
gh-130373: Avoid locking in _LOAD_ATTR_WITH_HINT (#130372)
Avoid locking in _LOAD_ATTR_WITH_HINT
2025-03-28 15:16:41 -07:00
Michael Droettboom 8614f86b71
gh-131525: Cache the result of tuple_hash (#131529)
* gh-131525: Cache the result of tuple_hash

* Fix debug builds

* Add blurb

* Fix formatting

* Pre-compute empty tuple singleton

* Mostly set the cache within tuple_alloc

* Fixes for TSAN

* Pre-compute empty tuple singleton

* Fix for 32-bit platforms

* Assert that op != NULL in _PyTuple_RESET_HASH_CACHE

* Use FT_ATOMIC_STORE_SSIZE_RELAXED macro

* Update Include/internal/pycore_tuple.h

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

* Fix alignment

* atomic load

* Update Objects/tupleobject.c

Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
2025-03-27 09:57:06 -04:00
Sam Gross 5abff6960b
gh-117657: Fix data race in `compare_unicode_unicode_threadsafe` (gh-131746)
We can't safely check the type of the found key until we incref it or if we know that it's immortal.
2025-03-26 09:01:57 -04:00
Victor Stinner 20c5f969dd
gh-131238: Remove more includes from pycore_interp.h (#131480) 2025-03-19 23:01:32 +01:00
Sam Gross c00ac57824
gh-131113: Fix data race in dict.popitem() (gh-131115)
The clearing of the key, hash, and value need to use atomic operations
to avoid a data race with concurrent read operations.
2025-03-11 19:15:22 -04:00
Victor Stinner 9d759b63d8
gh-111178: Change Argument Clinic signature for METH_O (#130682)
Use "PyObject*" for METH_O functions to fix an undefined behavior.
2025-03-11 16:33:36 +01:00
Donghee Na 80e6d3ec49
gh-130547: Fix race between dict_dealloc and split_keys_entry_added (gh-130778) 2025-03-04 22:50:33 +09:00
Dino Viehland 6c450f44c2
gh-130313: Avoid locking when clearing objects (#130126)
Avoid locking when clearing objects in the free-threaded build
2025-02-20 13:32:57 -08:00
Dino Viehland 28f5e3de57
gh-129984: Mark immortal objects as deferred (#129985)
Mark immortal objects as deferred
2025-02-13 09:01:43 -08:00
Sam Gross 34379d0a59
gh-117657: Fix data race in `dict_dict_merge` (gh-129755)
Found while running `test_load_attr_module` from `test_opcache` under TSan.
2025-02-07 09:44:24 -05:00
Victor Stinner 95504f429e
gh-129354: Fix grammar in PyErr_FormatUnraisable() (#129475)
Replace "on verb+ing" with "while verb+ing".
2025-01-31 09:45:35 +01:00
Victor Stinner 4e47e05045
gh-129354: Use PyErr_FormatUnraisable() function (#129435)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-30 16:09:38 +01:00
Pieter Eendebak 64c417dee5
gh-112075: Remove critical section in dict.get (gh-129336)
The `dict.get` implementation uses `_Py_dict_lookup_threadsafe`, which is
thread-safe, so we remove the critical section from the argument clinic.

Add a test for concurrent dict get and set operations.
2025-01-28 21:55:45 +00:00
Victor Stinner 9012fa741d
gh-128863: Deprecate private C API functions (#128864)
Deprecate private C API functions:

* _PyBytes_Join()
* _PyDict_GetItemStringWithError()
* _PyDict_Pop()
* _PyThreadState_UncheckedGet()
* _PyUnicode_AsString()
* _Py_HashPointer()
* _Py_fopen_obj()

Replace _Py_HashPointer() with Py_HashPointer().

Remove references to deprecated functions.
2025-01-22 11:04:19 +00:00
Sam Gross d66c08aa75
gh-128923: Use zero to indicate unassigned unique id (#128925)
In the free threading build, the per thread reference counting uses a
unique id for some objects to index into the local reference count
table. Use 0 instead of -1 to indicate that the id is not assigned. This
avoids bugs where zero-initialized heap type objects look like they have
a unique id assigned.
2025-01-17 16:42:27 +01:00
mpage b5ee0258bf
gh-115999: Specialize `LOAD_ATTR` for instance and class receivers in free-threaded builds (#128164)
Finish specialization for LOAD_ATTR in the free-threaded build by adding support for class and instance receivers.
2025-01-14 11:56:11 -08:00
Neil Schemenauer 1b15c89a17
gh-115999: Specialize `STORE_ATTR` in free-threaded builds. (gh-127838)
* Add `_PyDictKeys_StringLookupSplit` which does locking on dict keys and
  use in place of `_PyDictKeys_StringLookup`.

* Change `_PyObject_TryGetInstanceAttribute` to use that function
  in the case of split keys.

* Add `unicodekeys_lookup_split` helper which allows code sharing
  between `_Py_dict_lookup` and `_PyDictKeys_StringLookupSplit`.

* Fix locking for `STORE_ATTR_INSTANCE_VALUE`.  Create
  `_GUARD_TYPE_VERSION_AND_LOCK` uop so that object stays locked and
  `tp_version_tag` cannot change.

* Pass `tp_version_tag` to `specialize_dict_access()`, ensuring
  the version we store on the cache is the correct one (in case of
  it changing during the specalize analysis).

* Split `analyze_descriptor` into `analyze_descriptor_load` and
  `analyze_descriptor_store` since those don't share much logic.
  Add `descriptor_is_class` helper function.

* In `specialize_dict_access`, double check `_PyObject_GetManagedDict()`
  in case we race and dict was materialized before the lock.

* Avoid borrowed references in `_Py_Specialize_StoreAttr()`.

* Use `specialize()` and `unspecialize()` helpers.

* Add unit tests to ensure specializing happens as expected in FT builds.

* Add unit tests to attempt to trigger data races (useful for running under TSAN).

* Add `has_split_table` function to `_testinternalcapi`.
2024-12-19 10:21:17 -08:00
Bénédikt Tran 9af96f4406
gh-127563: use `dk_log2_index_bytes=3` in empty dicts (GH-127568)
This fixes a UBSan failure (unaligned zero-size memcpy) in `dictobject.c`.
2024-12-10 16:58:17 +01:00
Petr Viktorin 690fe077f6
gh-126491: Revert "GH-126491: Lower heap size limit with faster marking (GH-127519)" (GH-127770)
Revert "GH-126491: Lower heap size limit with faster marking (GH-127519)"

This reverts commit 023b7d2141, which introduced
a refleak.
2024-12-10 11:53:56 +01:00
Mark Shannon 023b7d2141
GH-126491: Lower heap size limit with faster marking (GH-127519)
* Faster marking of reachable objects

* Changes calculation of work to do and work done.

* Merges transitive closure calculations
2024-12-06 10:46:59 +00:00
Sam Gross f4f530804b
gh-127582: Make object resurrection thread-safe for free threading. (GH-127612)
Objects may be temporarily "resurrected" in destructors when calling
finalizers or watcher callbacks. We previously undid the resurrection
by decrementing the reference count using `Py_SET_REFCNT`. This was not
thread-safe because other threads might be accessing the object
(modifying its reference count) if it was exposed by the finalizer,
watcher callback, or temporarily accessed by a racy dictionary or list
access.

This adds internal-only thread-safe functions for temporary object
resurrection during destructors.
2024-12-05 16:07:31 -05:00
Kumar Aditya 45c5cba318
gh-127316: fix incorrect assertion in setting `__class__` in free-threading (#127399) 2024-11-29 21:44:20 +05:30
mpage 09c240f20c
gh-115999: Specialize `LOAD_GLOBAL` in free-threaded builds (#126607)
Enable specialization of LOAD_GLOBAL in free-threaded builds.

Thread-safety of specialization in free-threaded builds is provided by the following:

A critical section is held on both the globals and builtins objects during specialization. This ensures we get an atomic view of both builtins and globals during specialization.
Generation of new keys versions is made atomic in free-threaded builds.
Existing helpers are used to atomically modify the opcode.
Thread-safety of specialized instructions in free-threaded builds is provided by the following:

Relaxed atomics are used when loading and storing dict keys versions. This avoids potential data races as the dict keys versions are read without holding the dictionary's per-object lock in version guards.
Dicts keys objects are passed from keys version guards to the downstream uops. This ensures that we are loading from the correct offset in the keys object. Once a unicode key has been stored in a keys object for a combined dictionary in free-threaded builds, the offset that it is stored in will never be reused for a different key. Once the version guard passes, we know that we are reading from the correct offset.
The dictionary read fast-path is used to read values from the dictionary once we know the correct offset.
2024-11-21 11:22:21 -08:00
Dino Viehland bf542f8bb9
gh-124470: Fix crash when reading from object instance dictionary while replacing it (#122489)
Delay free a dictionary when replacing it
2024-11-21 10:41:19 -06:00
Mark Shannon aea0c586d1
GH-127010: Don't lazily track and untrack dicts (GH-127027) 2024-11-20 16:41:20 +00:00
Hugo van Kemenade 899fdb213d
Revert "GH-126491: GC: Mark objects reachable from roots before doing cycle collection (GH-126502)" (#126983) 2024-11-19 11:25:09 +02:00
Mark Shannon b0fcc2c47a
GH-126491: GC: Mark objects reachable from roots before doing cycle collection (GH-126502)
* Mark almost all reachable objects before doing collection phase

* Add stats for objects marked

* Visit new frames before each increment

* Remove lazy dict tracking

* Update docs

* Clearer calculation of work to do.
2024-11-18 14:31:26 +00:00
Mark Shannon fa40922597
GH-126547: Pre-assign version numbers for a few common classes (GH-126551) 2024-11-08 16:44:44 +00:00
Prometheus3375 5527c4051c
gh-116938: Fix `dict.update` docstring and remove erraneous full stop from `dict` documentation (#125421)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
2024-10-29 23:00:04 +00:00
Sam Gross 3c4a7fa617
gh-124218: Avoid refcount contention on builtins module (GH-125847)
This replaces `_PyEval_BuiltinsFromGlobals` with
`_PyDict_LoadBuiltinsFromGlobals`, which returns a new reference
instead of a borrowed reference. Internally, the new function uses
per-thread reference counting when possible to avoid contention on the
refcount fields on the builtins module.
2024-10-24 12:44:38 -04:00
Arjun Singh 03f9264ece
fix grammar in comment in dictobject.c (#125822) 2024-10-22 11:48:16 +09:00
Sam Gross 9b0bfba2a2
gh-124218: Use per-thread reference counting for globals and builtins (#125713)
Use per-thread refcounting for the reference from function objects to
the globals and builtins dictionaries.
2024-10-21 12:51:29 -04:00
Sam Gross 5989eb7446
gh-125608: Trigger dictionary watchers when inline values change (#125611)
Dictionary watchers on an object's attributes dictionary
(`object.__dict__`) were not triggered when the managed dictionary used
the object's inline values.
2024-10-21 08:23:38 -04:00
Victor Stinner bb594e801b
gh-125196: Use PyUnicodeWriter for repr(dict) (#125270) 2024-10-10 20:41:14 +02:00
Mark Shannon c9014374c5
GH-125174: Make immortal objects more robust, following design from PEP 683 (GH-125251) 2024-10-10 18:19:08 +01:00
Sam Gross 5aa91c56bf
gh-124296: Remove private dictionary version tag (PEP 699) (#124472) 2024-10-01 12:39:56 -04:00
Dino Viehland 077e7ef6a0
gh-124642: Dictionaries aren't marking objects as weakref'd (#124643)
Dictionaries aren't marking objects as weakref'd
2024-09-30 10:04:32 -07:00
Mark Shannon 0e21cc6cf8
GH-124547: Clear instance dictionary if memory error occurs during object dealloc (GH-124627) 2024-09-27 14:51:01 -07:00
Donghee Na ad7c778546
gh-123990: Good bye WITH_FREELISTS macro (gh-124358) 2024-09-24 01:28:59 +00:00