FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159739: Preserve ContextVar cache updates from finalizers by jelle-openai · Pull Request #160197 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 13 additions & 0 deletions Lib/test/test_context.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,19 @@ def fun():
contextvars.copy_context().run(fun)
self.assertEqual(c.get(), [])

@isolated_context
def test_reset_with_reentrant_finalizer(self):
var = contextvars.ContextVar('var')
var.set('original')

class Reenter:
def __del__(self):
var.set('finalizer')

token = var.set(Reenter())
var.reset(token)
self.assertEqual(var.get(), 'finalizer')

def test_context_copy_1(self):
ctx1 = contextvars.Context()
c = contextvars.ContextVar('c', default=42)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :class:`contextvars.ContextVar` returning a stale value after a reentrant finalizer changes it during :meth:`~contextvars.ContextVar.reset`.
4 changes: 2 additions & 2 deletions Python/context.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -872,13 +872,13 @@ contextvar_set(PyContextVar *var, PyObject *val)
return -1;
}

context_set_vars(ctx, new_vars);

#ifndef Py_GIL_DISABLED
var->var_cached = val; /* borrow */
var->var_cached_tsid = ts->id;
var->var_cached_tsver = ts->context_ver;
#endif
/* Releasing the old variables may run a finalizer that updates the cache. */
context_set_vars(ctx, new_vars);
return 0;
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL