You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mapreduce-issues@hadoop.apache.org by "Owen O'Malley (JIRA)" <ji...@apache.org> on 2011/01/05 18:06:48 UTC

[jira] Commented: (MAPREDUCE-2243) Close all the file streams propely in a finally block to avoid their leakage.

    [ https://issues.apache.org/jira/browse/MAPREDUCE-2243?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12977849#action_12977849 ] 

Owen O'Malley commented on MAPREDUCE-2243:
------------------------------------------

Finally blocks have some very bad properties for exceptions. In particular, they tend to mask errors.

{code:title=bad example}
try {
  f = fs.open(...);
  f.write(...);
} finally {
  f.close();
}
{code}

because if an exception is thrown in the close it will mask exceptions thrown in main body. 

To make the problem concrete, if you don't have permission to write the file, it will throw an IOException saying so. But then the finally block will get a NullPointerException on the close and the user will get that exception without the original exception.

The preferred style is to do:

{code:title=good example}
try {
  f = fs.open(...);
  f.write(...);
  f.close();
} catch (IOException ioe) {
  IOUtils.cleanup(LOG, f);
  throw ioe;
}
{code}

> Close all the file streams propely in a finally block to avoid their leakage.
> -----------------------------------------------------------------------------
>
>                 Key: MAPREDUCE-2243
>                 URL: https://issues.apache.org/jira/browse/MAPREDUCE-2243
>             Project: Hadoop Map/Reduce
>          Issue Type: Improvement
>          Components: jobtracker, tasktracker
>    Affects Versions: 0.20.1, 0.22.0
>         Environment: NA
>            Reporter: Bhallamudi Venkata Siva Kamesh
>            Priority: Minor
>             Fix For: 0.22.0
>
>   Original Estimate: 72h
>  Remaining Estimate: 72h
>
> In the following classes streams should be closed in finally block to avoid their leakage in the exceptional cases.
> CompletedJobStatusStore.java
> ------------------------------------------
>        dataOut.writeInt(events.length);
>         for (TaskCompletionEvent event : events) {
>           event.write(dataOut);
>         }
>        dataOut.close() ;
> EventWriter.java
> ----------------------
>    encoder.flush();
>    out.close();
> MapTask.java
> -------------------
>     splitMetaInfo.write(out);
>      out.close();
> TaskLog
> ------------
>  1) str = fis.readLine();
>       fis.close();
> 2) dos.writeBytes(Long.toString(new File(logLocation, LogName.SYSLOG
>       .toString()).length() - prevLogLength) + "\n");
>     dos.close();
> TotalOrderPartitioner.java
> -----------------------------------
>  while (reader.next(key, value)) {
> 	      parts.add(key);
> 	      key = ReflectionUtils.newInstance(keyClass, conf);
> 	    }
> reader.close();

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.