Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ 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
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.
*
* 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.
Expand All @@ -22,12 +33,43 @@ 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 [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:
* - [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, apiDescription: String, detachNatively: () -> Unit) {
try {
detachNatively()
} catch (failure: LinkageError) {
warnNotDetached(warnings, apiDescription, failure)
} catch (failure: IllegalCallerException) {
warnNotDetached(warnings, apiDescription, failure)
} catch (failure: UnsupportedOperationException) {
warnNotDetached(warnings, apiDescription, failure)
}
}

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 $apiDescription failed: $failure"
)
}

private const val FOREIGN_FUNCTION_API_JAVA_VERSION = 22
private const val ERROR_INVALID_PARAMETER = 87

Expand Down
Original file line number Diff line number Diff line change
@@ -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<IllegalStateException> {
DaemonProcess.detach(PrintStream(warnings, true), "JNA (jna.boot.library.path=/mps/lib/jna)") {
error("Could not detach daemon session: errno 1")
}
}
assertEquals("", warnings.toString())
}
}
4 changes: 3 additions & 1 deletion skills/mops-daemon/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ Deleting a live daemon's record or cache does not stop it. Stale records are rem
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.
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
Expand Down
Loading