Skip to content

Commit

Permalink
Fix for the rider plugin hanging. When the error stream filled up wit…
Browse files Browse the repository at this point in the history
…hout being read it couldn't read from the input stream was waiting indefinitely (#958)
  • Loading branch information
belav authored Sep 16, 2023
1 parent 8504fa0 commit 85c6a68
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Src/CSharpier.Rider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# csharpier-rider Changelog

## [1.3.10]
- Read from error stream to prevent the plugin hanging when the csharpier process writes too much to the error stream.

## [1.3.9]
- Wait at most 3 seconds for csharpier to format otherwise consider it hung and restart it.

Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Rider/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pluginGroup = com.intellij.csharpier
pluginName = csharpier
# SemVer format -> https://semver.org
pluginVersion = 1.3.9
pluginVersion = 1.3.10

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@
import com.intellij.openapi.Disposable;
import com.intellij.openapi.diagnostic.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Disposable {
private final boolean useUtf8;
Expand All @@ -39,6 +31,12 @@ private void startProcess() {

this.stdin = new OutputStreamWriter(this.process.getOutputStream(), charset);
this.stdOut = new BufferedReader(new InputStreamReader(this.process.getInputStream(), charset));

// if we don't read the error stream, eventually too much is buffered on it and the plugin hangs
var errorGobbler = new StreamGobbler(this.process.getErrorStream());
errorGobbler.start();


} catch (Exception e) {
this.logger.error("error", e);
}
Expand Down Expand Up @@ -109,4 +107,22 @@ public void dispose() {
this.process.destroy();
}
}

private class StreamGobbler extends Thread {
InputStream inputStream;

private StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
}

@Override
public void run() {
try {
var streamReader = new InputStreamReader(this.inputStream);
var reader = new BufferedReader(streamReader);
while (reader.readLine() != null) {}
}
catch (IOException ioe) { }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.intellij.csharpier;

import com.intellij.application.Topics;
import com.intellij.ide.actions.SaveAllAction;
import com.intellij.ide.actions.SaveDocumentAction;
import com.intellij.openapi.actionSystem.AnAction;
Expand All @@ -15,11 +14,6 @@ public class ReformatWithCSharpierOnSave implements AnActionListener {

private final Logger logger = CSharpierLogger.getInstance();

public ReformatWithCSharpierOnSave() {
// TODO this is deprecated and should be switched to https://plugins.jetbrains.com/docs/intellij/messaging-infrastructure.html#subscribing-to-a-topic
Topics.subscribe(AnActionListener.TOPIC, null, this);
}

@Override
public void beforeActionPerformed(@NotNull AnAction action, @NotNull AnActionEvent event) {
if (action instanceof SaveDocumentAction) {
Expand Down
5 changes: 4 additions & 1 deletion Src/CSharpier.Rider/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<depends>com.intellij.modules.rider</depends>

<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="com.intellij.csharpier.ReformatWithCSharpierOnSave"/>
<projectService serviceImplementation="com.intellij.csharpier.CSharpierProcessProvider"/>
<projectService serviceImplementation="com.intellij.csharpier.FormattingService"/>
<projectService serviceImplementation="com.intellij.csharpier.CSharpierSettings"/>
Expand All @@ -26,4 +25,8 @@
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action>
</actions>
<applicationListeners>
<listener class="com.intellij.csharpier.ReformatWithCSharpierOnSave"
topic="com.intellij.openapi.actionSystem.ex.AnActionListener" />
</applicationListeners>
</idea-plugin>

0 comments on commit 85c6a68

Please sign in to comment.