Skip to content

Commit 8321fc2

Browse files
authored
Fix problems with python execution (#1995)
1 parent bfc2de9 commit 8321fc2

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

utbot-python/src/main/kotlin/org/utbot/python/PythonEngine.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ class PythonEngine(
154154
pythonPath,
155155
until,
156156
{ constructEvaluationInput(it) },
157-
timeoutForRun.toInt()
158157
)
159158
} catch (_: TimeoutException) {
160159
return@flow

utbot-python/src/main/kotlin/org/utbot/python/evaluation/PythonCodeSocketExecutor.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,22 @@ class PythonCodeSocketExecutor(
7676
logger.info { "Send data error" }
7777
return parseExecutionResult(FailExecution("Send data error"))
7878
}
79-
val response = pythonWorker.receiveMessage()
80-
val executionResult = if (response == null) {
81-
logger.info { "Response error" }
82-
FailExecution("Execution result error")
83-
} else {
84-
ExecutionResultDeserializer.parseExecutionResult(response)
85-
?: error("Cannot parse execution result: $response")
79+
80+
val (status, response) = UtExecutorThread.run(pythonWorker, executionTimeout)
81+
return when (status) {
82+
UtExecutorThread.Status.TIMEOUT -> {
83+
PythonEvaluationTimeout()
84+
}
85+
86+
UtExecutorThread.Status.OK -> {
87+
val executionResult = response?.let {
88+
ExecutionResultDeserializer.parseExecutionResult(it)
89+
?: error("Cannot parse execution result: $it")
90+
} ?: FailExecution("Execution result error")
91+
92+
parseExecutionResult(executionResult)
93+
}
8694
}
87-
return parseExecutionResult(executionResult)
8895
}
8996

9097
private fun parseExecutionResult(executionResult: PythonExecutionResult): PythonEvaluationResult {

utbot-python/src/main/kotlin/org/utbot/python/evaluation/PythonWorkerManager.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class PythonWorkerManager(
1818
val pythonPath: String,
1919
val until: Long,
2020
val pythonCodeExecutorConstructor: (PythonWorker) -> PythonCodeExecutor,
21-
private val timeoutForRun: Int
2221
) {
2322
private val logfile = TemporaryFileManager.createTemporaryFile("","utbot_executor.log", "log", true)
2423

@@ -39,22 +38,21 @@ class PythonWorkerManager(
3938
"localhost",
4039
serverSocket.localPort.toString(),
4140
"--logfile", logfile.absolutePath,
42-
"--loglevel", "INFO", // "DEBUG", "INFO", "ERROR"
41+
"--loglevel", "INFO", // "DEBUG", "INFO", "WARNING", "ERROR"
4342
))
4443
timeout = max(until - processStartTime, 0)
4544
workerSocket = try {
4645
serverSocket.soTimeout = timeout.toInt()
4746
serverSocket.accept()
4847
} catch (e: SocketTimeoutException) {
49-
timeout = max(until - processStartTime, 0)
50-
val result = getResult(process, timeout)
51-
logger.info("utbot_executor exit value: ${result.exitValue}. stderr: ${result.stderr}.")
48+
val result = getResult(process, max(until - processStartTime, 0))
49+
logger.info("utbot_executor exit value: ${result.exitValue}. stderr: ${result.stderr}, stdout: ${result.stdout}.")
5250
process.destroy()
5351
throw TimeoutException("Worker not connected")
5452
}
5553
logger.debug { "Worker connected successfully" }
5654

57-
workerSocket.soTimeout = timeoutForRun // TODO: maybe +eps for serialization/deserialization?
55+
// workerSocket.soTimeout = timeoutForRun // TODO: maybe +eps for serialization/deserialization?
5856
val pythonWorker = PythonWorker(workerSocket)
5957
codeExecutor = pythonCodeExecutorConstructor(pythonWorker)
6058
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.utbot.python.evaluation
2+
3+
class UtExecutorThread : Thread() {
4+
override fun run() {
5+
response = pythonWorker?.receiveMessage()
6+
}
7+
8+
enum class Status {
9+
TIMEOUT,
10+
OK,
11+
}
12+
13+
companion object {
14+
var pythonWorker: PythonWorker? = null
15+
var response: String? = null
16+
17+
fun run(worker: PythonWorker, executionTimeout: Long): Pair<Status, String?> {
18+
pythonWorker = worker
19+
response = null
20+
val thread = UtExecutorThread()
21+
thread.start()
22+
// Wait for the thread to finish
23+
val finishTime = System.currentTimeMillis() + executionTimeout
24+
while (thread.isAlive && System.currentTimeMillis() < finishTime) {
25+
sleep(1)
26+
}
27+
val status = if (thread.isAlive) {
28+
thread.interrupt()
29+
Status.TIMEOUT
30+
} else {
31+
Status.OK
32+
}
33+
return status to response
34+
}
35+
}
36+
}

utbot-python/src/main/kotlin/org/utbot/python/utils/RequirementsUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package org.utbot.python.utils
33
object RequirementsUtils {
44
val requirements: List<String> = listOf(
55
"mypy==1.0.0",
6-
"coverage==6.5.0",
7-
"utbot-executor==1.2.0",
6+
"utbot-executor==1.3.1",
87
"utbot-mypy-runner==0.2.8",
98
)
109

@@ -26,6 +25,7 @@ object RequirementsUtils {
2625
requirementsScript.path
2726
) + requirementList
2827
)
28+
requirementsScript.delete()
2929
return result.exitValue == 0
3030
}
3131

0 commit comments

Comments
 (0)