Skip to content

Commit 8633b64

Browse files
gselzerclaude
andcommitted
Fix image parameters, and orphaned servers
The tooling environment gets NumPy: script parameters typed as NumPy arrays (e.g. images) were unknown to the server until the script's own environment was built, so 'image.' offered nothing from it. jedi-language-server is started via a launcher that exits once this JVM is gone: the server does not notice by itself, and on Windows then loops, failing to write to the closed pipe, flooding its log (which now starts anew beyond 5 MB). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 68fc9e9 commit 8633b64

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/appose/python/ApposePythonCodeCompleter.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,8 @@ private static ResidentWorker toolingWorker(
642642
}
643643

644644
/**
645-
* The tooling environment: Python, Jedi, jedi-language-server and Appose;
646-
* and uv, to install packages for {@link DependencyOverlays}.
645+
* The tooling environment: Python, Jedi, jedi-language-server, NumPy and
646+
* Appose; and uv, to install packages for {@link DependencyOverlays}.
647647
*/
648648
static String toolingConfig() {
649649
return "[workspace]\n" + //
@@ -654,6 +654,9 @@ static String toolingConfig() {
654654
"[dependencies]\n" + //
655655
"python = \"3.12.*\"\n" + //
656656
"jedi = \">=0.19.2\"\n" + //
657+
// NB: Script parameters are typed as NumPy arrays (see
658+
// ApposePythonDialect): known even before a script's environment is.
659+
"numpy = \"*\"\n" + //
657660
"\n" + //
658661
"[pypi-dependencies]\n" + //
659662
"appose = \"*\"\n" + //

‎src/main/java/org/scijava/plugins/scripting/appose/python/completion/JediLanguageServerSpec.java‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@
4848
*/
4949
public class JediLanguageServerSpec implements LspServerSpec {
5050

51+
/**
52+
* Runs jedi-language-server, exiting once the given process (this JVM) is
53+
* gone. NB: The server does not notice by itself; on Windows, it then even
54+
* loops, failing to write to the closed pipe.
55+
*/
56+
public static final String LAUNCHER = String.join("\n", //
57+
"import os, sys, threading, time", //
58+
"parent = int(sys.argv.pop(1))", //
59+
"def watch():", //
60+
" if os.name == 'nt':", //
61+
" import ctypes", //
62+
" k = ctypes.windll.kernel32", //
63+
" h = k.OpenProcess(0x00100000, False, parent) # SYNCHRONIZE", //
64+
" if h: k.WaitForSingleObject(h, 0xFFFFFFFF)", //
65+
" else:", //
66+
" while os.getppid() == parent: time.sleep(2)", //
67+
" os._exit(0)", //
68+
"threading.Thread(target=watch, daemon=True).start()", //
69+
"from jedi_language_server.cli import cli", //
70+
"cli()");
71+
72+
/** How large the error log may grow before it is started anew, in bytes. */
73+
private static final long MAX_LOG = 5 * 1024 * 1024;
74+
5175
private final Callable<String> python;
5276
private final File workspace;
5377
private final File log;
@@ -88,8 +112,9 @@ public LspConnection start(final EnvironmentContext env)
88112
if (exe == null) throw new IOException(
89113
"No Python for jedi-language-server");
90114
log.getParentFile().mkdirs();
91-
final ProcessBuilder pb = new ProcessBuilder(exe, "-c",
92-
"from jedi_language_server.cli import cli; cli()");
115+
if (log.length() > MAX_LOG) log.delete();
116+
final ProcessBuilder pb = new ProcessBuilder(exe, "-c", LAUNCHER, String
117+
.valueOf(ProcessHandle.current().pid()));
93118
pb.redirectError(ProcessBuilder.Redirect.appendTo(log));
94119
return LspConnection.of(pb.start());
95120
}

‎src/test/java/org/scijava/plugins/scripting/appose/python/ApposePythonCompletionIntegrationTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ public void testDiagnostics() throws Exception {
190190
"(1 + 2)"), language, null)).get(1, TimeUnit.MINUTES).isEmpty());
191191
}
192192

193+
@Test
194+
public void testParameterBeforeEnvironment() throws Exception {
195+
// An image parameter is a NumPy array: known, even though the script's
196+
// environment is not built (here, it is not even declared).
197+
completeUntil("#@ net.imglib2.img.Img image\nx = 1\nimage.", null,
198+
"shape");
199+
}
200+
201+
@Test
202+
public void testServerOutlivesNoParent() throws Exception {
203+
// The server, once its parent (this JVM) is gone, must exit: here, its
204+
// parent is a process that already has.
205+
completeUntil("import json\njson.du", null, "dumps");
206+
final String python = ApposePythonCodeCompleter.pythonOnDisk(new File(
207+
org.apposed.appose.util.Environments.apposeEnvsDir(),
208+
ApposePythonCodeCompleter.TOOLING_ENV));
209+
final Process gone = new ProcessBuilder(python, "-c", "pass").start();
210+
gone.waitFor();
211+
final Process server = new ProcessBuilder(python, "-c",
212+
org.scijava.plugins.scripting.appose.python.completion.JediLanguageServerSpec.LAUNCHER,
213+
String.valueOf(gone.pid())).redirectErrorStream(true).start();
214+
final boolean exited = server.waitFor(30, TimeUnit.SECONDS);
215+
if (!exited) server.destroyForcibly();
216+
assertTrue(exited);
217+
}
218+
193219
@Test
194220
public void testNewDependency() throws Exception {
195221
// A script declaring a dependency that is installed nowhere: its

0 commit comments

Comments
 (0)