From 220db5621396a02020ab44cdc16f3d35acdfd86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Ko=C5=A1=C4=8Dejev?= Date: Thu, 19 Nov 2026 21:12:11 +0100 Subject: [PATCH 1/6] refactor(daemon): run both native detach implementations through one function --- .../specificlanguages/mops/daemon/DaemonProcess.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt b/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt index 51314efb..0f359492 100644 --- a/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt +++ b/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt @@ -4,6 +4,7 @@ import com.sun.jna.Library import com.sun.jna.Native import com.sun.jna.Platform import com.sun.jna.win32.StdCallLibrary +import java.io.PrintStream import java.lang.foreign.Arena import java.lang.foreign.FunctionDescriptor import java.lang.foreign.Linker @@ -22,12 +23,20 @@ internal object DaemonProcess { val windows = System.getProperty("os.name").startsWith("Windows") // Each implementation is a separate class, so the JVM loads only the API that is used. if (Runtime.version().feature() >= FOREIGN_FUNCTION_API_JAVA_VERSION) { - ForeignFunctionDetach.detach(windows) + detach(System.err, "the Foreign Function API") { ForeignFunctionDetach.detach(windows) } } else { - JnaDetach.detach(windows) + val bootLibraryPath = System.getProperty("jna.boot.library.path") ?: "(not set)" + detach(System.err, "JNA (jna.boot.library.path=$bootLibraryPath)") { JnaDetach.detach(windows) } } } + /** + * Runs [detachNatively], which detaches through the native API that [api] names. Warnings go to [warnings]. + */ + internal fun detach(warnings: PrintStream, api: String, detachNatively: () -> Unit) { + detachNatively() + } + private const val FOREIGN_FUNCTION_API_JAVA_VERSION = 22 private const val ERROR_INVALID_PARAMETER = 87 From 75dfab0bf62c82925a0a50aca16a6d2ffd3c1960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Ko=C5=A1=C4=8Dejev?= Date: Thu, 19 Nov 2026 21:12:11 +0100 Subject: [PATCH 2/6] test(daemon): a detach whose native call cannot be set up leaves the daemon running --- .../mops/daemon/DaemonProcessTest.kt | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 daemon/src/test/kotlin/com/specificlanguages/mops/daemon/DaemonProcessTest.kt diff --git a/daemon/src/test/kotlin/com/specificlanguages/mops/daemon/DaemonProcessTest.kt b/daemon/src/test/kotlin/com/specificlanguages/mops/daemon/DaemonProcessTest.kt new file mode 100644 index 00000000..9216f44e --- /dev/null +++ b/daemon/src/test/kotlin/com/specificlanguages/mops/daemon/DaemonProcessTest.kt @@ -0,0 +1,48 @@ +package com.specificlanguages.mops.daemon + +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import kotlin.test.Test +import kotlin.test.assertContains +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class DaemonProcessTest { + + @Test + fun `a native library that does not load leaves the daemon attached with a warning`() { + val warnings = ByteArrayOutputStream() + + DaemonProcess.detach(PrintStream(warnings, true), "JNA (jna.boot.library.path=C:\\mps\\lib\\jna)") { + throw UnsatisfiedLinkError("Native library (com/sun/jna/win32-x86-64/jnidispatch.dll) not found in resource path") + } + + val warning = warnings.toString() + assertContains(warning, "did not detach") + assertContains(warning, "JNA (jna.boot.library.path=C:\\mps\\lib\\jna)") + assertContains(warning, "jnidispatch.dll) not found in resource path") + } + + @Test + fun `native access that is not enabled leaves the daemon attached with a warning`() { + val warnings = ByteArrayOutputStream() + + DaemonProcess.detach(PrintStream(warnings, true), "the Foreign Function API") { + throw IllegalCallerException("Illegal native access") + } + + assertContains(warnings.toString(), "did not detach") + } + + @Test + fun `a failing detach call still fails the daemon`() { + val warnings = ByteArrayOutputStream() + + assertFailsWith { + DaemonProcess.detach(PrintStream(warnings, true), "JNA (jna.boot.library.path=/mps/lib/jna)") { + error("Could not detach daemon session: errno 1") + } + } + assertEquals("", warnings.toString()) + } +} From 40d365789ca75eca7bede2871b5c46bf0015fcd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Ko=C5=A1=C4=8Dejev?= Date: Thu, 19 Nov 2026 21:13:19 +0100 Subject: [PATCH 3/6] fix(daemon): keep running when the native detach call cannot be set up --- .../mops/daemon/DaemonProcess.kt | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt b/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt index 0f359492..be06eca0 100644 --- a/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt +++ b/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt @@ -12,6 +12,11 @@ import java.lang.foreign.MemoryLayout import java.lang.foreign.SymbolLookup import java.lang.foreign.ValueLayout.JAVA_INT +/** + * Adapter. Detaches the daemon process from the console or process group that started it. + * + * Hides: which native API detaches a process on each operating system and Java version. + */ internal object DaemonProcess { /** * Detach from the launching session or console before publishing readiness. @@ -31,10 +36,33 @@ internal object DaemonProcess { } /** - * Runs [detachNatively], which detaches through the native API that [api] names. Warnings go to [warnings]. + * Runs [detachNatively], which detaches through the native API that [api] names. + * + * When the native call cannot be set up, the daemon keeps running without detaching and writes a warning to + * [warnings]. These failures are: + * - [LinkageError]: JNA cannot load its native library or its classes. + * - [IllegalCallerException]: native access is not enabled for the Foreign Function API. + * - [UnsupportedOperationException]: the Foreign Function API has no native linker for this platform. + * + * Any other failure propagates. */ internal fun detach(warnings: PrintStream, api: String, detachNatively: () -> Unit) { - detachNatively() + try { + detachNatively() + } catch (failure: LinkageError) { + warnNotDetached(warnings, api, failure) + } catch (failure: IllegalCallerException) { + warnNotDetached(warnings, api, failure) + } catch (failure: UnsupportedOperationException) { + warnNotDetached(warnings, api, failure) + } + } + + private fun warnNotDetached(warnings: PrintStream, api: String, failure: Throwable) { + warnings.println( + "Warning: the daemon did not detach from the console or process group that started it, so closing that " + + "console or ending that process group may stop the daemon. Detaching through $api failed: $failure" + ) } private const val FOREIGN_FUNCTION_API_JAVA_VERSION = 22 From 0c0c241c5b4eac5b7822cdb9b27ef20a2b3e9873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Ko=C5=A1=C4=8Dejev?= Date: Thu, 19 Nov 2026 21:15:58 +0100 Subject: [PATCH 4/6] docs(daemon): say when DaemonProcess stays attached and why it holds that decision --- .../mops/daemon/DaemonProcess.kt | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt b/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt index be06eca0..618f03d6 100644 --- a/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt +++ b/daemon/src/main/kotlin/com/specificlanguages/mops/daemon/DaemonProcess.kt @@ -16,10 +16,15 @@ import java.lang.foreign.ValueLayout.JAVA_INT * Adapter. Detaches the daemon process from the console or process group that started it. * * Hides: which native API detaches a process on each operating system and Java version. + * + * Exception to `adapter-translates-only`: this class also decides that a native call that cannot be set up leaves the + * daemon running without detaching. Which failures mean "cannot be set up" depends on the native APIs, which this class + * hides. */ internal object DaemonProcess { /** - * Detach from the launching session or console before publishing readiness. + * Detach from the launching session or console before publishing readiness. When the native call cannot be set up, + * the daemon stays attached and writes a warning to stderr. * * Java 22 and newer use the Foreign Function API. Older JVMs use JNA from the MPS boot classpath. Starting with * MPS 2026.3, JNA is no longer on the boot classpath; it is in the `com.intellij.modules.jna` plugin. @@ -36,7 +41,7 @@ internal object DaemonProcess { } /** - * Runs [detachNatively], which detaches through the native API that [api] names. + * Runs [detachNatively], which detaches through the native API that [apiDescription] names. * * When the native call cannot be set up, the daemon keeps running without detaching and writes a warning to * [warnings]. These failures are: @@ -46,22 +51,22 @@ internal object DaemonProcess { * * Any other failure propagates. */ - internal fun detach(warnings: PrintStream, api: String, detachNatively: () -> Unit) { + internal fun detach(warnings: PrintStream, apiDescription: String, detachNatively: () -> Unit) { try { detachNatively() } catch (failure: LinkageError) { - warnNotDetached(warnings, api, failure) + warnNotDetached(warnings, apiDescription, failure) } catch (failure: IllegalCallerException) { - warnNotDetached(warnings, api, failure) + warnNotDetached(warnings, apiDescription, failure) } catch (failure: UnsupportedOperationException) { - warnNotDetached(warnings, api, failure) + warnNotDetached(warnings, apiDescription, failure) } } - private fun warnNotDetached(warnings: PrintStream, api: String, failure: Throwable) { + private fun warnNotDetached(warnings: PrintStream, apiDescription: String, failure: Throwable) { warnings.println( "Warning: the daemon did not detach from the console or process group that started it, so closing that " + - "console or ending that process group may stop the daemon. Detaching through $api failed: $failure" + "console or ending that process group may stop the daemon. Detaching through $apiDescription failed: $failure" ) } From ee933dc4b83057db0ae20c7118172dd0b2e6f412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Ko=C5=A1=C4=8Dejev?= Date: Thu, 19 Nov 2026 21:17:07 +0100 Subject: [PATCH 5/6] docs(skills): say that a daemon that cannot detach keeps running attached --- skills/mops-daemon/SKILL.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/skills/mops-daemon/SKILL.md b/skills/mops-daemon/SKILL.md index 9ed211fd..e6727559 100644 --- a/skills/mops-daemon/SKILL.md +++ b/skills/mops-daemon/SKILL.md @@ -94,8 +94,10 @@ stops the running daemon, says so in one line on stderr, and starts one in the o Deleting a live daemon's record or cache does not stop it. Stale records are removed when the recorded process or socket is no longer reachable. Commands reach the daemon over a loopback socket with a per-daemon token. -Daemons detach from the launching command's session on Unix and from its console on Windows. Their stdout and stderr go -to the daemon log, and stdin is closed. Filesystem and network sandbox restrictions still apply. +Daemons detach from the launching command's session on Unix and from its console on Windows. If the native call for that +cannot be set up, the daemon keeps running attached and writes a warning to the daemon log; closing that console or +session may then stop it. Their stdout and stderr go to the daemon log, and stdin is closed. Filesystem and network +sandbox restrictions still apply. Initial startup, including opening the MPS project, allows 300 seconds. For a project that needs longer, set `MOPS_DAEMON_STARTUP_TIMEOUT_SECONDS` to a positive whole number of seconds. This controls the wait for the daemon's From fcacf82cffddf7c5e859ec3b576d54b7c4eedeed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Ko=C5=A1=C4=8Dejev?= Date: Thu, 19 Nov 2026 21:17:55 +0100 Subject: [PATCH 6/6] docs(skills): say where daemon output goes before the attached-daemon warning --- skills/mops-daemon/SKILL.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skills/mops-daemon/SKILL.md b/skills/mops-daemon/SKILL.md index e6727559..3ba28bd7 100644 --- a/skills/mops-daemon/SKILL.md +++ b/skills/mops-daemon/SKILL.md @@ -94,10 +94,10 @@ stops the running daemon, says so in one line on stderr, and starts one in the o Deleting a live daemon's record or cache does not stop it. Stale records are removed when the recorded process or socket is no longer reachable. Commands reach the daemon over a loopback socket with a per-daemon token. -Daemons detach from the launching command's session on Unix and from its console on Windows. If the native call for that -cannot be set up, the daemon keeps running attached and writes a warning to the daemon log; closing that console or -session may then stop it. Their stdout and stderr go to the daemon log, and stdin is closed. Filesystem and network -sandbox restrictions still apply. +Daemons detach from the launching command's session on Unix and from its console on Windows. Their stdout and stderr go +to the daemon log, and stdin is closed. If the native call for detaching cannot be set up, the daemon keeps running +attached and writes a warning to the daemon log; closing that console or session may then stop it. Filesystem and +network sandbox restrictions still apply. Initial startup, including opening the MPS project, allows 300 seconds. For a project that needs longer, set `MOPS_DAEMON_STARTUP_TIMEOUT_SECONDS` to a positive whole number of seconds. This controls the wait for the daemon's