On Jan 11, 2012, at 7:54 AM, Anders Roos wrote:
> I have discovered a bug where I got read errors for language files (ERROR:
> Syntax error in /home/... .../languages/en-gb.py). After some debugging I
> found out that the cause is that the file is being read while it is half
> written by another thread (or process). There is a lock for it, but the full
> contents of the file is not written before close (which is after the unlock).
> By adding a flush before the unlock in write_dict, I don't get the error
> anymore.
A simpler fix might be to remove the unlock call. (In fact, it seems to me that
it's an error to explicitly unlock a file that's been locked with LOCK_SH.
Isn't it?)
A precaution (though it only papers over the error) would be to flush in
portalocker before unlocking a file.
>
> I discovered the error by running system tests with Selenium in parallel, I
> suppose there is a simpler way to trigger the bug. ;-)
>
> My patched write_dict in languages.py:
>
> def write_dict(filename, contents):
> try:
> fp = open(filename, 'w')
> except IOError:
> if not is_gae:
> logging.warning('Unable to write to file %s' % filename)
> return
> portalocker.lock(fp, portalocker.LOCK_EX)
> fp.write('# coding: utf8\n{\n')
> for key in sorted(contents):
> fp.write('%s: %s,\n' % (utf8_repr(key), utf8_repr(contents[key])))
> fp.write('}\n')
> fp.flush()
> portalocker.unlock(fp)
> fp.close()
>
> Regards
> Anders