-
Notifications
You must be signed in to change notification settings - Fork 2k
fix get_access_token and lifespan_context returning None in background tasks - issue (#3095) #3121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
cristiangreco94
wants to merge
1
commit into
PrefectHQ:main
from
cristiangreco94:issue-3095-fix-depends-background-tasks
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
rg -n "DocketDependency\.docket" --type=py -C2Repository: jlowin/fastmcp
Length of output: 439
🏁 Script executed:
Repository: jlowin/fastmcp
Length of output: 6373
🏁 Script executed:
# Look at the full context of the file to understand the pattern head -n 20 src/fastmcp/server/dependencies.pyRepository: jlowin/fastmcp
Length of output: 768
🌐 Web query:
docket library Python Dependency ContextVar docket attribute documentation💡 Result:
In Docket (PyPI package
pydocket), “dependency injection” is done by declaring default parameter values likeCurrentDocket(),CurrentWorker(),CurrentExecution(),TaskKey(),TaskArgument(), orDepends(fn)in your task signature; Docket resolves those at runtime when executing the task. (chrisguidry.github.io)Those “current-*” dependencies are contextual (they’re meant to give you “the current docket/worker/execution for this running task” without threading it through every call). In Python, the standard way libraries implement safe per-request/per-task ambient context across async boundaries is
contextvars.ContextVar.ContextVarobjects have a read-only.nameattribute (so if a library creates something likeContextVar("docket"), its “docket attribute”/name is just"docket"). (docs.python.org)If you’re coming from FastMCP: FastMCP re-exports these DI concepts (and notes its DI is “powered by Docket”; task-related dependencies like
CurrentDocket/CurrentWorkerrequirefastmcp[tasks]). (gofastmcp.com)Citations:
🌐 Web query:
pydocket Dependency.docket ContextVar stable API💡 Result:
Dependency.docket(aContextVarhanging off the internalDependencybase class) should be treated as internal / not a stable public API. Docket is still marked “Beta” on PyPI, so internals are especially likely to change. [1]If you need “current docket/worker/execution” context in a supported way, use the documented built-in dependencies instead:
CurrentDocket()for the currentDocket[2]CurrentWorker()/CurrentExecution()for worker/execution context [2]TaskKey()/TaskArgument()for task identity/arguments (often used inside your own dependency functions) [2]And for custom DI, use the public helpers
Depends(...)(per-task) andShared(...)(worker-scoped), rather than reaching intoDependencyinternals. [3]Bottom line: don’t rely on
Dependency.docket/ContextVarattributes directly; preferCurrentDocket()(and friends) orDepends(...). [2][3]References:
[1] https://pypi.org/project/pydocket/
[2] https://chrisguidry.github.io/docket/dependencies/
[3] https://chrisguidry.github.io/docket/api-reference/
Replace
DocketDependency.docketwith the documented public API.The fallback to
DocketDependency.docket.get()relies on an internal, unstable API that is not part of Docket's public contract. According to Docket's documentation,Dependency.docketis an internal implementation detail and should not be used directly. Additionally, Docket is currently in Beta status on PyPI, making internal APIs especially prone to breaking changes.Use the documented public dependency
CurrentDocket()instead, which is the official way to access the current docket context in Docket-powered applications. If the fallback is needed to handle cases where_CurrentContexthasn't propagated, consider whetherCurrentDocket()would better address the underlying issue, or add a comment explaining why this fallback is necessary.