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.

No current context #128

Description

@kercos

Hi,
I'm not sure if this is a bug but it's not clear to me how to properly handle client.context() when handling multiple threads.

Steps to reproduce

  1. See code below

Code example

fromgoogle.cloudimportdatastorefromgoogle.cloudimportndbclient=ndb.Client()
classTest(ndb.Model):
name=ndb.StringProperty()
deftest(input_name): print('Adding {}'.format(input_name))
withclient.context():
t=Test(
name=input_name, ) t.put() print('Added {}'.format(input_name))
if__name__=="__main__":
importthreadingthreading.Thread(target=test, args=['John']).start()
threading.Thread(target=test, args=['Bob']).start()

Stack trace

Adding John
Adding Bob
Added John
Exception in thread Thread-2:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "ndb_test.py", line 16, in test
t.put()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/_options.py", line 93, in wrapper
return wrapped(*pass_args, _options=_options, **kwargs)
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4758, in _put
return self._put_async(_options=_options).result()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 190, in result
self.check_success()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 134, in check_success
self.wait()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 126, in wait
_eventloop.run1()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 411, in run1
loop.run1()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 344, in run1
delay = self.run0()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 333, in run0
callback(rpc)
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 323, in done_callback
_eventloop.call_soon(self._advance_tasklet, yielded.result())
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 380, in call_soon
loop = get_event_loop()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/lib/python3.7/site-packages/google/cloud/ndb/_eventloop.py", line 368, in get_event_loop
context = context_module.get_context()
File "/Users/fedja/Code/Bots/HistoricTrentoBotPy3/env/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.

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