Skip to content

Fix problems with python execution #1995

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

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
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 @@ -154,7 +154,6 @@ class PythonEngine(
pythonPath,
until,
{ constructEvaluationInput(it) },
timeoutForRun.toInt()
)
} catch (_: TimeoutException) {
return@flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,22 @@ class PythonCodeSocketExecutor(
logger.info { "Send data error" }
return parseExecutionResult(FailExecution("Send data error"))
}
val response = pythonWorker.receiveMessage()
val executionResult = if (response == null) {
logger.info { "Response error" }
FailExecution("Execution result error")
} else {
ExecutionResultDeserializer.parseExecutionResult(response)
?: error("Cannot parse execution result: $response")

val (status, response) = UtExecutorThread.run(pythonWorker, executionTimeout)
return when (status) {
UtExecutorThread.Status.TIMEOUT -> {
PythonEvaluationTimeout()
}

UtExecutorThread.Status.OK -> {
val executionResult = response?.let {
ExecutionResultDeserializer.parseExecutionResult(it)
?: error("Cannot parse execution result: $it")
} ?: FailExecution("Execution result error")

parseExecutionResult(executionResult)
}
}
return parseExecutionResult(executionResult)
}

private fun parseExecutionResult(executionResult: PythonExecutionResult): PythonEvaluationResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PythonWorkerManager(
val pythonPath: String,
val until: Long,
val pythonCodeExecutorConstructor: (PythonWorker) -> PythonCodeExecutor,
private val timeoutForRun: Int
) {
private val logfile = TemporaryFileManager.createTemporaryFile("","utbot_executor.log", "log", true)

Expand All @@ -39,22 +38,21 @@ class PythonWorkerManager(
"localhost",
serverSocket.localPort.toString(),
"--logfile", logfile.absolutePath,
"--loglevel", "INFO", // "DEBUG", "INFO", "ERROR"
"--loglevel", "INFO", // "DEBUG", "INFO", "WARNING", "ERROR"
))
timeout = max(until - processStartTime, 0)
workerSocket = try {
serverSocket.soTimeout = timeout.toInt()
serverSocket.accept()
} catch (e: SocketTimeoutException) {
timeout = max(until - processStartTime, 0)
val result = getResult(process, timeout)
logger.info("utbot_executor exit value: ${result.exitValue}. stderr: ${result.stderr}.")
val result = getResult(process, max(until - processStartTime, 0))
logger.info("utbot_executor exit value: ${result.exitValue}. stderr: ${result.stderr}, stdout: ${result.stdout}.")
process.destroy()
throw TimeoutException("Worker not connected")
}
logger.debug { "Worker connected successfully" }

workerSocket.soTimeout = timeoutForRun // TODO: maybe +eps for serialization/deserialization?
// workerSocket.soTimeout = timeoutForRun // TODO: maybe +eps for serialization/deserialization?
val pythonWorker = PythonWorker(workerSocket)
codeExecutor = pythonCodeExecutorConstructor(pythonWorker)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.utbot.python.evaluation

class UtExecutorThread : Thread() {
override fun run() {
response = pythonWorker?.receiveMessage()
}

enum class Status {
TIMEOUT,
OK,
}

companion object {
var pythonWorker: PythonWorker? = null
var response: String? = null

fun run(worker: PythonWorker, executionTimeout: Long): Pair<Status, String?> {
pythonWorker = worker
response = null
val thread = UtExecutorThread()
thread.start()
// Wait for the thread to finish
val finishTime = System.currentTimeMillis() + executionTimeout
while (thread.isAlive && System.currentTimeMillis() < finishTime) {
sleep(1)
}
val status = if (thread.isAlive) {
thread.interrupt()
Status.TIMEOUT
} else {
Status.OK
}
return status to response
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package org.utbot.python.utils
object RequirementsUtils {
val requirements: List<String> = listOf(
"mypy==1.0.0",
"coverage==6.5.0",
"utbot-executor==1.2.0",
"utbot-executor==1.3.1",
"utbot-mypy-runner==0.2.8",
)

Expand All @@ -26,6 +25,7 @@ object RequirementsUtils {
requirementsScript.path
) + requirementList
)
requirementsScript.delete()
return result.exitValue == 0
}

Expand Down