Skip to content
This repository was archived by the owner on May 6, 2026. It is now read-only.
This repository was archived by the owner on May 6, 2026. It is now read-only.

ContextError when using @ndb.transactional() #213

Description

@timlichen

Environment details

ndb version: 0.1.0

Code example

@staticmethod@ndb.transactional()deftest_func():
ModelName.query().get()

Stack trace

Traceback (most recent call last):
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/timchen/Projects/project/utils/custom_middleware.py", line 12, in __call__
return self.app(environ, start_response)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 119, in __exit__
next(self.gen)
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/client.py", line 176, in context
context.eventloop.run()
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 355, in run
if not self.run1():
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 344, in run1
delay = self.run0()
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 313, in run0
if self._run_current() or self.run_idle():
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 278, in run_idle
result = callback(*args, **kwargs)
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_batch.py", line 62, in idle
batch.idle_callback()
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_cache.py", line 67, in idle_callback
cache_call = self.make_call()
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/_cache.py", line 261, in make_call
cache = context_module.get_context().global_cache
File "/Users/timchen/.virtualenvs/pyenv3/lib/python3.7/site-packages/google/cloud/ndb/context.py", line 65, in get_context
raise exceptions.ContextError()
google.cloud.ndb.exceptions.ContextError: No current context. NDB calls must be made in context established by google.cloud.ndb.Client.context.

We are running this in a Flask application with custom middleware to instantiate client context. Calls made without transactional decorator work properly. We've tried decorating class, static, instance and standalone functions as well as closures with no success all resulting in the same traceback.

Metadata

Metadata

Assignees

Labels

🚨This issue needs some love.priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions