{"action":"closed","issue":{"url":"https://api.github.com/repos/pallets/jinja/issues/281","repository_url":"https://api.github.com/repos/pallets/jinja","labels_url":"https://api.github.com/repos/pallets/jinja/issues/281/labels{/name}","comments_url":"https://api.github.com/repos/pallets/jinja/issues/281/comments","events_url":"https://api.github.com/repos/pallets/jinja/issues/281/events","html_url":"https://github.com/pallets/jinja/issues/281","id":23089782,"number":281,"title":"Failure loading corrupt bytecode file","user":{"login":"timhanus","id":1318913,"avatar_url":"https://avatars.githubusercontent.com/u/1318913?v=3","gravatar_id":"","url":"https://api.github.com/users/timhanus","html_url":"https://github.com/timhanus","followers_url":"https://api.github.com/users/timhanus/followers","following_url":"https://api.github.com/users/timhanus/following{/other_user}","gists_url":"https://api.github.com/users/timhanus/gists{/gist_id}","starred_url":"https://api.github.com/users/timhanus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timhanus/subscriptions","organizations_url":"https://api.github.com/users/timhanus/orgs","repos_url":"https://api.github.com/users/timhanus/repos","events_url":"https://api.github.com/users/timhanus/events{/privacy}","received_events_url":"https://api.github.com/users/timhanus/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2013-11-21T19:14:08Z","updated_at":"2016-04-13T09:03:20Z","closed_at":"2016-04-13T09:03:19Z","body":"There seems to be an edge case in the cache where if the stored bytecode is truncated or corrupted it will throw an exception.  When this happens it should be perfectly fine to reload the template code from scratch in order to render, but instead jinja2 continues throwing exceptions until the key/file is manually removed.\r\n\r\nIn my specific case, a disk filled up and the end of the cache file was truncated.  This means it passed the magic number test that checks for jinja2 and python version, but continually failed to render.  \r\n\r\nPull request coming.  \r\n\r\nHere is a sample program to reproduce:\r\n```\r\nfrom jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache\r\nimport os\r\n\r\ntemplate_name = 'reproduce_bug.jinja2'\r\nfilename = '/tmp/' + template_name\r\n\r\ndef try_render(env):\r\n    try:\r\n        template = env.get_template(template_name)\r\n        template.render(name='world')\r\n        print 'Render Success'\r\n    except: \r\n        print 'Render Failure'\r\n\r\nif __name__ == '__main__':\r\n\r\n    # create an example jinja file\r\n    with open(filename,'w') as fh:\r\n        fh.write('hello {{ name }}')\r\n\r\n    # create an example jinja environment\r\n    fsloader = FileSystemLoader(['/tmp'])\r\n    cache = FileSystemBytecodeCache('/tmp','__jinja_%s.cache')\r\n    env = Environment(loader=fsloader, cache_size=-1, bytecode_cache=cache)\r\n\r\n    # Try a render, this one will work\r\n    try_render(env)\r\n    \r\n    # simulate a truncated file\r\n    cache_filename = cache._get_cache_filename(cache.get_bucket(env, template_name, filename, open(filename).read()))\r\n    cache_contents = open(cache_filename).read()\r\n    with open(cache_filename,'w') as fh:\r\n        fh.write(cache_contents[:-20])\r\n\r\n    # reset the environment so it forces a reload\r\n    env.cache = {}\r\n    \r\n    # this one will fail\r\n    try_render(env)\r\n\r\n    # clean up after ourselves\r\n    cache.clear()\r\n    os.unlink(filename)\r\n```"}}