Skip to content

Commit 4413a3b

Browse files
pfusikclaude
andcommitted
[lsp] Monitor if client process is alive.
#126 Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c7071c2 commit 4413a3b

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎editors/lsp/lsp.fu‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public abstract class FusionLanguageServer
2222
{
2323
protected abstract string() ReadStdin(int length);
2424

25+
protected abstract void watchClientProcess(int pid);
26+
2527
static int GetInt(JsonElement json) => Math.Truncate(json.GetDouble());
2628

2729
static void Respond(Dictionary<string(), JsonElement#> request, string resultOrError)
@@ -45,6 +47,9 @@ public abstract class FusionLanguageServer
4547
switch (method) {
4648
case "initialize":
4749
Respond(request, $"\"result\": {{ \"capabilities\": {{ \"positionEncoding\": \"utf-8\", \"textDocumentSync\": 1, \"hoverProvider\": true }}, \"serverInfo\": {{ \"name\": \"Fusion\", \"version\": \"3.3.7\" }} }}"); // TODO: version
50+
JsonElement pid = params["processId"];
51+
if (pid.IsNumber())
52+
watchClientProcess(GetInt(pid));
4853
break;
4954
case "shutdown":
5055
Respond(request, "\"result\": null");

‎editors/lsp/main.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,38 @@
1818
// You should have received a copy of the GNU General Public License
1919
// along with Fusion Transpiler. If not, see http://www.gnu.org/licenses/
2020

21+
#include <chrono>
22+
#include <cstdlib>
2123
#include <iostream>
24+
#include <thread>
2225
#ifdef _WIN32
2326
#include <fcntl.h>
2427
#include <io.h>
28+
#include <windows.h>
29+
#else
30+
#include <signal.h>
2531
#endif
2632

2733
#include "lsp.hpp"
2834

2935
class CppFusionLanguageServer : public FusionLanguageServer
3036
{
37+
static bool isProcessAlive(int pid)
38+
{
39+
#ifdef _WIN32
40+
HANDLE h = OpenProcess(SYNCHRONIZE, FALSE, pid);
41+
if (!h)
42+
return false;
43+
DWORD result = WaitForSingleObject(h, 0);
44+
CloseHandle(h);
45+
return result == WAIT_TIMEOUT;
46+
#else
47+
return kill(pid, 0) == 0;
48+
#endif
49+
}
50+
3151
protected:
52+
3253
std::string readStdin(int length) const override
3354
{
3455
std::string result(length, '\0');
@@ -37,6 +58,16 @@ class CppFusionLanguageServer : public FusionLanguageServer
3758
result.clear();
3859
return result;
3960
}
61+
62+
void watchClientProcess(int pid) const override
63+
{
64+
std::thread([pid]() {
65+
do
66+
std::this_thread::sleep_for(std::chrono::seconds(5));
67+
while (isProcessAlive(pid));
68+
std::exit(0);
69+
}).detach();
70+
}
4071
};
4172

4273
int main()

0 commit comments

Comments
 (0)