multi-model-server icon indicating copy to clipboard operation
multi-model-server copied to clipboard

Sys.stdout encoding should be utf-8

Open mikeobr opened this issue 5 years ago • 2 comments

Problem use case: Given an MMS custom service prints some input for logging purposes, when the service receives json with unicode characters, the service errors out.

Steps to reproduce"

  1. Create a dummy MMS service with a handler like:

    def handle(data, context): if data is None: return print(data[0]["body"]) return ["Finished successfully."]

  2. Send requests with ascii characters and it succeeds, but with unicode the service returns an error.

Test script

import requests

url="http://localhost:8080/predictions/classify"
headers={"Content-Type": "application/json"}
ascii_request={"test": "hi"}
unicode_request={"test": "你好"}
print(requests.post(url,json=ascii_request, headers=headers).content)
print(requests.post(url,json=unicode_request, headers=headers).content)

Results

python test.py
b'Finished successfully.'
b'{\n  "code": 503,\n  "type": "InternalServerException",\n  "message": "Prediction failed"\n}\n'
  1. If you remove the print line from the service, both requests succeed.

Workaround To work around this behavior, services need to override the sys.stdout encoding.

import sys
sys.stdout.reconfigure(encoding='utf-8')

Reference to topic in different issue: https://github.com/awslabs/multi-model-server/issues/850#issuecomment-566678665

mikeobr avatar Dec 17 '19 20:12 mikeobr

I am not clear on this. Is this an issue with the handler code or do you suspect it being a MMS issue? The error suggests that its a handler issue. Error seems to be coming as a part of exception thrown by the handler Code here

vdantu avatar Jan 08 '20 20:01 vdantu

I went through the #850, I think. I understand the issue a bit better now. This looks like a good solution as well. This should probably be picked up as a part of #845

vdantu avatar Jan 08 '20 20:01 vdantu