{"record":{"id":"0de65b9090215c46","repo":"python/cpython","slug":"pythondumprefsfile-cannot-create-file-ls-n","errorCode":null,"errorMessage":"PYTHONDUMPREFSFILE: cannot create file: %ls\\n","messagePattern":"PYTHONDUMPREFSFILE: cannot create file: %ls\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"Python/pylifecycle.c","lineNumber":2547,"sourceCode":"\n    /* unload faulthandler module */\n    _PyFaulthandler_Fini();\n\n    /* dump hash stats */\n    _PyHash_Fini();\n\n#ifdef Py_TRACE_REFS\n    /* Display all objects still alive -- this can invoke arbitrary\n     * __repr__ overrides, so requires a mostly-intact interpreter.\n     * Alas, a lot of stuff may still be alive now that will be cleaned\n     * up later.\n     */\n\n    FILE *dump_refs_fp = NULL;\n    if (dump_refs_file != NULL) {\n        dump_refs_fp = _Py_wfopen(dump_refs_file, L\"w\");\n        if (dump_refs_fp == NULL) {\n            fprintf(stderr, \"PYTHONDUMPREFSFILE: cannot create file: %ls\\n\", dump_refs_file);\n        }\n    }\n\n    if (dump_refs) {\n        _Py_PrintReferences(tstate->interp, stderr);\n    }\n\n    if (dump_refs_fp != NULL) {\n        _Py_PrintReferences(tstate->interp, dump_refs_fp);\n    }\n#endif /* Py_TRACE_REFS */\n\n    /* At this point there's almost no other Python code that will run,\n       nor interpreter state needed.  The only possibility is the\n       finalizers of the objects stored on tstate (and tstate->interp),\n       which are triggered via finalize_interp_clear().\n\n       For now we operate as though none of those finalizers actually","sourceCodeStart":2529,"sourceCodeEnd":2565,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/pylifecycle.c#L2529-L2565","documentation":"Printed in Py_Finalize (Python/pylifecycle.c, Py_TRACE_REFS builds only) when PYTHONDUMPREFSFILE names a file that _Py_wfopen cannot open for writing. Reference dumping to that file is skipped (dump_refs_fp stays NULL); if PYTHONDUMPREFS is also set, output still goes to stderr. Exit continues normally.","triggerScenarios":"Running a --with-trace-refs debug build with PYTHONDUMPREFSFILE set to a path in a nonexistent directory, a path without write permission, or an unwritable name; the fopen(\"w\") fails.","commonSituations":"Debug builds used for reference-leak hunting where the output path was never created; CI running as a user without write access to the target dir; typo'd filenames.","solutions":["mkdir -p the target directory and ensure the user can write it","Use an absolute path in a writable location, e.g. PYTHONDUMPREFSFILE=/tmp/refs.txt","Verify you are on a --with-trace-refs build (python -c 'import sys; print(sys.gettotalrefcount())' works only there)"],"exampleFix":"# before\nPYTHONDUMPREFSFILE=/nonexistent/dir/refs.txt ./python-dbg app.py\n# after\nmkdir -p /tmp/refs && PYTHONDUMPREFSFILE=/tmp/refs/refs.txt ./python-dbg app.py","handlingStrategy":"validation","validationCode":"# bash: pre-create and check writability before the run\nD=$(dirname \"$PYTHONDUMPREFSFILE\"); mkdir -p \"$D\" && touch \"$PYTHONDUMPREFSFILE\" || exit 1\nPYTHONDUMPREFSFILE=\"$PYTHONDUMPREFSFILE\" ./python-dbg app.py","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always mkdir -p the dump directory in leak-hunting scripts","Use absolute, writable paths like /tmp for PYTHONDUMPREFSFILE in CI","Remember this only exists on --with-trace-refs builds; regular builds ignore the variable silently"],"tags":["cpython","debug-build","trace-refs","file-io","leak-hunting"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}