Skip to content

Commit 3f1da96

Browse files
committed
Guard against duplicate close()
1 parent ab457ca commit 3f1da96

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

core/src/main/scala/org/apache/spark/serializer/KryoSerializer.scala

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.spark.serializer
1919

20-
import java.io.{EOFException, InputStream, OutputStream}
20+
import java.io.{EOFException, IOException, InputStream, OutputStream}
2121
import java.nio.ByteBuffer
2222

2323
import scala.reflect.ClassTag
@@ -148,14 +148,22 @@ class KryoSerializationStream(
148148
this
149149
}
150150

151-
override def flush() { output.flush() }
151+
override def flush() {
152+
if (output == null) {
153+
throw new IOException("Stream is closed")
154+
}
155+
output.flush()
156+
}
157+
152158
override def close() {
153-
try {
154-
output.close()
155-
} finally {
156-
serInstance.releaseKryo(kryo)
157-
kryo = null
158-
output = null
159+
if (output != null) {
160+
try {
161+
output.close()
162+
} finally {
163+
serInstance.releaseKryo(kryo)
164+
kryo = null
165+
output = null
166+
}
159167
}
160168
}
161169
}
@@ -179,13 +187,15 @@ class KryoDeserializationStream(
179187
}
180188

181189
override def close() {
182-
try {
183-
// Kryo's Input automatically closes the input stream it is using.
184-
input.close()
185-
} finally {
186-
serInstance.releaseKryo(kryo)
187-
kryo = null
188-
input = null
190+
if (input != null) {
191+
try {
192+
// Kryo's Input automatically closes the input stream it is using.
193+
input.close()
194+
} finally {
195+
serInstance.releaseKryo(kryo)
196+
kryo = null
197+
input = null
198+
}
189199
}
190200
}
191201
}

0 commit comments

Comments
 (0)