feat: add shared payment runtime - #184
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6a4c272614
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| try: | ||
| cleanup(self._bridge.cancel_pending) | ||
| if self._method_stack is not None: | ||
| cleanup(lambda: self._bridge.run(self._teardown_methods())) |
There was a problem hiding this comment.
Keep async context managers in their entering task context
When a factory returns an async context manager that uses task-local state, this submits _teardown_methods() as a new task even though __aenter__ ran in the earlier _initialize_methods() task. For example, an @asynccontextmanager that sets a ContextVar and resets its token in finally consistently raises ValueError: Token was created in a different Context during close(), potentially interrupting cleanup. Keep the exit stack alive in one supervisor task that performs both entry and exit rather than resubmitting teardown separately.
AGENTS.md reference: AGENTS.md:L68-L69
Useful? React with 👍 / 👎.
5d60b7d to
b5a4293
Compare
b5a4293 to
07d5dc8
Compare
07d5dc8 to
42cfb85
Compare
a9005de to
4aae448
Compare
4aae448 to
8763962
Compare
Summary
PaymentRuntimewith one AnyIO-owned asyncio loopLifecycle contract
methods=are borrowed and must be loop-independentmethod_factories=initialize once and async context managers exit on the owned loopWhy this shape
AnyIO owns the portal, loop thread, task submission, and loop cleanup. pympp only tracks runtime state, active root calls, and method teardown. The generic coroutine runner and detached-task lease model are intentionally private/out of scope.
This simplification removes 412 net lines from the previous draft. It also declares the tested AnyIO range (
>=4.12,<5).Scope
Runtime foundation only. Existing HTTP and MCP clients are unchanged; HTTPX instrumentation, origin policy, retry/outcome tracking, and MCP integration come in later PRs.
Validation
Managed as a stacked change with GitHub Stacks.