Skip to content

Commit 5814292

Browse files
committed
Logging close() in case close() fails.
1 parent 323dfec commit 5814292

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

core/src/main/scala/org/apache/spark/network/ManagedBuffer.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import scala.util.Try
2727
import com.google.common.io.ByteStreams
2828
import io.netty.buffer.{ByteBufInputStream, ByteBuf}
2929

30-
import org.apache.spark.util.ByteBufferInputStream
30+
import org.apache.spark.util.{ByteBufferInputStream, Utils}
3131

3232

3333
/**
@@ -83,7 +83,7 @@ final class FileSegmentManagedBuffer(val file: File, val offset: Long, val lengt
8383
}
8484
} finally {
8585
if (channel != null) {
86-
channel.close()
86+
Utils.tryLog(channel.close())
8787
}
8888
}
8989
}
@@ -97,7 +97,7 @@ final class FileSegmentManagedBuffer(val file: File, val offset: Long, val lengt
9797
} catch {
9898
case e: IOException =>
9999
if (is != null) {
100-
is.close()
100+
Utils.tryLog(is.close())
101101
}
102102
Try(file.length).toOption match {
103103
case Some(fileLen) =>
@@ -107,7 +107,7 @@ final class FileSegmentManagedBuffer(val file: File, val offset: Long, val lengt
107107
}
108108
case e: Throwable =>
109109
if (is != null) {
110-
is.close()
110+
Utils.tryLog(is.close())
111111
}
112112
throw e
113113
}

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,20 @@ private[spark] object Utils extends Logging {
13041304
}
13051305
}
13061306

1307+
/** Executes the given block in a Try, logging any uncaught exceptions. */
1308+
def tryLog[T](f: => T): Try[T] = {
1309+
try {
1310+
val res = f
1311+
scala.util.Success(res)
1312+
} catch {
1313+
case ct: ControlThrowable =>
1314+
throw ct
1315+
case t: Throwable =>
1316+
logError(s"Uncaught exception in thread ${Thread.currentThread().getName}", t)
1317+
scala.util.Failure(t)
1318+
}
1319+
}
1320+
13071321
/** Returns true if the given exception was fatal. See docs for scala.util.control.NonFatal. */
13081322
def isFatalError(e: Throwable): Boolean = {
13091323
e match {

0 commit comments

Comments
 (0)