fix: memory corruption and serialization issues (v0.5.21)#15
Merged
Conversation
- Remove duplicate benchmark_graphs/ entry - Change *.json to specific patterns to avoid ignoring config files - Remove Cargo.lock from ignore (should be tracked for apps) - Add common Python/IDE patterns Generated with AI Co-Authored-By: AI <ai@example.com>
- Check for model_dump() method first (handles dhi/Pydantic models) - Properly detect Response objects via body/status_code attributes - Handle binary response bodies without crashing This fixes Issue 1 where Response objects were being serialized as their string representation instead of actual content. Generated with AI Co-Authored-By: AI <ai@example.com>
- simd-json has stricter parsing than Python's json module - Fall back to Python's json.loads for edge cases with special chars - Fixes JSON parsing errors with exclamation marks and unicode This fixes Issue 3 where certain valid JSON was rejected by simd-json. Generated with AI Co-Authored-By: AI <ai@example.com>
- Set needs_body=True for async handlers with BaseModel parameters - Ensures proper model instantiation in Python context - Prevents kwargs being passed instead of model instance This fixes Issue 2 where async handlers received individual kwargs instead of the validated BaseModel instance. Generated with AI Co-Authored-By: AI <ai@example.com>
- Add defensive copy using bytes(bytearray(body)) in parse_json_body - Fix normalize_response to handle binary data without UnicodeDecodeError - Add base64 encoding fallback for binary content in make_serializable When running free-threaded Python (3.13t) with GPU frameworks like MLX, memory can be corrupted between operations due to concurrent access. The defensive copy ensures body bytes are in isolated memory before parsing. Generated with AI Co-Authored-By: AI <ai@example.com>
- Change from body_vec.as_slice() to PyBytes::new() for proper ownership - PyBytes copies data into Python-managed memory - Prevents corruption when Metal/MLX accesses shared memory regions The previous approach passed a Rust slice reference to Python, which could be corrupted by GPU memory operations in free-threaded mode. PyBytes::new() ensures the data is copied to Python's heap. Generated with AI Co-Authored-By: AI <ai@example.com>
- Test Response serialization and body extraction - Test async handler classification with BaseModel params - Test JSON parsing with special characters and unicode - Test empty body handling Generated with AI Co-Authored-By: AI <ai@example.com>
Generated with AI Co-Authored-By: AI <ai@example.com>
Generated with AI Co-Authored-By: AI <ai@example.com>
Release includes: - Fix Response object serialization - Fix async handler BaseModel parameter passing - Add simd-json fallback for special characters - Fix memory corruption with free-threaded Python + Metal/MLX - Clean up .gitignore Generated with AI Co-Authored-By: AI <ai@example.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR fixes several critical issues discovered when running TurboAPI with free-threaded Python 3.13t and Metal/MLX GPU frameworks:
Changes
Core Fixes
PyBytes::new()instead ofas_slice()for proper memory ownership in async handlersbytes(bytearray(body))copy in Python to prevent corruption with Metal/MLXjson.loadsfallback when simd-json failsResponse Handling
model_dump()and attribute checksHousekeeping
Test plan