{"record":{"id":"a8769a714ec89dde","repo":"apache/hadoop","slug":"cannot-open-filename","errorCode":null,"errorMessage":"Cannot open filename {}","messagePattern":"Cannot open filename (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java","lineNumber":1136,"sourceCode":"    try (TraceScope ignored = newPathTraceScope(\"newDFSInputStream\", src)) {\n      HdfsLocatedFileStatus s = getLocatedFileInfo(src, true);\n      fd.verify(s); // check invariants in path handle\n      LocatedBlocks locatedBlocks = s.getLocatedBlocks();\n      return openInternal(locatedBlocks, src, verifyChecksum);\n    }\n  }\n\n  private DFSInputStream openInternal(LocatedBlocks locatedBlocks, String src,\n      boolean verifyChecksum) throws IOException {\n    if (locatedBlocks != null) {\n      ErasureCodingPolicy ecPolicy = locatedBlocks.getErasureCodingPolicy();\n      if (ecPolicy != null) {\n        return new DFSStripedInputStream(this, src, verifyChecksum, ecPolicy,\n            locatedBlocks);\n      }\n      return new DFSInputStream(this, src, verifyChecksum, locatedBlocks);\n    } else {\n      throw new IOException(\"Cannot open filename \" + src);\n    }\n  }\n\n  /**\n   * Get the namenode associated with this DFSClient object\n   * @return the namenode associated with this DFSClient object\n   */\n  public ClientProtocol getNamenode() {\n    return namenode;\n  }\n\n  /**\n   * Call {@link #create(String, boolean, short, long, Progressable)} with\n   * default <code>replication</code> and <code>blockSize</code> and null\n   * <code>progress</code>.\n   */\n  public OutputStream create(String src, boolean overwrite)\n      throws IOException {","sourceCodeStart":1118,"sourceCodeEnd":1154,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java#L1118-L1154","documentation":"DFSClient.open() first fetches LocatedBlocks from the NameNode; a null response means no file exists at that src, and openInternal throws a plain IOException('Cannot open filename ' + src) rather than FileNotFoundException. This typing wart means a missing file at open time masquerades as a generic IO error unless you re-check existence yourself.","triggerScenarios":"dfsClient.open(src) when src never existed or was deleted between an exists()/getFileStatus() check and the open; readers racing a writer's create or a cleanup's delete; paths assembled from wrong variables.","commonSituations":"Test pipelines reading output of a job that failed before creating the file; consumers opening files after upstream retention cleanup; create-then-open races in integration tests without synchronization.","solutions":["Pre-check existence immediately before open and throw a precise FileNotFoundException yourself when absent.","In the catch for open(), disambiguate by re-checking existence: the IOException subtype will not tell you it is a missing file.","For read-after-write races, have the producer publish via atomic rename of a temp file and retry open with backoff until it appears.","Log the exact src string handed to open to catch path-assembly bugs."],"exampleFix":"// before\nFSDataInputStream in = dfsClient.open(src);\n// IOException: Cannot open filename /path/file\n\n// after\nif (dfsClient.getFileInfo(src) == null) {\n  throw new FileNotFoundException('Cannot open ' + src + ': not in namespace');\n}\nFSDataInputStream in = dfsClient.open(src);","handlingStrategy":"try-catch","validationCode":"if (!fs.exists(path)) {\n  throw new FileNotFoundException(path + \" missing before open\");\n}","typeGuard":null,"tryCatchPattern":"catch (IOException e) {\n  // open() throws generic IOException for missing files: re-check to classify\n  if (!fs.exists(path)) { /* missing file: recreate/skip/fail with context */ }\n  else { throw e; }\n}","preventionTips":["Do not rely on exception type at open(); re-verify existence in the catch.","Publish files by atomic rename of a temp name, then open the final name.","Retry open with backoff when a concurrent creator is expected."],"tags":["hdfs","open-file","file-not-found","race-condition","exception-typing"],"backgroundTag":"file-not-found","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}