FileResponse with io.BytesIO returns 500 error

I have a solution that works in development (VScode, Django v5.1, Python 3.12.5) returning a pdf file created in a buffer using io.BytesIO then return a page response using FileResponse. The following is part of the code that works in the development environment:

        buffer = io.BytesIO()
        myCanvas = Canvas(buffer, pagesize=letter)
        {... more code here to produce the pdf content ... }
        myCanvas.showPage()
        myCanvas.save()
        buffer.seek(0)
        return FileResponse(buffer, as_attachment=False, filename="filename.pdf")

Now when I deploy (Ubuntu 22.04 , OpenLiteSpeed, Django 5.1, Python 3.10.12) I get a ‘500 Internal Error’. Looks like all of the generation code works but when returning the resulting buffer in FileResponse the error occurs. The only error I see in the logs is in the lsws/logs/stderr.log which says ‘io.UnsupportedOperation: fileno’. Maybe this is a OpenLiteSpeed issue and not a Django issue but any help would be appreciated.

I’ve got no suggestions, but some ideas for further investigation.

Doing some searches on this error leads me to believe that the issue isn’t no much that it’s LightSpeed, but that it’s possibly a difference between runserver running from a shell and the server process being initiated from something else. (Systemd? Init? Supervisord? runit?)

Can you post the complete traceback generated for this?

What pdf library are you using?

Do you have any logging configured in your application?

Did you check syslog for errors related to this?

Did you look for other log files, with perhaps seemingly unrelated file names?

You’re also using two different versions of Python. (You’re showing 3.12 in development but 3.10 deployed. Is there any possible compatibility issue with the versions of any libraries you’re using?)

1 Like

Thanks for ideas … based on someone else’s post the issue is how the response stream is handled by some underlining process. Running under VScode a stream made by io.BytesIO seems to by handled fine. When running under OpenLiteSpeed, there is some process that expects the stream to be a file handle and will throw an exception if it is not. Once I changed my code to create a temp pdf file and pass that it works perfectly. Not sure where the issue is but the ‘fix’ is easy at least for my application. Thanks again for your help.