Personal.X-Istence.com

Bert JW Regeer (畢傑龍)

Interesting bug

I was starting to see something interesting in the output from my Google AppEngine based project. I was automatically creating a link based on a config setting, named SETTINGS["contact"], and the code would work perfectly the first time the page was created and loaded, however multiple refreshes of the same page would show that the link was gaining slashes even when it shouldn't have.

The logic was as follows:

if contact is "mailto:" then do nothing
else
        if contact does not start with "/" or not with "http"
                contact = "/" + contact

This code would work perfectly the first time around, however if I had properly tested by setting contact = "/contact" as well, I may have caught this bug faster.

However since that piece of code was only being called once per page view, it worked perfectly as far as I was aware, the thing is I started seeing more and more slashes being prepended to contact each time I refreshed the page. I looked at the code above, and did not see that my logic should have contained an "and" instead of an "or". Even so if the code is being loaded once per page view the variable "contact" that started off containing the word "contactme" should only turn into "/contactme" and not "////contactme" on the fourth page load.

Each time I added in a logging or debugging statement and refreshed the page it would go back to "/contactme", I would change another thing, or add another debug statement and "/contactme" was there staring me in the face. It took me a little while before I hit refresh a couple of times and noticed that my debug output now contained what I had seen beforehand. That is when it hit me, Google AppEngine caches imports that included my global variables. As long as I did not modify any of the files in my project it would happily use the cached variable, which after the first reload would contain "/contactme", then because of my use of or instead of and, would become "//contactme" on the second reload.

Google AppEngine has some very aggressive caching, which may well be required to have it run at the scale Google wants it to run. This is just another warning for myself that I need to watch out when I modify global variables and that they will stay the same across different sessions.

This is the logic I ended up with:

if contact is "mailto:" or contact starts with "http" then do nothing
else
        if contact does not start with "/"
                contact = "/" + contact

Works exactly how I had expected it to work.

Project file structure

I'm currently working on a new website that will be hosted on Google AppEngine, however I am currently in the middle of revamping some older code and realised that maybe just maybe it belongs with the model rather than in a separate directory.

The setup currently is to have a models directory and a forms directory, they contain the models and the forms respectively, however I am starting to realise more and more that the form is a direct extension of the validation required in the model (which is not possible with the current database stuff that Google AppEngine offers), the form is taking user input and making sure that it is correct and then just passing it off to the model to check it into the datastore.

I'm thinking of merging the form into the model source code so that I can remove the forms folder entirely, since it just doesn't make sense to have the two in separate locations whereby one may be changed but the other may not. I'm afraid that they may get to be out of sync, and cause issues.

Will have to think about it for a little bit, no need to make rash decisions this late into the evening and blow away part of my source tree.