awslabs / multi-model-server

Multi Model Server is a tool for serving neural net models for inference
Apache License 2.0
995 stars 231 forks source link

Sys.stdout encoding should be utf-8 #883

Open mikeobr opened 4 years ago

mikeobr commented 4 years ago

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

vdantu commented 4 years ago

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 commented 4 years ago

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