[Python-Dev] PEP 567 v2

Guido van Rossum guido at python.org
Thu Jan 4 11:30:55 EST 2018


On Wed, Jan 3, 2018 at 6:35 PM, Nathaniel Smith <njs at pobox.com> wrote:

> On Wed, Jan 3, 2018 at 3:44 PM, Victor Stinner <victor.stinner at gmail.com>
> wrote:
> > Ok, I finally got access to a computer and I was able to test the PEP
> > 567 implementation: see my code snippet below.
> >
> > The behaviour is more tricky than what I expected. While running
> > context.run(), the context object is out of sync of the "current
> > context". It's only synchronized again at run() exit. So
> > ContextVar.set() doesn't immediately modifies the "current context"
> > object (set by Context.run()).
>
> To me this sounds like a oversight (= bug), not intended behavior. At
> the conceptual level, I think what we want is:
>
> - Context is a mutable object representing a mapping
> - BUT it doesn't allow mutation through the MutableMapping interface;
> instead, the only way to mutate it is by calling Context.run and then
> ContextVar.set(). Funneling all 'set' operations through a single
> place makes it easier to do clever caching tricks, and it lets us
> avoid dealing with operations that we don't want here (like 'del')
> just because they happen to be in the MutableMapping interface.
> - OTOH we do implement the (read-only) Mapping interface because
> there's no harm in it and it's probably useful for debuggers.
>

I think that in essence what Victor saw is a cache consistency issue. If
you look at the implementation section in the PEP, the ContextVar.set()
operation mutates _ContextData, which is a private (truly) immutable data
structure that stands in for the HAMT, and the threadstate contains one of
these (not a Context). When you call copy_context() you get a fresh Context
that wraps the current _ContextData. Because the latter is effectively
immutable this is a true clone. ctx.run() manipulates the threadstate to
make the current _ContextData the one from ctx, then calls the function. If
the function calls var.set(), this will create a new _ContextData that is
stored in the threadstate, but it doesn't update the ctx. This is where the
current state and ctx go out of sync. Once the function returns or raises,
run() takes the _ContextData from the threadstate and stuffs it into ctx,
resolving the inconsistency. (It then also restores the previous
_ContextData that it had saved before any of this started.)

So all in all Context is mutable but the only time it is mutated is when
run() returns.

I think Yury's POV is that you rarely if ever want to introspect a Context
object that's not freshly obtained from copy_context(). I'm not sure if
that's really true; it means that introspecting the context stored in an
asyncio.Task may give incorrect results if it's the currently running task.

Should we declare it a bug? The fix would be complex given the current
implementation (either the PEP's pseudo-code or Yury's actual HAMT-based
implementation). I think it would involve keeping track of the current
Context in the threadstate rather than just the _ContextData, and updating
the Context object on each var.set() call. And this is something that Yury
wants to avoid, so that he can do more caching for var.get() (IIUC).

We could also add extra words to the PEP's spec for run() explaining this
temporary inconsistency.

I think changing the introspection method from Mapping to something custom
won't fix the basic issue (which is that there's a difference between the
Context and the _ContextData, and ContextVar actually only manipulates the
latter, always accessing it via the threadstate).

However there's another problem with the Mapping interface, which is: what
should it do with variables that are not set and have no default value?
Should they be considered to have a value equal to _NO_DEFAULT or
Token.MISSING? Or should they be left out of the keys altogether? The PEP
hand-waves on this issue (we didn't think of missing values when we made
the design). Should it be possible to introspect a Context that's not the
current context?


> (Note that I didn't say anything about HAMTs here, because that's
> orthogonal implementation detail. It would make perfect sense to have
> Context be an opaque wrapper around a regular dict; it would just give
> different performance trade-offs.)
>

Agreed, that's how the PEP pseudo-code does it.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180104/fa564d8d/attachment-0001.html>


More information about the Python-Dev mailing list