Skip to content

[CP v2.16] Fix WebSocket disconnect during large manual log file uploads (#1062) - #344

Merged
rquidute merged 1 commit into
v2.16-developfrom
cp/1062-manual-log-upload-websocket-error-v2.16
Jul 22, 2026
Merged

[CP v2.16] Fix WebSocket disconnect during large manual log file uploads (#1062)#344
rquidute merged 1 commit into
v2.16-developfrom
cp/1062-manual-log-upload-websocket-error-v2.16

Conversation

@rquidute

Copy link
Copy Markdown
Contributor

Summary

Cherry-pick of the merged fix for #1062 (WebSocket disconnect during large manual log file uploads) onto v2.16-develop, for the Matter v1.7 TE2 release line.

Original PR: #343 (merged to v2.15.1-develop as cb44885).

Contents

Single cherry-picked commit (squash-merge of #343, including review fixes for CRLF handling and the mypy BinaryIO annotation):

  • Batches uploaded log lines into chunks of 500 before calling logger.info(), instead of one call per line, avoiding the event-loop stall that previously tripped the WebSocket's keepalive ping/pong during large uploads.
  • Collapses the invalid-UTF-8 warning to a single message per upload.
  • Adds app.uvicorn_worker.ExtendedTimeoutUvicornWorker (raises ws_ping_timeout to 60s) and wires it up as the default production worker class in gunicorn/start.sh.
  • Unit tests covering chunked logging, CRLF handling, invalid UTF-8 handling, and unsupported content-type rejection.

Testing

Cherry-picked cleanly with no conflicts. Re-ran flake8/mypy/black/isort against the v2.16-develop base for all touched files — all clean.

…#343)

* Fix WebSocket disconnect during large manual log file uploads

Uploading a manual test log with hundreds of thousands of lines
(e.g. TC-CADMIN-1.17) called logger.info() once per line. Each call
triggered a broadcast over the same main WebSocket used by the CLI
and a synchronous DB commit, stalling the event loop long enough for
the WebSocket's ping/pong keepalive to time out mid-upload. The
connection was dropped with ConnectionClosedError, and the next
prompt-response send failed with 'Unexpected error uploading file'
even though the upload itself had already succeeded (issue #1062).

- Batch uploaded log lines into chunks of 500 before calling
  logger.info(), instead of one call per line, so a large file
  produces a handful of log/broadcast/DB-commit operations instead
  of hundreds of thousands.
- Collapse the invalid-UTF-8 warning to a single message per upload
  instead of one per bad line.
- Add app.uvicorn_worker.ExtendedTimeoutUvicornWorker, which raises
  ws_ping_timeout to 60s (matching the value already used by the
  dev-only start-reload.sh script), and wire it up as the default
  worker class in gunicorn/start.sh so production tolerates the same
  event-loop stalls as local dev.
- Add unit tests covering chunked logging, invalid UTF-8 handling,
  and unsupported content-type rejection.

* Address review: strip CRLF instead of LF only when batching log lines

.rstrip("\n") left a trailing \r on every line of a Windows-style
(CRLF) uploaded log, since the file is read in binary mode and split
only on \n. That stray \r would end up embedded in the joined chunk,
showing up as ^M characters or unexpected double newlines wherever
the chunk is logged or displayed.

Use .rstrip("\r\n") so both Unix and Windows line endings are
handled correctly. Add a regression test with a CRLF-terminated
upload.

* Fix CI mypy error: annotate FakeUploadFile.file as BinaryIO

mypy's structural check against the UploadFile protocol requires
file: BinaryIO exactly; a bare BytesIO() assignment left the
attribute inferred as BytesIO, which mypy treats as incompatible
with the protocol's BinaryIO annotation despite BytesIO satisfying
it at runtime. Annotate the attribute explicitly so
handle_uploaded_file(FakeUploadFile(...)) type-checks.
@mergify

mergify Bot commented Jul 22, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request addresses websocket connection drops during large manual log uploads (GitHub issue #1062). It introduces log chunking (500 lines per chunk) in handle_uploaded_file to prevent event loop starvation, optimizes UTF-8 warning frequency, and implements a custom ExtendedTimeoutUvicornWorker with an increased ws_ping_timeout of 60 seconds. Comprehensive unit tests have also been added to verify these changes. There are no review comments, and I have no feedback to provide.

@rquidute
rquidute requested a review from antonio-amjr July 22, 2026 20:01
@rquidute
rquidute merged commit 5772bcc into v2.16-develop Jul 22, 2026
8 of 9 checks passed
@rquidute
rquidute deleted the cp/1062-manual-log-upload-websocket-error-v2.16 branch July 22, 2026 20:38
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