Skip to content

Commit

Permalink
Use Job objects in lieu of a shutdown hook on Windows.
Browse files Browse the repository at this point in the history
Shutdown hooks have bad behavior on Windows. Using Windows Job objects
ensure child processes die when the parent process (the NuProcess
caller) dies, regardless of the circumstances that caused the JVM
running NuProcess died.
  • Loading branch information
doctorpangloss committed Sep 30, 2021
1 parent 95ce332 commit a484ea9
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ This property controls how long the processing thread(s) remains after the last
order to avoid the overhead of starting up another processing thread, if processes are frequently run it may be desirable
for the processing thread to remain (linger) for some amount of time (default 2500ms).

##### ``com.zaxxer.nuprocess.enableShutdownHook``
On Windows, this enables creating processes with job objects, which ensures they die when the parent process dies, in all
circumstances. On Linux and macOS, this creates a Java shutdown hook that quits created, running processes. This has the
same limitations as shutdown hooks, which may not run in certain circumstances like termination. The default value is "true".

#### Related Projects
Charles Duffy has developed a Clojure wrapper library [here](https://github.com/threatgrid/asynp).
Julien Viet has developed a Vert.x 3 library [here](https://github.com/vietj/vertx-childprocess).
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/zaxxer/nuprocess/windows/NuKernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public static native int ReadFile(HANDLE hFile, ByteBuffer lpBuffer, int nNumber
public static native int WriteFile(HANDLE hFile, ByteBuffer lpBuffer, int nNumberOfBytesToWrite, IntByReference lpNumberOfBytesWritten,
NuKernel32.OVERLAPPED lpOverlapped);

public static native HANDLE CreateJobObject(SECURITY_ATTRIBUTES attrs, String name);

public static native boolean SetInformationJobObject(HANDLE hJob, int JobObjectInfoClass, Pointer lpJobObjectInfo, int cbJobObjectInfoLength); // {return false;};

public static native boolean AssignProcessToJobObject(HANDLE hJob, HANDLE hProcess); // {return false;};

public static native boolean TerminateJobObject(HANDLE hJob, long uExitCode); // {return false;};

/**
* The OVERLAPPED structure contains information used in
* asynchronous (or overlapped) input and output (I/O).
Expand Down
Loading

0 comments on commit a484ea9

Please sign in to comment.