{"record":{"id":"c95c6ab0f27a6dda","repo":"apache/dolphinscheduler","slug":"task-instance-is-null-or-host-is-null","errorCode":null,"errorMessage":"task instance is null or host is null","messagePattern":"task instance is null or host is null","errorType":"exception","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java","lineNumber":113,"sourceCode":"        Result<ResponseTaskLog> result = new Result<>(Status.SUCCESS.getCode(), Status.SUCCESS.getMsg());\n        String log = queryLog(taskInstance, skipLineNum, limit);\n        int lineNum = log.split(\"\\\\r\\\\n\").length;\n        result.setData(new ResponseTaskLog(lineNum, log));\n        return result;\n    }\n\n    /**\n     * get log size\n     *\n     * @param loginUser  login user\n     * @param taskInstId task instance id\n     * @return log byte array\n     */\n    @Override\n    public byte[] getLogBytes(User loginUser, int taskInstId) {\n        TaskInstance taskInstance = taskInstanceDao.queryById(taskInstId);\n        if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) {\n            throw new ServiceException(\"task instance is null or host is null\");\n        }\n        Project project = projectDao.queryProjectByTaskInstanceId(taskInstId);\n        projectService.checkProjectAndAuthThrowException(loginUser, project, DOWNLOAD_LOG);\n        return getLogBytes(taskInstance);\n    }\n\n    /**\n     * query log\n     *\n     * @param loginUser   login user\n     * @param projectCode project code\n     * @param taskInstId  task instance id\n     * @param skipLineNum skip line number\n     * @param limit       limit\n     * @return log string data\n     */\n    @Override\n    @SuppressWarnings(\"unchecked\")","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java#L95-L131","documentation":"getLogBytes looks up the task instance by id and requires both the instance to exist and its host to be non-blank, because log bytes are fetched from the worker host. If the instance is missing or its host is empty, a ServiceException with the literal message 'task instance is null or host is null' is thrown instead of a typed status.","triggerScenarios":"Calling GET /log/download-log (or getLogBytes) with a taskInstId that does not exist, or whose task instance has an empty host (task not yet dispatched to a worker, or host data lost).","commonSituations":"Downloading logs for a task that failed before being assigned to a worker; requesting logs of a task instance deleted from the DB; stale UI link pointing at a purged instance id.","solutions":["Confirm taskInstId exists: query t_ds_task_instance for that id before downloading.","Wait until the task has been dispatched and has a non-empty host; retry after the task starts running.","Download logs from a completed, assigned task instance instead.","If hosts are legitimately blank due to DB corruption, re-run the task to regenerate host info."],"exampleFix":"// before: blind download\nservice.getLogBytes(loginUser, 999999);\n// after: guard first\nTaskInstance ti = taskInstanceDao.queryById(999999);\nif (ti == null || StringUtils.isBlank(ti.getHost())) {\n    throw new ServiceException(Status.TASK_INSTANCE_NOT_FOUND);\n}\nservice.getLogBytes(loginUser, 999999);","handlingStrategy":"validation","validationCode":"TaskInstance ti = taskInstanceDao.queryById(taskInstId);\nif (ti == null || StringUtils.isBlank(ti.getHost())) {\n    throw new ServiceException(Status.TASK_INSTANCE_NOT_FOUND);\n}","typeGuard":"boolean isDownloadable(TaskInstance ti) { return ti != null && StringUtils.isNotBlank(ti.getHost()); }","tryCatchPattern":"try {\n    byte[] log = loggerService.getLogBytes(loginUser, taskInstId);\n} catch (ServiceException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"task instance is null\")) {\n        // inform user the task has not run on any worker yet / does not exist\n    } else { throw e; }\n}","preventionTips":["Only expose log download links for task instances in a completed/running state with a host.","Poll the task status endpoint until host is populated before fetching logs.","Purge old instances from UI history to avoid stale ids.","Prefer queryLog-style endpoints returning typed statuses over the string-message variant."],"tags":["logging","task-instance","null-check"],"backgroundTag":"record-not-found","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"}