{"record":{"id":"220db447b1da624a","repo":"nodejs/node","slug":"can-t-write-empty-bucket","errorCode":null,"errorMessage":"can't write empty bucket","messagePattern":"can't write empty bucket","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/bccache.py","lineNumber":101,"sourceCode":"        if magic != bc_magic:\n            self.reset()\n            return\n        # the source code of the file changed, we need to reload\n        checksum = pickle.load(f)\n        if self.checksum != checksum:\n            self.reset()\n            return\n        # if marshal_load fails then we need to reload\n        try:\n            self.code = marshal_load(f)\n        except (EOFError, ValueError, TypeError):\n            self.reset()\n            return\n\n    def write_bytecode(self, f):\n        \"\"\"Dump the bytecode into the file or file like object passed.\"\"\"\n        if self.code is None:\n            raise TypeError('can\\'t write empty bucket')\n        f.write(bc_magic)\n        pickle.dump(self.checksum, f, 2)\n        marshal_dump(self.code, f)\n\n    def bytecode_from_string(self, string):\n        \"\"\"Load bytecode from a string.\"\"\"\n        self.load_bytecode(BytesIO(string))\n\n    def bytecode_to_string(self):\n        \"\"\"Return the bytecode as string.\"\"\"\n        out = BytesIO()\n        self.write_bytecode(out)\n        return out.getvalue()\n\n\nclass BytecodeCache(object):\n    \"\"\"To implement your own bytecode cache you have to subclass this class\n    and override :meth:`load_bytecode` and :meth:`dump_bytecode`.  Both of","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/bccache.py#L83-L119","documentation":"Raised by Bucket.write_bytecode (jinja2 bccache.py) when asked to serialize a bytecode cache bucket whose compiled code was never loaded — i.e. self.code is still None. write_bytecode refuses to emit an empty bucket because the cache file would be meaningless (no marshal payload).","triggerScenarios":"Calling bucket.write_bytecode(f) before bucket.load_bytecode has successfully populated bucket.code; calling bytecode_to_string() on a freshly constructed, never-loaded Bucket; a cache-management loop that writes buckets unconditionally including ones that failed to load.","commonSituations":"Custom bytecode-cache wiring that initializes buckets but skips the load step on cache misses then tries to write; a corruption path where load_bytecode hit EOFError/ValueError (calling reset(), which sets code=None) and the caller still tries to write.","solutions":["Always call load_bytecode(f) (and let it set self.code) before write_bytecode; check `bucket.code is not None` first.","On a cache miss, compile the template and assign bucket.code = compiled before writing.","Treat a None code as 'cache empty' and skip the write rather than erroring."],"exampleFix":"# before\nbucket.write_bytecode(f)  # code may be None\n# after\nif bucket.code is None:\n    bucket.code = env.compile(source)\nbucket.write_bytecode(f)","handlingStrategy":"validation","validationCode":"def safe_write(bucket, f):\n    if bucket.code is None:\n        raise RuntimeError('bucket has no code; load/compile first')\n    bucket.write_bytecode(f)","typeGuard":"def bucket_has_code(bucket) -> bool:\n    return bucket.code is not None","tryCatchPattern":"try:\n    bucket.write_bytecode(f)\nexcept TypeError as e:\n    if 'empty bucket' in str(e):\n        bucket.code = env.compile(source)\n        bucket.write_bytecode(f)\n    else:\n        raise","preventionTips":["Always load_bytecode (or assign bucket.code) before write_bytecode.","Treat load failure (EOFError/ValueError) as 'cache empty', not as 'write anyway'.","Centralize cache read/write through one helper that checks code is set."],"tags":["jinja2","cache","bytecode"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}