Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIVY-322. Catch JsonParseExceptions in PythonInterpreter on rawtext response from fake_shell. #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConverters._
import scala.util.control.NonFatal

import com.fasterxml.jackson.core.JsonParseException
import org.apache.spark.{SparkConf, SparkContext}
import org.json4s.{DefaultFormats, JValue}
import org.json4s.JsonAST.JObject
Expand Down Expand Up @@ -254,7 +255,35 @@ private class PythonInterpreter(process: Process, gatewayServer: GatewayServer,
stdin.flush()

Option(stdout.readLine()).map { case line =>
parse(line)
try {
parse(line)
}
catch {
// LIVY-322 - If a statement puts raw text in the stdout without fake_shell parsing it,
// retry the readLine up to 100 times in an attempt to find the next parsable line of JSON.
case e: JsonParseException => retryRead(100)
}
}
}

// LIVY-322 - Method for retrying reads from stdout if a JsonParseException is encountered
// due to non-JSON formatted text in the stdout. Accepts a param of the number of times
// to retry the read, and recurses until either it finds a parsable line of JSON, or
// exhausts the maximum level of retries. If the maximum number of retries is exhausted,
// an Exception is thrown.
private def retryRead(maxRetries: Int): JValue = {
if (maxRetries > 0) {
try {
parse(stdout.readLine())
} catch {
case e: JsonParseException =>
retryRead(maxRetries - 1)
}
}
else {
throw new Exception(
"Livy is unable to find valid JSON in the response from fake_shell. " +
"Please recreate the session.")
}
}

Expand Down