-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
Fix kpsewhich
timeout on Windows using Tex Live Full
#3727
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package nl.hannahsten.texifyidea.util | ||
|
||
import com.intellij.execution.ExecutionException | ||
import com.intellij.execution.configurations.GeneralCommandLine | ||
import com.intellij.util.io.awaitExit | ||
import kotlinx.coroutines.* | ||
import java.io.File | ||
import java.io.IOException | ||
import javax.swing.SwingUtilities | ||
|
||
data class CommandResult( | ||
val exitCode: Int, | ||
val standardOutput: String?, | ||
val errorOutput: String? | ||
) { | ||
|
||
val output: String? | ||
get() = if (standardOutput != null || errorOutput != null) (standardOutput ?: "") + (errorOutput ?: "") else null | ||
} | ||
|
||
/** | ||
* Run a command in the terminal in a non-blocking way. | ||
* | ||
* @param workingDirectory If provided, the process' working directory. | ||
* @param input If provided, this will be written to the process' input pipe. | ||
* @param discardOutput Whether to discard all command outputs (stdout, stderr) and only return its exit code. | ||
* @param returnExceptionMessageAsErrorOutput Whether to return exception messages as error output if exceptions are thrown. | ||
* @param timeout The timeout for execution. Does not stop reading the process' output as long as it is available. | ||
*/ | ||
suspend fun runCommandNonBlocking( | ||
vararg commands: String, | ||
workingDirectory: File? = null, | ||
input: String? = null, | ||
discardOutput: Boolean = false, | ||
returnExceptionMessageAsErrorOutput: Boolean = false, | ||
timeout: Long = 3 | ||
): CommandResult = withContext(Dispatchers.IO) { | ||
try { | ||
Log.debug("isEDT=${SwingUtilities.isEventDispatchThread()} Executing in ${workingDirectory ?: "current working directory"} ${GeneralCommandLine(*commands).commandLineString}") | ||
|
||
val processBuilder = GeneralCommandLine(*commands) | ||
.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE) | ||
.withWorkDirectory(workingDirectory) | ||
.toProcessBuilder() | ||
|
||
if (discardOutput) { | ||
processBuilder.redirectOutput(ProcessBuilder.Redirect.DISCARD) | ||
processBuilder.redirectError(ProcessBuilder.Redirect.DISCARD) | ||
} | ||
|
||
val process = processBuilder.start() | ||
|
||
process.outputWriter().use { if (input != null) it.write(input) } | ||
val output = if (!discardOutput) async { process.inputReader().use { it.readText() } } else null | ||
val error = if (!discardOutput) async { process.errorReader().use { it.readText() } } else null | ||
|
||
withTimeoutOrNull(1_000 * timeout) { | ||
process.awaitExit() | ||
} ?: run { | ||
process.destroy() | ||
Log.debug("${commands.firstOrNull()} destroyed after timeout $timeout seconds") | ||
} | ||
|
||
val result = CommandResult(process.awaitExit(), output?.await()?.trim(), error?.await()?.trim()) | ||
Log.debug("${commands.firstOrNull()} exited with ${result.exitCode} ${result.standardOutput?.take(100)} ${result.errorOutput?.take(100)}") | ||
|
||
return@withContext result | ||
} | ||
catch (e: IOException) { | ||
Log.debug(e.message ?: "Unknown IOException occurred") | ||
|
||
return@withContext CommandResult( | ||
-1, | ||
null, | ||
if (returnExceptionMessageAsErrorOutput) e.message else null | ||
) | ||
} | ||
catch (e: ExecutionException) { | ||
Log.debug(e.message ?: "Unknown ExecutionException occurred") | ||
|
||
return@withContext CommandResult( | ||
-1, | ||
null, | ||
if (returnExceptionMessageAsErrorOutput) e.message else null | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Run a command in the terminal. | ||
* | ||
* @return The output of the command or null if an exception was thrown. | ||
*/ | ||
fun runCommand(vararg commands: String, workingDirectory: File? = null, timeout: Long = 3): String? = | ||
runBlocking { | ||
|
||
runCommandNonBlocking(*commands, workingDirectory = workingDirectory, timeout = timeout).output | ||
} | ||
|
||
/** | ||
* See [runCommandNonBlocking]. | ||
* | ||
* @param returnExceptionMessage Whether to return exception messages as output if exceptions are thrown. | ||
* @param inputString If provided, this will be written to the process' input pipe. | ||
* @return Pair of output (stdout + stderr) to exit code. | ||
*/ | ||
fun runCommandWithExitCode( | ||
vararg commands: String, | ||
workingDirectory: File? = null, | ||
timeout: Long = 3, | ||
returnExceptionMessage: Boolean = false, | ||
discardOutput: Boolean = false, | ||
inputString: String = "" | ||
): Pair<String?, Int> = | ||
runBlocking { | ||
with( | ||
runCommandNonBlocking( | ||
*commands, | ||
workingDirectory = workingDirectory, | ||
timeout = timeout, | ||
returnExceptionMessageAsErrorOutput = returnExceptionMessage, | ||
discardOutput = discardOutput, | ||
input = inputString | ||
) | ||
) { | ||
Pair(output, exitCode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remove LatexPackageLocationCacheInitializer then it doesn't appear to be blocking the UI, at least it's running in a background thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea on how to test this.
Sadly I was now able to provoke a freezed UI. Steps to reproduce:
LatexPackageLocationCacheInitializer
.To make the effect more obvious, replace
runBlocking { fillCacheWithKpsewhich(project) }
withrunBlocking { delay(1_000) }
.I can try to move the execution in another thread, but if this works depends on where the freezing occurs. Maybe another option is just to remove this call to
fillCacheWithKpsewhich()
as the cache should be filled at startup andgetPackageLocation()
is called multiple times - at least if it returnsnull
. In either case I probably won't be able to evaluate this before next weekend.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, that does block the UI. But I don't understand, why is the UI not blocked if I put there
runBlocking { runCommandNonBlocking("sleep", "3") }
to simulate a long-running command?I'm also not quite sure why the UI is blocked if we run
runBlocking { delay(1_000) }
in a background thread. Documentation: https://plugins.jetbrains.com/docs/intellij/threading-model.html#avoiding-ui-freezesIt's true that for this particular case we can maybe circumvent the issue, but there are other places where we are running system calls and need to wait for the result to return it to IntelliJ api, and I don't know how to do that without blocking the thread (and thus the UI, apparently?). Another example is LatexDocumentationProvider, which can be triggered in the autocompletion popup. (If I put a delay in runCommandWithExitCode then UI will block)
In any case, that is out of scope for this PR, so I'll merge this already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you make sure the command is executed? I had to circumvent the if-clause to test this:
With this I was able to block the UI until timeout.
As to why it blocks the UI, I'm not really sure as the debugger implies that those are indeed run in separate threads.