@@ -100,7 +100,7 @@ final class ProcessHandleImpl implements ProcessHandle {
100
100
ThreadFactory threadFactory = grimReaper -> {
101
101
Thread t = InnocuousThread .newSystemThread ("process reaper" , grimReaper ,
102
102
stackSize , Thread .MAX_PRIORITY );
103
- t . setDaemon ( true );
103
+ privilegedThreadSetDaemon ( t , true );
104
104
return t ;
105
105
};
106
106
@@ -115,6 +115,22 @@ private static class ExitCompletion extends CompletableFuture<Integer> {
115
115
}
116
116
}
117
117
118
+ @ SuppressWarnings ("removal" )
119
+ private static void privilegedThreadSetName (Thread thread , String name ) {
120
+ AccessController .doPrivileged ((PrivilegedAction <Void >) () -> {
121
+ thread .setName (name );
122
+ return null ;
123
+ });
124
+ }
125
+
126
+ @ SuppressWarnings ("removal" )
127
+ private static void privilegedThreadSetDaemon (Thread thread , boolean on ) {
128
+ AccessController .doPrivileged ((PrivilegedAction <Void >) () -> {
129
+ thread .setDaemon (on );
130
+ return null ;
131
+ });
132
+ }
133
+
118
134
/**
119
135
* Returns a CompletableFuture that completes with process exit status when
120
136
* the process completes.
@@ -140,8 +156,9 @@ static CompletableFuture<Integer> completion(long pid, boolean shouldReap) {
140
156
processReaperExecutor .execute (new Runnable () {
141
157
// Use inner class to avoid lambda stack overhead
142
158
public void run () {
143
- String threadName = Thread .currentThread ().getName ();
144
- Thread .currentThread ().setName ("process reaper (pid " + pid + ")" );
159
+ Thread t = Thread .currentThread ();
160
+ String threadName = t .getName ();
161
+ privilegedThreadSetName (t , "process reaper (pid " + pid + ")" );
145
162
try {
146
163
int exitValue = waitForProcessExit0 (pid , shouldReap );
147
164
if (exitValue == NOT_A_CHILD ) {
@@ -172,7 +189,7 @@ public void run() {
172
189
completions .remove (pid , newCompletion );
173
190
} finally {
174
191
// Restore thread name
175
- Thread . currentThread (). setName ( threadName );
192
+ privilegedThreadSetName ( t , threadName );
176
193
}
177
194
}
178
195
});
0 commit comments