diff --git a/hsds/chunk_sn.py b/hsds/chunk_sn.py index ee77b1a2..ea9028a9 100755 --- a/hsds/chunk_sn.py +++ b/hsds/chunk_sn.py @@ -16,6 +16,7 @@ import base64 import math +import traceback import numpy as np from json import JSONDecodeError @@ -1071,6 +1072,7 @@ async def GET_Value(request): resp_json = {"status": 200} # will over-write if there's a problem # write response + resp = None try: resp = StreamResponse() if config.get("http_compression"): @@ -1224,11 +1226,13 @@ async def GET_Value(request): await resp.write_eof() except Exception as e: log.error(f"{type(e)} Exception during data write: {e}") - import traceback - - tb = traceback.format_exc() - print("traceback:", tb) - raise HTTPInternalServerError() + log.error(f"traceback: {traceback.format_exc()}") + if resp is not None and resp.prepared: + # headers are already on the wire - raising here would inject a new + # status line into the body and corrupt the chunked stream + await resp.write_eof() + else: + raise HTTPInternalServerError() return resp @@ -1473,11 +1477,8 @@ async def POST_Value(request): resp_body = resp_body.encode("utf-8") await resp.write(resp_body) except Exception as e: - log.error(f"{type(e)} Exception during response write") - import traceback - - tb = traceback.format_exc() - print("traceback:", tb) + log.error(f"{type(e)} Exception during response write: {e}") + log.error(f"traceback: {traceback.format_exc()}") # finalize response await resp.write_eof()