Skip to content

wsgi.errors should be a text stream, not a binary stream #529

Closed

Description

@awoimbee

Causing issues with Flask's logging handler (it writes to wsgi.errors)

--- Logging error ---
Traceback (most recent call last):
  File "/home/arthur/.local/share/uv/python/cpython-3.13.0-linux-x86_64-gnu/lib/python3.13/logging/__init__.py", line 1153, in emit
    stream.write(msg + self.terminator)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Call stack:
[...]
  File "/home/arthur/Documents/work/myapp/myapp/app_logging.py", line 37, in _log_request_result
    logger.info("Request complete", extra=data)
Message: 'Request complete'
Arguments: ()

Offending line:

"wsgi.errors": BytesIO(),

WSGI spec:

An output stream (file-like object) to which error output can be written, [...]. This should be a “text mode” stream; i.e., applications should use "\n" as a line ending [...]
(On platforms where the str type is unicode, the error stream should accept and log arbitrary unicode without raising an error; it is allowed, however, to substitute characters that cannot be rendered in the stream’s encoding.)


Why wasn't this raised sooner ?
Flask has been using wsgi.errors for >8 years. This repo has been using BytesIO since WsgiToAsgi exists (8 years).


Workaround:

import sys
import io

from asgiref.wsgi import WsgiToAsgi, WsgiToAsgiInstance


class WsgiToAsgiInstanceFixed(WsgiToAsgiInstance):
    def build_environ(self, scope, body):
        environ = super().build_environ(scope, body)
        # environ["wsgi.errors"] = io.StringIO() # Doesn't print anything ?
        environ["wsgi.errors"] = sys.stderr
        return environ

class WsgiToAsgiFixed(WsgiToAsgi):
    async def __call__(self, scope, receive, send):
        await WsgiToAsgiInstanceFixed(self.wsgi_application)(scope, receive, send)

Activity

  1. andrewgodwin commented on Sep 15, 2025

    @andrewgodwin
    Member

    Fascinating, I don't know how this went unnoticed all this time - I presume nobody ever tested it with Flask (that plus the WSGI-to-ASGI adapter code is not used very often I think).

    Definitely needs to be fixed - this might just be a case where we can land a direct PR to change it.

  2. andrewgodwin commented on Sep 15, 2025

    @andrewgodwin
    Member

    Added a direct fix for this in #530, but that still does what the old code did - absorbs the errors and doesn't do anything with them.

    Maybe we should point it at sys.stderr instead so it at least goes somewhere?

  3. andrewgodwin commented on Sep 15, 2025

    @andrewgodwin
    Member

    Ah, I now see this is what you did in your workaround. I think I'll just switch the PR to that.

  4. added a commit that references this issue on Sep 16, 2025
    29b108b
  5. carltongibson commented on Sep 17, 2025

    @carltongibson
    Member

    Thanks both. I think this is worth a point release — @andrewgodwin I'll get to that shortly if you don't first.

  6. andrewgodwin commented on Sep 18, 2025

    @andrewgodwin
    Member

    Sounds good - I am travelling the next three weeks so I suspect you'll get there first!

  7. carltongibson commented on Sep 23, 2025

    @carltongibson
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions