Make sure we close the file handle before we delete the file.

git-svn-id: https://hyracks.googlecode.com/svn/branches/fullstack_rsd_fixes@3331 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/DatasetPartitionWriter.java b/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/DatasetPartitionWriter.java
index f6ae540..317f553 100644
--- a/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/DatasetPartitionWriter.java
+++ b/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/DatasetPartitionWriter.java
@@ -77,7 +77,7 @@
         FileReference fRef = manager.getFileFactory().createUnmanagedWorkspaceFile(fName);
         fileHandle = resultState.getIOManager().open(fRef, IIOManager.FileReadWriteMode.READ_WRITE,
                 IIOManager.FileSyncMode.METADATA_ASYNC_DATA_ASYNC);
-        resultState.init(fRef);
+        resultState.init(fRef, fileHandle);
     }
 
     @Override
diff --git a/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/ResultState.java b/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/ResultState.java
index 0f1d94c..661df93 100644
--- a/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/ResultState.java
+++ b/hyracks/hyracks-control/hyracks-control-nc/src/main/java/edu/uci/ics/hyracks/control/nc/dataset/ResultState.java
@@ -24,6 +24,7 @@
 import edu.uci.ics.hyracks.api.dataflow.state.IStateObject;
 import edu.uci.ics.hyracks.api.dataset.Page;
 import edu.uci.ics.hyracks.api.io.FileReference;
+import edu.uci.ics.hyracks.api.io.IFileHandle;
 import edu.uci.ics.hyracks.api.io.IIOManager;
 import edu.uci.ics.hyracks.api.job.JobId;
 import edu.uci.ics.hyracks.api.partitions.ResultSetPartitionId;
@@ -43,6 +44,8 @@
 
     private FileReference fileRef;
 
+    private IFileHandle writeFileHandle;
+
     private long size;
 
     private long persistentSize;
@@ -56,8 +59,9 @@
         localPageList = new ArrayList<Page>();
     }
 
-    public synchronized void init(FileReference fileRef) {
+    public synchronized void init(FileReference fileRef, IFileHandle writeFileHandle) {
         this.fileRef = fileRef;
+        this.writeFileHandle = writeFileHandle;
 
         size = 0;
         persistentSize = 0;
@@ -65,6 +69,13 @@
     }
 
     public synchronized void deinit() {
+        if (writeFileHandle != null) {
+            try {
+                ioManager.close(writeFileHandle);
+            } catch (IOException e) {
+                // Since file handle could not be closed, just ignore.
+            }
+        }
         fileRef.delete();
     }