Skip to content

Commit b31d09a

Browse files
committed
fix process exit handler
1 parent 1de2793 commit b31d09a

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/main/kotlin/com/wsl/symlinks/vfs/WslVirtualFileSystem.kt

+21-18
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ val myResourceLock = ReentrantLock()
6161
class WslSymlinksProvider(distro: String) {
6262
val LOGGER = Logger.getInstance(WslSymlinksProvider::class.java)
6363

64-
private var process: Process
65-
private var processReader: BufferedReader
66-
private var processWriter: BufferedWriter
64+
private var process: Process? = null
65+
private var processReader: BufferedReader? = null
66+
private var processWriter: BufferedWriter? = null
6767
private val queue: LinkedBlockingQueue<AsyncValue> = LinkedBlockingQueue()
6868
private val mapped: SynchronizedMap<String, AsyncValue> = SynchronizedMap()
6969

@@ -93,28 +93,28 @@ class WslSymlinksProvider(distro: String) {
9393
val location = "\\\\wsl.localhost\\$distro\\var\\tmp\\intellij-idea-wsl-symlinks.sh"
9494
File(location).writeText(bash)
9595
val builder = ProcessBuilder("wsl.exe", "-d", distro, "-e", "bash", "//var/tmp/intellij-idea-wsl-symlinks.sh")
96-
val process = builder.start()
97-
this.process = process
9896

99-
val stdin: OutputStream = process.outputStream // <- Eh?
100-
val stdout: InputStream = process.inputStream
10197

102-
val reader = BufferedReader(InputStreamReader(stdout))
103-
val writer = BufferedWriter(OutputStreamWriter(stdin))
104-
this.processReader = reader;
105-
this.processWriter = writer;
106-
107-
process.onExit().whenComplete { t, u ->
108-
LOGGER.error("process did exit", u)
109-
this.process = builder.start()
98+
fun setupProcess() {
99+
val process = builder.start()
100+
this.process = process
110101
this.processReader = BufferedReader(InputStreamReader(process.inputStream))
111102
this.processWriter = BufferedWriter(OutputStreamWriter(process.outputStream));
103+
process.onExit().whenComplete { t, u ->
104+
LOGGER.error("process did exit ${u?.message ?: "no-reason"}")
105+
setupProcess()
106+
}
112107
}
113108

109+
setupProcess()
110+
114111
thread {
115112
while (true) {
116113
try {
117-
val line = this.processReader.readLine()
114+
if (this.processReader == null) {
115+
Thread.sleep(100)
116+
}
117+
val line = this.processReader!!.readLine()
118118
val (id, answer) = line.split(";")
119119
val a = mapped[id]!!
120120
a.value = answer
@@ -131,11 +131,14 @@ class WslSymlinksProvider(distro: String) {
131131
thread {
132132
while (true) {
133133
try {
134+
if (this.processWriter == null) {
135+
Thread.sleep(100)
136+
}
134137
val a = this.queue.take()
135138
if (a.value != null) continue
136139
mapped[a.id] = a
137-
this.processWriter.write(a.request!!)
138-
this.processWriter.flush()
140+
this.processWriter!!.write(a.request!!)
141+
this.processWriter!!.flush()
139142
} catch (e: Exception) {
140143
LOGGER.error("failed to write", e)
141144
}

src/main/resources/files.sh

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
while IFS='$\n' read line
2-
do
3-
IFS=';'
4-
read -ra command <<< "$line"
5-
id=${command[0]}
6-
type=${command[1]}
7-
value=${command[2]}
1+
while IFS='$\n' read line; do
2+
IFS=';';
3+
read -ra command <<< "$line";
4+
id=${command[0]};
5+
type=${command[1]};
6+
value=${command[2]};
87
if [ "$type" == "is-symlink" ]; then
9-
(test -L "$value") && echo "$id;true" || echo "$id;false"
8+
(test -L "$value") && echo "$id;true" || echo "$id;false";
109
fi
1110
if [ "$type" == "read-symlink" ]; then
12-
link=$(readlink -f "$value")
13-
echo "$id;$link"
11+
link=$(readlink -f "$value");
12+
echo "$id;$link";
1413
fi
1514
done

0 commit comments

Comments
 (0)