Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make event stdout encoding more resilient to UTF-16 surrogate pairs #7918

Merged

Conversation

ryanpetrello
Copy link
Contributor

@ryanpetrello ryanpetrello commented Aug 17, 2020

Given that we're returning literally any bytestream that Ansible can muster, it probably makes sense to relax errors='strict' a bit to avoid 500 errors.

see: https://unicodebook.readthedocs.io/unicode_encodings.html#utf-16-surrogate-pairs

Run this adhoc command w/ -m shell:

printf '\uD83D\uDE00'

image

after (better than a 500 error):

image

@softwarefactory-project-zuul
Copy link
Contributor

Build failed.

@ryanpetrello
Copy link
Contributor Author

recheck

@ryanpetrello
Copy link
Contributor Author

There's also apparently libraries for actually handling these more gracefully: https://pypi.org/project/surrogates/

...but I also don't know that I want to add a dependency to deal with something so esoteric.

@softwarefactory-project-zuul
Copy link
Contributor

Build failed.

class SurrogateEncoder(encoders.JSONEncoder):

def encode(self, obj):
ret = super(SurrogateEncoder, self).encode(obj)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a little gross, but I couldn't find a less gross way to do this.

tl;dr there’s code further down the stack in DRF that ends up calling .encode() (and it blows up), so I’m calling it, seeing if it blows up, and then fixing the problem (so it won’t blow up later)

try:
ret.encode()
except UnicodeEncodeError as e:
if 'surrogates not allowed' in e.reason:
Copy link
Contributor

Choose a reason for hiding this comment

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

since ret is just a python str this assumption lgtm 👍

@softwarefactory-project-zuul
Copy link
Contributor

Build failed.

@ryanpetrello
Copy link
Contributor Author

recheck

@softwarefactory-project-zuul
Copy link
Contributor

Build failed.

@softwarefactory-project-zuul
Copy link
Contributor

Build failed.

@ryanpetrello
Copy link
Contributor Author

recheck

@softwarefactory-project-zuul
Copy link
Contributor

Build succeeded.

@softwarefactory-project-zuul
Copy link
Contributor

Build succeeded.

@softwarefactory-project-zuul
Copy link
Contributor

Build succeeded (gate pipeline).

@softwarefactory-project-zuul softwarefactory-project-zuul bot merged commit d452c1d into ansible:devel Aug 18, 2020
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