From 40c5605973c71f7d83cc0e225d82c1544d55f2b1 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Wed, 22 Jun 2022 19:17:10 +0200 Subject: [PATCH] Adapt byte content for StreamingResponse Fixes https://github.com/galaxyproject/galaxy/issues/14156 ``` TypeError: 'bytes' object is not an iterator File "starlette/middleware/base.py", line 69, in __call__ await response(scope, receive, send) File "starlette/responses.py", line 260, in __call__ await wrap(partial(self.listen_for_disconnect, receive)) File "anyio/_backends/_asyncio.py", line 662, in __aexit__ raise exceptions[0] File "starlette/responses.py", line 256, in wrap await func() File "starlette/responses.py", line 245, in stream_response async for chunk in self.body_iterator: File "starlette/middleware/base.py", line 58, in body_stream raise app_exc File "starlette/middleware/base.py", line 36, in coro await self.app(scope, request.receive, send_stream.send) File "starlette/exceptions.py", line 93, in __call__ raise exc File "starlette/exceptions.py", line 82, in __call__ await self.app(scope, receive, sender) File "fastapi/middleware/asyncexitstack.py", line 21, in __call__ raise e File "fastapi/middleware/asyncexitstack.py", line 18, in __call__ await self.app(scope, receive, send) File "starlette/routing.py", line 670, in __call__ await route.handle(scope, receive, send) File "starlette/routing.py", line 266, in handle await self.app(scope, receive, send) File "starlette/routing.py", line 68, in app await response(scope, receive, send) File "starlette/responses.py", line 260, in __call__ await wrap(partial(self.listen_for_disconnect, receive)) File "anyio/_backends/_asyncio.py", line 662, in __aexit__ raise exceptions[0] File "starlette/responses.py", line 256, in wrap await func() File "starlette/responses.py", line 245, in stream_response async for chunk in self.body_iterator: File "starlette/concurrency.py", line 63, in iterate_in_threadpool yield await anyio.to_thread.run_sync(_next, iterator) File "anyio/to_thread.py", line 31, in run_sync return await get_asynclib().run_sync_in_worker_thread( File "anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread return await future File "anyio/_backends/_asyncio.py", line 867, in run result = context.run(func, *args) File "starlette/concurrency.py", line 53, in _next return next(iterator) ``` --- lib/galaxy/webapps/galaxy/api/datasets.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/webapps/galaxy/api/datasets.py b/lib/galaxy/webapps/galaxy/api/datasets.py index c78a12a0e0e6..cd4a8e6c88d6 100644 --- a/lib/galaxy/webapps/galaxy/api/datasets.py +++ b/lib/galaxy/webapps/galaxy/api/datasets.py @@ -2,7 +2,10 @@ API operations on the contents of a history dataset. """ import logging -from io import IOBase +from io import ( + BytesIO, + IOBase, +) from typing import ( Any, cast, @@ -259,6 +262,8 @@ def display( return FileResponse(file_name, headers=headers) elif isinstance(display_data, ZipstreamWrapper): return StreamingResponse(display_data.response(), headers=headers) + elif isinstance(display_data, bytes): + return StreamingResponse(BytesIO(display_data), headers=headers) return StreamingResponse(display_data, headers=headers) @router.get(