{"record":{"id":"0b1d3d8a74226380","repo":"apache/dolphinscheduler","slug":"flink-script-file-exists-in-path-s-before-creati","errorCode":null,"errorMessage":"Flink Script file exists in path: %s before creation and cannot be deleted","messagePattern":"Flink Script file exists in path: (.+?) before creation and cannot be deleted","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FileUtils.java","lineNumber":73,"sourceCode":"\n    public static void generateScriptFile(TaskExecutionContext taskExecutionContext, FlinkParameters flinkParameters) {\n        String initScriptFilePath = FileUtils.getInitScriptFilePath(taskExecutionContext);\n        String scriptFilePath = FileUtils.getScriptFilePath(taskExecutionContext);\n        String initOptionsString = StringUtils.join(\n                FlinkArgsUtils.buildInitOptionsForSql(flinkParameters),\n                FlinkConstants.FLINK_SQL_NEWLINE).concat(FlinkConstants.FLINK_SQL_NEWLINE);\n        writeScriptFile(initScriptFilePath, initOptionsString + flinkParameters.getInitScript());\n        writeScriptFile(scriptFilePath, flinkParameters.getRawScript());\n    }\n\n    private static void writeScriptFile(String scriptFileFullPath, String script) {\n        File scriptFile = new File(scriptFileFullPath);\n        Path path = scriptFile.toPath();\n        if (Files.exists(path)) {\n            try {\n                Files.delete(path);\n            } catch (IOException e) {\n                throw new RuntimeException(String\n                        .format(\"Flink Script file exists in path: %s before creation and cannot be deleted\", path), e);\n            }\n        }\n\n        Set<PosixFilePermission> perms = PosixFilePermissions.fromString(RWXR_XR_X);\n        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);\n        try {\n            if (SystemUtils.IS_OS_WINDOWS) {\n                Files.createFile(path);\n            } else {\n                if (!scriptFile.getParentFile().exists()) {\n                    scriptFile.getParentFile().mkdirs();\n                }\n                Files.createFile(path, attr);\n            }\n\n            if (StringUtils.isNotEmpty(script)) {\n                String replacedScript = script.replaceAll(\"\\\\r\\\\n\", \"\\n\");","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FileUtils.java#L55-L91","documentation":"FileUtils.writeScriptFile() in the Flink SQL task plugin deletes a pre-existing script file at the target path before writing a new one. If Files.delete(path) throws IOException, it throws RuntimeException('Flink Script file exists in path: %s before creation and cannot be deleted'). This prevents stale or undeletable script files from blocking task execution.","triggerScenarios":"A file exists at scriptFileFullPath and deletion fails due to file permissions, the file being locked/held by another process, or it being a non-empty directory.","commonSituations":"Leftover script from a crashed prior run owned by a different user; file held open by a monitoring process; read-only mount; worker running as a user without delete permission on the tenant temp dir.","solutions":["Check ownership/permissions of the existing script file and the parent directory; ensure the worker user can delete it","Manually remove the stale file, then re-run the task","Verify the tenant execution temp directory is writable and not on a read-only mount","Kill processes holding the file open (e.g. lsof) before retrying"],"exampleFix":"// before\nFiles.delete(path);\n// after\ntry {\n    Files.delete(path);\n} catch (IOException e) {\n    log.warn(\"could not delete stale script {}, attempting overwrite\", path);\n}\n","handlingStrategy":"validation","validationCode":"Path path = Paths.get(scriptFileFullPath);\nif (Files.exists(path)) {\n    if (!Files.isWritable(path.getParent())) {\n        throw new IllegalStateException(\"cannot delete/create script: parent not writable \" + path.getParent());\n    }\n    Files.deleteIfExists(path);\n}","typeGuard":null,"tryCatchPattern":"try {\n    task.handle(null);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"cannot be deleted\")) {\n        Files.deleteIfExists(Paths.get(expectedScriptPath));\n    }\n    throw e;\n}","preventionTips":["Run all tasks for a tenant under the same OS user","Clean stale temp scripts after failed runs","Avoid read-only or NFS mounts for the execution temp dir","Check for file locks (lsof) before retrying"],"tags":["flink","file-io","file-delete","posix-permissions"],"backgroundTag":"file-write-failed","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}