Skip to content

fix: Read file-like KVS values before upload and reject unencodable ones - #965

Open
vdusek wants to merge 10 commits into
masterfrom
worktree-fix-b7
Open

fix: Read file-like KVS values before upload and reject unencodable ones#965
vdusek wants to merge 10 commits into
masterfrom
worktree-fix-b7

Conversation

@vdusek

@vdusek vdusek commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Passing a file-like value (open(...), io.BytesIO(...)) to set_record — or to actor.start / actor.call / actor.validate_input / run.metamorph via run_input — crashed with a raw TypeError: 'BytesIO' object is not an instance of 'Sequence'. encode_key_value_store_record_value set a content type for io.IOBase but passed the object through unread, and impit's content= only accepts bytes-like bodies.

The encoder now reads file-like values into memory. Detection is duck-typed on a callable read, so non-io.IOBase wrappers work too. Binary maps to application/octet-stream, text to text/plain. read() is called with no arguments, so the value is consumed from its current position and the object is neither rewound nor closed.

Values it cannot turn into a body now raise TypeError instead of reaching the transport:

  • async file-likes (aiofiles, Starlette UploadFile) — read() returns a coroutine
  • read() returning neither bytes nor str, e.g. None from a non-blocking raw stream
  • a non-bytes-like value with a non-JSON content type, e.g. set_record('k', {'a': 1}, content_type='image/png')

Note: impit exposes no streaming content= API, so uploads are fully buffered rather than streamed.

Tests: set_record over BytesIO / StringIO / a duck-typed reader, sync and async, over both compression algorithms, plus encoder coverage of the three rejection paths.

✍️ Drafted by Claude Code

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Jul 20, 2026
@vdusek vdusek self-assigned this Jul 20, 2026
@github-actions github-actions Bot added this to the 145th sprint - Tooling team milestone Jul 20, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label Jul 20, 2026
@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.65%. Comparing base (77c4ea6) to head (8c081d6).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #965      +/-   ##
==========================================
- Coverage   94.66%   94.65%   -0.01%     
==========================================
  Files          58       58              
  Lines        5248     5259      +11     
==========================================
+ Hits         4968     4978      +10     
- Misses        280      281       +1     
Flag Coverage Δ
integration 92.45% <46.66%> (-0.12%) ⬇️
unit 84.04% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek
vdusek requested a review from Pijukatel July 21, 2026 09:19
@vdusek
vdusek marked this pull request as ready for review July 21, 2026 09:19
"""
value, content_type = encode_key_value_store_record_value(value, content_type=content_type)
# Encoding may read a file or serialize a large payload (blocking), so run it off the event loop.
value, content_type = await asyncio.to_thread(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

asyncio.to_thread is useful only to the branch that can do IO. It just adds overhead in other cases, or? So it improves the less frequent case at the cost of the main use case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, AFAIK the criterion is more whether the callee releases the GIL. If it does, the worker thread can work on the offloaded task, while the main thread, which runs the event loop, can run other asyncio tasks.

And here is the problem: json.dumps, which is called in encode_key_value_store_record_value, never releases the GIL. So yeah, let's remove it.

Thanks.

@vdusek
vdusek requested a review from Pijukatel July 30, 2026 07:36
@vdusek vdusek changed the title fix: Read file-like values before upload in the impit transport fix: Read file-like KVS values before upload and reject unencodable ones Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants