After converting my Google Apps Engine website from Python 2.7 to Python 3.11 the website crashes about once an hour. The error message is that a local variable is undefined. This error is not repeatable. When I run the same query over I do not get an error.
The full error message from the log is:UnboundLocalError: cannot access local variable 'Pelevation' where it is not associated with a value
In my program I do not see how this the variable can be undefined. I have added a check where the program defines the variable to insure that it is defined. The intermittent error still happens. Then I added a check for the variable being undefined and then reset it back to the proper value. After setting the variable back to the correct value the next variable accessed in the program is undefined. My impression is that all the variables in the program have become undefined.
These symptoms are consistent with a problem in the free memory management, something that my program can not access. I have updated to the most recent version of flask and the other support package that I use. This did not change the frequency of this intermittent error.
I have spent perhaps a week looking for something that I may have done to cause this problem. The problem is present in three different websites that have similar code. I am stuck.
A code sample would be too long. However, a code description may be clearer. The Python 3.11 code is the same as the Python 2.7 code which never failed.
Before the loop a number of variables are loaded with strings from NDB memory. For example, the elevation string contains 13,000 characters. Each character indicates that a species can be found at the selected elevation. The first character might be for species Aster alba while the last character might be for specie Zygmorf zambia. If the user selects an elevation of 500 feet the string for around 500 feet would be loaded from NDB into the elevation string. The second string might be for location. If the user selected New York City the string for that area would be loaded from NDB into the location string. In this way the program sets up strings for many attributes.
After these strings have been loaded from NDB into various variables the loop is entered. The loop starts by examining the first character in each attribute string and if all characters are "true" that species qualifies and is added to a list of species that meet all of the selection criteria. The next iteration of the loop looks at the next character in all of the strings. After 13,000 iterations of the loop all of the species that meet the selection criteria have been found and have been added to the list.
The crash happens in the loop. Generally it is the elevation string that no longer contains a value. However, it can be one of the other strings.To investigate this problem I added code right before the start of the loop that checks the length of each string. This shows that when the loop is entered all strings have a value. After an hour or so of this version of the website operating it also crashed with the elevation string having no value. I added code so that if the elevation string had no value, the proper value was again read from NDB memory allowing the program to continue. After this change was made the program continued to crash with the string immediately after the elevation string containing no value.
Note that as the program loops it adds species that meet the criteria to a list. This list can be up to 13,000 species long if all species meet the criteria. As the list becomes longer the free memory management software may decide to perform free memory garbage collection. If the free memory contains an error the garbage collection process may corrupt the contents of the variables of the program which could then cause the "variable not associated with a value" crash. An hour of operation will usually be around 300 queries. Many of these queries will have been entered by people. Some of the queries may be from robots that work for search engines. I also see queries from malicious programs that generate seemingly reasonable queries but with obvious garbage. This sort of stuff was happening with the Python 2.7 version and I have not found any correlation between these attacks and the crashes.
A Python program is not allowed to corrupt the free memory. A bug in Python or in one of the other associated programs, such as Flask, can corrupt the free memory.
I am having trouble understanding how I can have this sort of problem without it causing trouble to most other Google App Engine websites. I am also unable to find a way of debugging this problem.