fix: prevent TogetherException repr crash on non-JSON-serializable headers#431
Open
giulio-leone wants to merge 2 commits intotogethercomputer:mainfrom
Open
fix: prevent TogetherException repr crash on non-JSON-serializable headers#431giulio-leone wants to merge 2 commits intotogethercomputer:mainfrom
giulio-leone wants to merge 2 commits intotogethercomputer:mainfrom
Conversation
6cd9a96 to
b9c7094
Compare
Author
|
Friendly ping — CI is green, tests pass, ready for review whenever convenient. Happy to address any feedback. Thanks! 🙏 |
Author
|
Friendly ping — this branch has been refreshed on the latest upstream base and all current review feedback has been addressed. It should be ready for review whenever you have a chance. Happy to make any further changes quickly. |
Author
|
Friendly ping — rebased on latest and ready for review. Happy to address any feedback! |
…aders When headers contain non-JSON-serializable objects (e.g. aiohttp's CIMultiDictProxy), `json.dumps()` in `__repr__` raises TypeError, making it impossible to print or log the exception. Add `default=str` to json.dumps so non-serializable objects fall back to their string representation instead of crashing. Fixes togethercomputer#108 Signed-off-by: Giulio Leone <6887247+giulio-leone@users.noreply.github.com>
- preserve mapping-like headers as structured JSON in __repr__ - validate the regression with a real CIMultiDictProxy repro Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
b9c7094 to
df6e869
Compare
Author
|
Refreshed this branch on the latest What changed in this follow-up:
Validation (double pass):
Runtime proof (double pass):
|
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.
Problem
TogetherException.__repr__usesjson.dumps()to serialize the exception's attributes (includingheaders) into a JSON string. Whenheaderscontains a non-JSON-serializable object like aiohttp'sCIMultiDictProxy, this raises aTypeError:This makes it impossible to
print(),repr(), or log the exception — the debugging tool itself crashes.Root Cause
json.dumps()in__repr__has no fallback for non-serializable types:Fix
Add
default=strtojson.dumps()so non-serializable objects fall back to their string representation:This is the standard Python pattern for graceful JSON serialization of arbitrary objects.
Tests
Added 7 unit tests covering:
CIMultiDictProxy-like headers (the reported bug)Noneheaders (default case)Verified Locally
All 7 new tests pass. All 181 existing unit tests continue to pass.
Fixes #108
Note
Low Risk
Low risk: change is confined to exception stringification and adds tests; behavior only affects how errors are represented/logged when headers are non-JSON-serializable.
Overview
Fixes
TogetherException.__repr__to never crash while logging/printing errors whenheaderscontains non-JSON-serializable types (e.g.,CIMultiDictProxy).Adds
_json_safe_headersplusjson.dumps(..., default=str)to safely stringify/normalize headers, and introduces a focused unit test suite covering dict/None/string headers,CIMultiDictProxy, nested non-serializable values, and subclass behavior.Written by Cursor Bugbot for commit df6e869. This will update automatically on new commits. Configure here.