Skip to content

Preserve buffered request/response body on protocol error#8231

Open
Halle-Astra wants to merge 2 commits into
mitmproxy:mainfrom
Halle-Astra:fix/preserve-buffered-body-on-protocol-error
Open

Preserve buffered request/response body on protocol error#8231
Halle-Astra wants to merge 2 commits into
mitmproxy:mainfrom
Halle-Astra:fix/preserve-buffered-body-on-protocol-error

Conversation

@Halle-Astra

Copy link
Copy Markdown

Description

When a connection is interrupted mid-stream, the body data already
buffered in request_body_buf / response_body_buf was discarded,
causing the Web UI to show 'Content is missing.' instead of the
partial content that was received.

This fix saves any buffered data to flow.request.data.content /
flow.response.data.content in handle_protocol_error before the
error hook is triggered, so users can still inspect what was
received before the connection broke.

Checklist

  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.

  When a connection is interrupted mid-stream, the body data already
  buffered in request_body_buf / response_body_buf was discarded,
  causing the Web UI to show 'Content is missing.' instead of the
  partial content that was received.

  This fix saves any buffered data to flow.request.data.content /
  flow.response.data.content in handle_protocol_error before the
  error hook is triggered, so users can still inspect what was
  received before the connection broke.
@mhils

mhils commented May 15, 2026

Copy link
Copy Markdown
Member

I'm not sure if I like this change. None clearly signals that we don't have the full body. Now we have a partial body but no good signalling that it's just partial.

@Halle-Astra

Copy link
Copy Markdown
Author

I'm not sure if I like this change. None clearly signals that we don't have the full body. Now we have a partial body but no good signalling that it's just partial.

In my practice, I need the buffered stream to analyze when my LLM agent disconnected request (shown in mitmweb as 'client disconnected'). Such as, something like timeout when agent received partial stream but think it's too long to receive full stream content and raise the timeout Error.
In this scence, I hope to know what content LLM wants to produce and makes it so long.

But current behaviour is discarding the buffered stream content. So I cannot analyze it even with a hook script method.

Also, I know in most practice, the response of failed request is None or meaningless(maybe partial stream but nobody care).

current performance, though this post is disconnected by client, but still show partial stream
image
image
image

@Halle-Astra

Copy link
Copy Markdown
Author

I'm not sure if I like this change. None clearly signals that we don't have the full body. Now we have a partial body but no good signalling that it's just partial.

Thanks for your reply and good point. I've updated the PR — when partial body data is saved, the error message now says e.g. "Client disconnected. (partial body saved)" so it's clear the body is incomplete. How about this?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants