Skip to content

Commit d26bfca

Browse files
committed
Don't use daemon threads in MockWebServer
This was a regression introduced with the TaskRunner changes. I couldn't find other places where daemon threads were likely to cause potential problems. #5512
1 parent 7e48705 commit d26bfca

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ import javax.net.ssl.X509TrustManager
9898
*/
9999
class MockWebServer : ExternalResource(), Closeable {
100100
private val taskRunnerBackend = TaskRunner.RealBackend(
101-
threadFactory("MockWebServer TaskRunner", true))
101+
threadFactory("MockWebServer TaskRunner", daemon = false))
102102
private val taskRunner = TaskRunner(taskRunnerBackend)
103103
private val requestQueue = LinkedBlockingQueue<RecordedRequest>()
104104
private val openClientSockets =
@@ -391,10 +391,10 @@ class MockWebServer : ExternalResource(), Closeable {
391391

392392
taskRunner.newQueue().execute("MockWebServer $portField", cancelable = false) {
393393
try {
394-
logger.info("${this@MockWebServer} starting to accept connections")
394+
logger.info("$this starting to accept connections")
395395
acceptConnections()
396396
} catch (e: Throwable) {
397-
logger.log(Level.WARNING, "${this@MockWebServer} failed unexpectedly", e)
397+
logger.log(Level.WARNING, "$this failed unexpectedly", e)
398398
}
399399

400400
// Release all sockets and all threads, even if any close fails.
@@ -468,10 +468,9 @@ class MockWebServer : ExternalResource(), Closeable {
468468
try {
469469
SocketHandler(raw).handle()
470470
} catch (e: IOException) {
471-
logger.info("${this@MockWebServer} connection from ${raw.inetAddress} failed: $e")
471+
logger.info("$this connection from ${raw.inetAddress} failed: $e")
472472
} catch (e: Exception) {
473-
logger.log(Level.SEVERE,
474-
"${this@MockWebServer} connection from ${raw.inetAddress} crashed", e)
473+
logger.log(Level.SEVERE, "$this connection from ${raw.inetAddress} crashed", e)
475474
}
476475
}
477476
}

okhttp/src/main/java/okhttp3/internal/concurrent/TaskRunner.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package okhttp3.internal.concurrent
1818
import okhttp3.internal.addIfAbsent
1919
import okhttp3.internal.assertThreadDoesntHoldLock
2020
import okhttp3.internal.assertThreadHoldsLock
21+
import okhttp3.internal.concurrent.TaskRunner.Companion.INSTANCE
2122
import okhttp3.internal.notify
2223
import okhttp3.internal.threadFactory
2324
import java.util.concurrent.SynchronousQueue
@@ -28,9 +29,8 @@ import java.util.concurrent.TimeUnit
2829
/**
2930
* A set of worker threads that are shared among a set of task queues.
3031
*
31-
* The task runner is responsible for managing non-daemon threads. It keeps the process alive while
32-
* user-visible (ie. non-daemon) tasks are scheduled, and allows the process to exit when only
33-
* housekeeping (ie. daemon) tasks are scheduled.
32+
* Use [INSTANCE] for a task runner that uses daemon threads. There is not currently a shared
33+
* instance for non-daemon threads.
3434
*
3535
* The task runner is also responsible for releasing held threads when the library is unloaded.
3636
* This is for the benefit of container environments that implement code unloading.
@@ -289,6 +289,6 @@ class TaskRunner(
289289

290290
companion object {
291291
@JvmField
292-
val INSTANCE = TaskRunner(RealBackend(threadFactory("OkHttp TaskRunner", true)))
292+
val INSTANCE = TaskRunner(RealBackend(threadFactory("OkHttp TaskRunner", daemon = true)))
293293
}
294294
}

0 commit comments

Comments
 (0)