Skip to content

[Container App] Fix az containerapp exec crash on non-ASCII (emoji) terminal output#33476

Open
after2842 wants to merge 1 commit into
Azure:devfrom
after2842:fix/containerapp-exec-unicode-encode
Open

[Container App] Fix az containerapp exec crash on non-ASCII (emoji) terminal output#33476
after2842 wants to merge 1 commit into
Azure:devfrom
after2842:fix/containerapp-exec-unicode-encode

Conversation

@after2842
Copy link
Copy Markdown

Related command
az containerapp exec

Description
az containerapp exec crashes with UnicodeEncodeError whenever a container's
stdout/stderr contains characters the local terminal's codec cannot encode
(emoji, non-Latin scripts). On a cp1252 Windows console, e.g. the lobster emoji
U+1F99E:
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f99e' ...
File "_ssh_utils.py", line 108, in _decode_and_output_to_terminal

Root cause: in _decode_and_output_to_terminal, the container bytes decode to a
str correctly (UTF-8), but print() then re-encodes that str to the
console's encoding (cp1252), which raises UnicodeEncodeError. The surrounding
try/except only caught UnicodeDecodeError, so the encode-side failure was
uncaught and tore down the exec session.

Fix: route terminal output through a new _write_to_terminal() helper that
prints natively on capable (UTF-8) terminals, and on UnicodeEncodeError falls
back to encoding with errors="backslashreplace" written to sys.stdout.buffer
— so unencodable characters degrade gracefully instead of crashing. Also
separates the decode and print steps and corrects a misleading
"Failed to encode" → "Failed to decode" log message.

Testing Guide

  • New unit tests: azdev test test_ssh_utils (covers the fallback, the print
    fast path, and end-to-end decode+output of emoji without disconnecting).
  • Manual repro: on a cp1252 console (set PYTHONLEGACYWINDOWSSTDIO=1), run a
    container command that prints an emoji
    (az containerapp exec ... --command "printf '\xf0\x9f\xa6\x9e\n'"):
    crashes before this change, prints the character (or its \U0001f99e escape)
    and keeps the session alive after.

History Notes
[Container App] Fix az containerapp exec crash on non-ASCII (emoji) terminal output

Copilot AI review requested due to automatic review settings May 31, 2026 07:50
@azure-client-tools-bot-prd
Copy link
Copy Markdown

Validation for Azure CLI Full Test Starting...

Thanks for your contribution!

@azure-client-tools-bot-prd
Copy link
Copy Markdown

Validation for Breaking Change Starting...

Thanks for your contribution!

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented May 31, 2026

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

@github-actions
Copy link
Copy Markdown

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

Note

Copilot was unable to run its full agentic suite in this review.

Adds resilient terminal output handling for the Container Apps SSH exec session so that characters not representable in the host console's encoding (e.g., emoji on a cp1252 Windows terminal) don't crash the session.

Changes:

  • Introduced _write_to_terminal helper that prints normally when possible and falls back to encoding with backslashreplace via sys.stdout.buffer when the console codec cannot encode the text.
  • Refactored _decode_and_output_to_terminal to decode first, then route output through the new helper; also fixed a misleading log message ("encode" -> "decode").
  • Added unit tests covering the fast path, the unencodable-character fallback, and end-to-end decode+output behavior.

Reviewed changes

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

File Description
src/azure-cli/azure/cli/command_modules/containerapp/_ssh_utils.py New _write_to_terminal fallback writer; _decode_and_output_to_terminal reworked to use it and corrected log wording.
src/azure-cli/azure/cli/command_modules/containerapp/tests/latest/test_ssh_utils.py New tests verifying fast path, encoding fallback, and that emoji output does not disconnect.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +132 to 133
decoded = response[2:].decode(encoding)
break
@after2842
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

4 participants