Skip to content

fix(testing): keep MemoryBackend active in async decorated functions - #582

Merged
leandrodamascena merged 5 commits into
ferrumio:mainfrom
jpcaicedok:fix/memory-backend-async-decorator
Nov 6, 2026
Merged

leandrodamascena merged 5 commits into
ferrumio:mainfrom
jpcaicedok:fix/memory-backend-async-decorator

Conversation

@jpcaicedok

Copy link
Copy Markdown
Contributor

Summary

@MemoryBackend() wrapped every function in a sync wrapper. For an async def, the wrapper returned the coroutine and left the backend context before the coroutine ran. So the model failed with ValueError: No client configured.

Closes #560

Changes

  • MemoryBackend.__call__() now uses an async wrapper for coroutine functions. The wrapper awaits the function inside the context, so the backend stays active until the function ends. Sync functions use the same wrapper as before.
  • Tests in tests/unit/test_memory_backend.py for async success, an await inside the function, cleanup after an exception, restoring a previous default client, the example from the issue with asyncio.run(), and the existing sync use.
  • A short note in docs/guides/testing.md, and an example in the class docstring.

Behavior to check

  • The backend now starts when the coroutine starts to run, not when the function is called. A test pins this.
  • Async generator functions keep the old behavior. They are not coroutine functions, and the issue does not cover them.

Tests

Built with maturin develop on Python 3.13.

  • Six of the new decorator tests failed before the fix: the ones that use the model failed with the error from the issue. The exception cleanup test and the existing sync test passed before and after it.
  • pytest tests/unit/test_memory_backend.py -q: 55 passed.
  • pytest tests/unit -q: 1211 passed, 2 skipped.
  • ruff check and ruff format --check pass on the changed Python files.
  • ty check python/pydynox/testing/memory.py reports one diagnostic, on a @contextmanager return type that this change does not touch.
  • I did not run cargo test or the integration tests, because no Rust code changed and the integration tests need LocalStack.

Assignment note: I commented on the issue first, as it asks, but I did not wait for the assignment before opening this PR. Please close it if someone else should take the issue.


Disclosure: Claude, an AI agent, wrote this change and ran the checks above from the account of the person opening this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR

jpcaicedok and others added 3 commits November 3, 2026 17:49
The sync wrapper returned the coroutine and left the context before the coroutine ran. Use an async wrapper for coroutine functions, so the backend stays active until the function ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR
Cover async success, an await inside the function, cleanup after an exception, restoring a previous client, and the sync decorator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G1VdxZmWX7QvJykW7HN8rR

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Juan Pablo! Looks good to me. I confirmed that the six regression cases fail before the fix and pass here. Cancellation, repeated calls, and nested decorators also restore the previous client correctly, and the sync behavior is preserved.

All 1,213 unit tests and 212 examples passed locally, along with the documentation build, Ruff, and ty. I’ll approve the pending CI runs before we merge. Thanks for picking this up!

@sonarqubecloud

sonarqubecloud Bot commented Nov 6, 2026

Copy link
Copy Markdown

@leandrodamascena
leandrodamascena merged commit 82cd082 into ferrumio:main Nov 6, 2026
4 checks passed
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MemoryBackend decorator exits before an async function runs

2 participants