Hello. I have built a Flask application based on a python project of mine. The application basically performs a few REST API queries, computes logic on the results and then writes them out to an html file. It cycles through this for several queries depending on the data available for the user.
As I wrote this originally without Flask in mind, I simply present the html file to the user after the processing has completed. This is working most of the time.
However I have a few strange instances from some users who tested the app for me. 2 users I see that their html file wrote out with a size of 0KB. My code basically looks like this:
def initialize_report(obj_report, user):
''' Create opening HTML/CSS for report '''
f = obj_report
html = f"""
<HTML>
<title>
CCSSR - {user}
</title>
"""
write_line(f, html)
filename = f"{user.name}_{date_time}.html"
obj_report = open(filename, 'a+', encoding='utf-8')
initialize_report(obj_report, user.name)
So, you can basically see that opening the report is immediately followed by writing a few lines of html to the file. This means the script must have been able to create the file, but was unable to append data into it.
I realize I don't have a lot of error handling here (mostly because I am not yet proficient enough with Flask to pass these errors back to the user). However I need to know how this is possible. I do have several sections in my error.log that have:
OSError: write error
However they don't appear to correspond with the file creation times (I'm not sure if file time stamps in files area is same time format as logs). I have about 4 separate instances of this error, sometimes it is just logged once, sometimes there are several in succession.
My other issue which may be related is that one user ran a report that had many entries. On the first iteration for the user, over 400 items were queried from REST and written out to the html. The second iteration returned more (I don't know how much as I can only see what was recorded. The file appears to have stopped writing in the middle of item # 1,038 (approximately). I know this because I can actually see the HTML table truncated half way through this item.
The user doesn't remember exact details of runtime, but it is possible that this write operation was running for several minutes. Originally I was opening and closing my file on every writeline, but this obviously was slow performance. So, I modified my code to open the file at the beginning of the report and not close it until the entire report was written. Is there some timeout that would have cause the OS to close the file in the middle of writes? How can this be prevented?