[ 
https://issues.apache.org/jira/browse/HIVE-24851?focusedWorklogId=562314&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-562314
 ]

ASF GitHub Bot logged work on HIVE-24851:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 08/Mar/21 11:46
            Start Date: 08/Mar/21 11:46
    Worklog Time Spent: 10m 
      Work Description: losipiuk commented on a change in pull request #2040:
URL: https://github.com/apache/hive/pull/2040#discussion_r589363045



##########
File path: 
ql/src/java/org/apache/hadoop/hive/ql/io/avro/AvroGenericRecordReader.java
##########
@@ -88,22 +88,38 @@ public AvroGenericRecordReader(JobConf job, FileSplit 
split, Reporter reporter)
       gdr.setExpected(latest);
     }
 
-    if (split.getLength() == 0) {
-      this.isEmptyInput = true;
-      this.start = 0;
-      this.reader = null;
+    org.apache.avro.file.FileReader<GenericRecord> nonFinalReader = null;
+    try {
+      if (split.getLength() == 0) {
+        this.isEmptyInput = true;
+        this.start = 0;
+        this.reader = null;
+      } else {
+        this.isEmptyInput = false;
+        nonFinalReader = new DataFileReader<GenericRecord>(new 
FsInput(split.getPath(), job), gdr);
+        this.reader = nonFinalReader;
+        this.reader.sync(split.getStart());
+        this.start = reader.tell();

Review comment:
       We could drop `nonFinalReader` if we just wrapped two `extract*` calls 
with `try..catch` like this:
   ```java
         if (split.getLength() == 0) {
           this.isEmptyInput = true;
           this.start = 0;
           this.reader = null;
         } else {
           this.isEmptyInput = false;
           this.reader = new DataFileReader<GenericRecord>(new 
FsInput(split.getPath(), job), gdr);
           this.reader.sync(split.getStart());
           this.start = reader.tell();
         }
         this.stop = split.getStart() + split.getLength();
         this.recordReaderID = new UID();
   
       try {
         this.writerTimezone = extractWriterTimezoneFromMetadata(job, split, 
gdr);
         this.writerProleptic = extractWriterProlepticFromMetadata(job, split, 
gdr);
       } catch (Exception e) {
         if (this.reader != null) {
           try {
             this.reader.close();
           } catch (Exception closeException) {
             if (closeException != e) {
               e.addSuppressed(closeException);
             }
           }
         }
         throw e;
       }
   ```
   
   But then we can still have a `close`-leak if `this.reader.sync()` or 
`this.reader.tell()` throws an exception.
   
   And If I want to wrap everything with `try...catch` I need extra variable.
   Specifically this (use `this.reader` instead `nonFinalReader`):
   ```java
       try {
         if (split.getLength() == 0) {
           this.isEmptyInput = true;
           this.start = 0;
           this.reader = null;
         } else {
           this.isEmptyInput = false;
           this.reader = new DataFileReader<GenericRecord>(new 
FsInput(split.getPath(), job), gdr);
           this.reader.sync(split.getStart());
           this.start = reader.tell();
         }
         this.stop = split.getStart() + split.getLength();
         this.recordReaderID = new UID();
   
         this.writerTimezone = extractWriterTimezoneFromMetadata(job, split, 
gdr);
         this.writerProleptic = extractWriterProlepticFromMetadata(job, split, 
gdr);
       } catch (Exception e) {
         if (this.reader != null) {
           try {
             this.reader.close();
           } catch (Exception closeException) {
             if (closeException != e) {
               e.addSuppressed(closeException);
             }
           }
         }
         throw e;
       }
     }
   ```
   does not work because compiler complains that `this.reader` in `catch` 
clause can be not initialized
   
   And this (initialize `this.reader` to null before `if`):
   ```java
       this.reader = null;
       try {
         if (split.getLength() == 0) {
           this.isEmptyInput = true;
           this.start = 0;
           this.reader = null;
         } else {
           this.isEmptyInput = false;
           this.reader = new DataFileReader<GenericRecord>(new 
FsInput(split.getPath(), job), gdr);
           this.reader.sync(split.getStart());
           this.start = reader.tell();
         }
         this.stop = split.getStart() + split.getLength();
         this.recordReaderID = new UID();
   
         this.writerTimezone = extractWriterTimezoneFromMetadata(job, split, 
gdr);
         this.writerProleptic = extractWriterProlepticFromMetadata(job, split, 
gdr);
       } catch (Exception e) {
         if (this.reader != null) {
           try {
             this.reader.close();
           } catch (Exception closeException) {
             if (closeException != e) {
               e.addSuppressed(closeException);
             }
           }
         }
         throw e;
       }
     }
   ```
   does not work because `this.reader` is assigned more than once.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 562314)
    Time Spent: 2h 50m  (was: 2h 40m)

> resources leak on exception in AvroGenericRecordReader constructor
> ------------------------------------------------------------------
>
>                 Key: HIVE-24851
>                 URL: https://issues.apache.org/jira/browse/HIVE-24851
>             Project: Hive
>          Issue Type: Bug
>    Affects Versions: 3.1.2, 4.0.0
>            Reporter: Lukasz Osipiuk
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 2h 50m
>  Remaining Estimate: 0h
>
> AvroGenericRecordReader constructor creates an instance of FileReader but 
> lacks proper exception handling, and reader is not closed on the failure path.
> This results in leaking of underlying resources (e.g. S3 connections).
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to