{"record":{"id":"bc248c4840653327","repo":"apache/hadoop","slug":"taskid-not-found-or-invalid","errorCode":null,"errorMessage":"taskid {} not found or invalid","messagePattern":"taskid (.+?) not found or invalid","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/webapp/AMWebServices.java","lineNumber":182,"sourceCode":"    TaskId taskID;\n    Task task;\n    try {\n      taskID = MRApps.toTaskID(tid);\n    } catch (YarnRuntimeException e) {\n      // TODO: after MAPREDUCE-2793 YarnRuntimeException is probably not expected here\n      // anymore but keeping it for now just in case other stuff starts failing.\n      // Also, the webservice should ideally return BadRequest (HTTP:400) when\n      // the id is malformed instead of NotFound (HTTP:404). The webserver on\n      // top of which AMWebServices is built seems to automatically do that for\n      // unhandled exceptions\n      throw new NotFoundException(e.getMessage());\n    } catch (NumberFormatException ne) {\n      throw new NotFoundException(ne.getMessage());\n    } catch (IllegalArgumentException e) {\n      throw new NotFoundException(e.getMessage());\n    } \n    if (taskID == null) {\n      throw new NotFoundException(\"taskid \" + tid + \" not found or invalid\");\n    }\n    task = job.getTask(taskID);\n    if (task == null) {\n      throw new NotFoundException(\"task not found with id \" + tid);\n    }\n    return task;\n  }\n\n  /**\n   * convert a task attempt id string to an actual task attempt and handle all\n   * the error checking.\n   */\n  public static TaskAttempt getTaskAttemptFromTaskAttemptString(String attId, Task task)\n      throws NotFoundException {\n    TaskAttemptId attemptId;\n    TaskAttempt ta;\n    try {\n      attemptId = MRApps.toTaskAttemptID(attId);","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/webapp/AMWebServices.java#L164-L200","documentation":"Thrown by the MapReduce ApplicationMaster REST API (AMWebServices, usually http://<am-host>:8099/ws/v1/mapreduce). The {taskid} path parameter was converted with MRApps.toTaskID(), which returned null (null/empty input or a string that is not a task id) instead of throwing. JAX-RS NotFoundException maps this to HTTP 404 even though a malformed id is semantically a 400; the in-code comment at AMWebServices.java:171-173 admits this.","triggerScenarios":"Calling endpoints that embed a task id - GET /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}, /tasks/{taskid}/counters, /tasks/{taskid}/attempts/... , PUT /tasks/{taskid}/attempts/{attemptid}/state - with a taskid that is empty, lacks the 'task_' prefix (for example '0001_m_000003' or an attempt_... id), or cannot be parsed into a TaskId.","commonSituations":"Client code builds the id by string concatenation and drops the prefix; an attempt id is sent where a task id is expected; ids copy-pasted from history-server or log URLs with different formatting; untrimmed whitespace or bad URL encoding.","solutions":["Use the exact task id returned by GET /ws/v1/mapreduce/jobs/{jobid}/tasks instead of building one by hand","Check the format: task_<jobTimestamp>_<jobSeq>_[m|r]_<taskSeq>, e.g. task_1401318629735_0001_m_000003","Trim and URL-encode/decode the parameter before sending","If you must distinguish 'malformed' from 'missing', validate the format client-side: the API returns 404 for both"],"exampleFix":"// before\nGET /ws/v1/mapreduce/jobs/job_1401318629735_0001/tasks/0001_m_000003\n// 404 { \"taskid 0001_m_000003 not found or invalid\" }\n\n// after\nGET /ws/v1/mapreduce/jobs/job_1401318629735_0001/tasks/task_1401318629735_0001_m_000003","handlingStrategy":"validation","validationCode":"private static final Pattern TASK_ID_RE =\n    Pattern.compile(\"^task_\\\\d+_\\\\d+_[mr]_\\\\d+$\");\n\nif (tid == null || !TASK_ID_RE.matcher(tid).matches()) {\n  throw new IllegalArgumentException(\"bad task id: \" + tid); // do not call the AM\n}","typeGuard":"static boolean isWellFormedTaskId(String tid) {\n  return tid != null && tid.startsWith(\"task_\")\n      && tid.split(\"_\").length == 5;\n}","tryCatchPattern":"try {\n  TaskInfo t = client.target(amBase).path(\"jobs/{jid}/tasks/{tid}\")\n      .resolveTemplates(Map.of(\"jid\", jid, \"tid\", tid))\n      .request(MediaType.APPLICATION_JSON).get(TaskInfo.class);\n} catch (javax.ws.rs.NotFoundException nfe) {\n  // 404 covers both malformed and unknown ids here; treat as 'no such task'\n}","preventionTips":["Always take task ids from GET /jobs/{jobid}/tasks, never build them by concatenation","Keep one id-formatting helper in client code and route every call through it","URL-encode path parameters and trim user input","Log the offending id on every 404 so bad ids surface immediately"],"tags":["mapreduce","rest","http-404","applicationmaster","task-id","web-services"],"backgroundTag":"rest-malformed-resource-id","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}