-
-
Save cichli/54d91face43893f3fbcc4246c76228e9 to your computer and use it in GitHub Desktop.
POC: stop threads using JDWP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nrepl; | |
public class EvaluationInterrupted extends Throwable { | |
public static EvaluationInterrupted t = new EvaluationInterrupted(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns nrepl.middleware.stop-thread-poc | |
(:import | |
(com.sun.jdi Bootstrap | |
ClassType | |
Field | |
ObjectReference | |
ThreadReference | |
VirtualMachine | |
VirtualMachineManager) | |
(com.sun.jdi.connect AttachingConnector Connector$Argument) | |
(java.lang ProcessHandle) | |
(nrepl EvaluationInterrupted))) | |
;; N.B. JVM must be started with the following arguments: | |
;; -agentlib:jdwp=transport=dt_socket,server=y,suspend=n | |
;; -Djdk.attach.allowAttachSelf | |
(defn ^VirtualMachine connect! [^VirtualMachineManager manager] | |
(let [^AttachingConnector connector (first (.attachingConnectors manager)) | |
args (.defaultArguments connector) | |
pid (str (.pid (ProcessHandle/current)))] | |
(.setValue ^Connector$Argument (get args "pid") pid) | |
(.attach connector args))) | |
(defn ^VirtualMachine vm [] | |
(let [manager (Bootstrap/virtualMachineManager)] | |
(or (first (.connectedVirtualMachines manager)) | |
(connect! manager)))) | |
(defn ^ThreadReference thread-ref [^Thread thread] | |
(first (filter #(= (.getName thread) (.name ^ThreadReference %)) | |
(.allThreads (vm))))) | |
(defn ^ObjectReference err-ref [] | |
(let [[^ClassType clazz] (.classesByName (vm) "nrepl.EvaluationInterrupted") | |
^Field field (.fieldByName clazz "t")] | |
(.getValue clazz field))) | |
(defn stop! [^Thread thread] | |
(.stop (thread-ref thread) (err-ref))) |
Author
cichli
commented
May 7, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment