Skip to content

fix(serial-console): decode binary WebSocket frames as UTF-8#9795

Open
cjp256 wants to merge 2 commits intoAzure:mainfrom
cjp256:fix-decoding
Open

fix(serial-console): decode binary WebSocket frames as UTF-8#9795
cjp256 wants to merge 2 commits intoAzure:mainfrom
cjp256:fix-decoding

Conversation

@cjp256
Copy link
Copy Markdown

@cjp256 cjp256 commented Apr 17, 2026

The on_message callback passed WebSocket messages directly to PC.print, which forwards to Python's built-in print(). Per RFC 6455, the websocket-client library delivers text-opcode frames as str and binary-opcode frames as bytes. When the server sends a binary frame, print() renders the bytes via repr() — emitting literals like b'\x1b[0;32m OK \xe2\x80\xa6\r\r\n' instead of interpreting ANSI escapes and UTF-8.

The Azure Serial Console stream is semantically text (a guest TTY), but the client shouldn't rely on the server's choice of opcode. Decode bytes/bytearray messages as UTF-8 (with errors="replace") before printing so the terminal renders colors, box-drawing characters, and line endings correctly regardless of which opcode the server uses.


This checklist is used to make sure that common guidelines for a pull request are followed.

Related command

az serial-console connect -n $name -g $rg

General Guidelines

  • Have you run azdev style <YOUR_EXT> locally? (pip install azdev required)
  • Have you run python scripts/ci/test_index.py -q locally? (pip install wheel==0.30.0 required)
  • My extension version conforms to the Extension version schema

About Extension Publish

There is a pipeline to automatically build, upload and publish extension wheels.
Once your pull request is merged into main branch, a new pull request will be created to update src/index.json automatically.
You only need to update the version information in file setup.py and historical information in file HISTORY.rst in your PR but do not modify src/index.json.

Example

Bad screenshot:

console-broke

Fixed:

console-fixed

The `on_message` callback passed WebSocket messages directly to `PC.print`,
which forwards to Python's built-in `print()`. Per RFC 6455, the
`websocket-client` library delivers text-opcode frames as `str` and
binary-opcode frames as `bytes`. When the server sends a binary frame,
`print()` renders the `bytes` via `repr()` — emitting literals like
`b'\x1b[0;32m  OK  \xe2\x80\xa6\r\r\n'` instead of interpreting ANSI
escapes and UTF-8.

The Azure Serial Console stream is semantically text (a guest TTY), but
the client shouldn't rely on the server's choice of opcode. Decode
`bytes`/`bytearray` messages as UTF-8 (with `errors="replace"`) before
printing so the terminal renders colors, box-drawing characters, and
line endings correctly regardless of which opcode the server uses.

Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
Copilot AI review requested due to automatic review settings April 17, 2026 16:32
@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd Bot commented Apr 17, 2026

️✔️Azure CLI Extensions Breaking Change Test
️✔️Non Breaking Changes

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented Apr 17, 2026

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link
Copy Markdown
Contributor

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves az serial-console connect output rendering by ensuring WebSocket binary frames are decoded to UTF-8 before being printed, so ANSI escapes and multi-byte characters display correctly even when the server uses a binary opcode.

Changes:

  • Bump the serial-console extension version to 1.0.0b4 and add a matching HISTORY entry.
  • Add _decode_ws_message and use it in the WebSocket on_message handler to decode bytes/bytearray as UTF-8 with errors="replace".
  • Add unit tests covering decoding behavior for str, bytes, bytearray, ANSI escapes, UTF-8 box-drawing characters, and invalid UTF-8 replacement.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/serial-console/setup.py Version bump to publish the fix as 1.0.0b4.
src/serial-console/azext_serialconsole/custom.py Decode binary WebSocket payloads to UTF-8 before printing to the terminal.
src/serial-console/azext_serialconsole/tests/latest/test_decode_ws_message.py Adds unit coverage for the decoding helper.
src/serial-console/HISTORY.rst Documents the rendering fix in release notes.

Comment thread src/serial-console/HISTORY.rst Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented Apr 17, 2026

Please fix CI issues

@necusjz
Copy link
Copy Markdown
Member

necusjz commented Apr 20, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants