nicer error messages in run.io_bound when attempting to access app.storage.user #2801
petergaultney
started this conversation in
Ideas / Feature Requests
Replies: 2 comments
-
Sounds good. Would you like to try a pull request? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yes, improving the error message would be great. And it doesn't interfere with other plans we might have in mind about the contextvar and the request object. Just for reference, here is a related issue scheduled for version 2.0.0: #2520 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've noticed that
nicegui
doesn't allow accessing the built-in app user storage when running something in theio_bound
thread pool. I am pretty sure this is because that thread pool has no way of accessing the actual request context - which makes lots of sense, since that's embedded in a contextvar that is set only when the actual request is happening.Unfortunately, the error received is a characteristically large async stack trace, which declares (after about two screens worth of stack frames, part of which I've posted below) that the issue is not having a set an app secret in
ui.run
:RuntimeError: app.storage.user needs a storage_secret passed in ui.run()
I think it's unlikely this is the most precise way to describe the underlying error - more likely, the error is what I said above - it's just not possible to get access to that request context in a meaningful, clean way - anything you want to get from user storage should be synchronously retrieved from it before creating your async closure that you pass to
run.io_bound
. In my specific case, I have set an app secret, and just avoiding calling this from within a separate thread fixes my issue.I wonder, first of all, if there might be a clean way of detecting the case the error message currently indicates, where no storagesecret is set, and therefore no RequestTrackingMiddleware was added. As far as I can tell, you don't actually need a storage_secret for there to be a RequestTrackingMiddleware - that's only actually required for the SessionMiddleware. Possibly we should be deferring this message until after we find a request but determine that the request has no
session
(and/or session id)?If we could do the above, then maybe we could, in cases where no Request object exists at all, raise an error that says something more like "No request is active - are you sure you're accessing the user storage from within an active request? Do not attempt to access this from within
io_bound
,cpu_bound
, or other runtime contexts that are not part of an active request"This is obviously just a minor question about getting error messages to point people in the right direction; nothing is broken in terms of intended functionality.
Thank you again for a remarkably usable Python UI library.
Beta Was this translation helpful? Give feedback.
All reactions