Skip to content

Commit a67deeb

Browse files
committed
Fix race condition around subprocess inference state tidyup
There was a race condition due to the combination of Python's object ids being re-usable and Jedi persisting such ids beyond the real lifeteime of some objects. This could lead to the subprocess' view of the lifetime of `InferenceState` contexts getting out of step with that in the parent process and resulting in errors when removing them. It is also possible that this could result in erroneous results being reported, however this was not directly observed. The race was specifically: - `InferenceState` A created, gets id 1 - `InferenceStateSubprocess` A' created, uses `InferenceState` A which it stores as a weakref and an id - `InferenceStateSubprocess` A' is used, the sub-process learns about an `InferenceState` with id 1 - `InferenceState` A goes away, `InferenceStateSubprocess` A' is not yet garbage collected - `InferenceState` B created, gets id 1 - `InferenceStateSubprocess` B' created, uses `InferenceState` B which it stores as a weakref and an id - `InferenceStateSubprocess` B' is used, the sub-process re-uses its entry for an `InferenceState` with id 1 At this point the order of operations between the two `InferenceStateSubprocess` instances going away is immaterial -- both will trigger a removal of a state with id 1. As long as B' doesn't try to use the sub-process again after the first removal has happened then the second removal will fail. This commit resolves the race condition by coupling the context in the subprocess to the corresponding manager class instance in the parent process, rather than to the consumer `InferenceState`. See inline comments for further details.
1 parent 9d18b7c commit a67deeb

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

jedi/inference/compiled/subprocess/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def _cleanup_process(process, thread):
106106
class _InferenceStateProcess:
107107
def __init__(self, inference_state: 'InferenceState') -> None:
108108
self._inference_state_weakref = weakref.ref(inference_state)
109-
self._inference_state_id = id(inference_state)
110109
self._handles: Dict[int, AccessHandle] = {}
111110

112111
def get_or_create_access_handle(self, obj):
@@ -159,6 +158,27 @@ def __init__(
159158
self._used = False
160159
self._compiled_subprocess = compiled_subprocess
161160

161+
# Opaque id we'll pass to the subprocess to identify the context (an
162+
# `InferenceState`) which should be used for the request. This allows us
163+
# to make subsequent requests which operate on results from previous
164+
# ones, while keeping a single subprocess which can work with several
165+
# contexts in the parent process. Once it is no longer needed(i.e: when
166+
# this class goes away), we also use this id to indicate that the
167+
# subprocess can discard the context.
168+
#
169+
# Note: this id is deliberately coupled to this class (and not to
170+
# `InferenceState`) as this class manages access handle mappings which
171+
# must correspond to those in the subprocess. This approach also avoids
172+
# race conditions from successive `InferenceState`s with the same object
173+
# id (as observed while adding support for Python 3.13).
174+
#
175+
# This value does not need to be the `id()` of this instance, we merely
176+
# need to ensure that it enables the (visible) lifetime of the context
177+
# within the subprocess to match that of this class. We therefore also
178+
# depend on the semantics of `CompiledSubprocess.delete_inference_state`
179+
# for correctness.
180+
self._inference_state_id = id(self)
181+
162182
def __getattr__(self, name):
163183
func = _get_function(name)
164184

@@ -330,6 +350,10 @@ def delete_inference_state(self, inference_state_id):
330350
Note: it is not guaranteed that the corresponding state will actually be
331351
deleted immediately.
332352
"""
353+
# Warning: if changing the semantics of context deletion see the comment
354+
# in `InferenceStateSubprocess.__init__` regarding potential race
355+
# conditions.
356+
333357
# Currently we are not deleting the related state instantly. They only
334358
# get deleted once the subprocess is used again. It would probably a
335359
# better solution to move all of this into a thread. However, the memory
@@ -397,6 +421,9 @@ def _run(self, inference_state_id, function, args, kwargs):
397421
if inference_state_id is None:
398422
return function(*args, **kwargs)
399423
elif function is None:
424+
# Warning: if changing the semantics of context deletion see the comment
425+
# in `InferenceStateSubprocess.__init__` regarding potential race
426+
# conditions.
400427
del self._inference_states[inference_state_id]
401428
else:
402429
inference_state = self._get_inference_state(function, inference_state_id)

0 commit comments

Comments
 (0)