mirror of
https://github.com/square/okhttp.git
synced 2025-11-24 18:41:06 +03:00
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. https://github.com/square/okhttp/issues/5512
This commit is contained in:
@@ -98,7 +98,7 @@ import javax.net.ssl.X509TrustManager
|
|||||||
*/
|
*/
|
||||||
class MockWebServer : ExternalResource(), Closeable {
|
class MockWebServer : ExternalResource(), Closeable {
|
||||||
private val taskRunnerBackend = TaskRunner.RealBackend(
|
private val taskRunnerBackend = TaskRunner.RealBackend(
|
||||||
threadFactory("MockWebServer TaskRunner", true))
|
threadFactory("MockWebServer TaskRunner", daemon = false))
|
||||||
private val taskRunner = TaskRunner(taskRunnerBackend)
|
private val taskRunner = TaskRunner(taskRunnerBackend)
|
||||||
private val requestQueue = LinkedBlockingQueue<RecordedRequest>()
|
private val requestQueue = LinkedBlockingQueue<RecordedRequest>()
|
||||||
private val openClientSockets =
|
private val openClientSockets =
|
||||||
@@ -391,10 +391,10 @@ class MockWebServer : ExternalResource(), Closeable {
|
|||||||
|
|
||||||
taskRunner.newQueue().execute("MockWebServer $portField", cancelable = false) {
|
taskRunner.newQueue().execute("MockWebServer $portField", cancelable = false) {
|
||||||
try {
|
try {
|
||||||
logger.info("${this@MockWebServer} starting to accept connections")
|
logger.info("$this starting to accept connections")
|
||||||
acceptConnections()
|
acceptConnections()
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
logger.log(Level.WARNING, "${this@MockWebServer} failed unexpectedly", e)
|
logger.log(Level.WARNING, "$this failed unexpectedly", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release all sockets and all threads, even if any close fails.
|
// Release all sockets and all threads, even if any close fails.
|
||||||
@@ -468,10 +468,9 @@ class MockWebServer : ExternalResource(), Closeable {
|
|||||||
try {
|
try {
|
||||||
SocketHandler(raw).handle()
|
SocketHandler(raw).handle()
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
logger.info("${this@MockWebServer} connection from ${raw.inetAddress} failed: $e")
|
logger.info("$this connection from ${raw.inetAddress} failed: $e")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.log(Level.SEVERE,
|
logger.log(Level.SEVERE, "$this connection from ${raw.inetAddress} crashed", e)
|
||||||
"${this@MockWebServer} connection from ${raw.inetAddress} crashed", e)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package okhttp3.internal.concurrent
|
|||||||
import okhttp3.internal.addIfAbsent
|
import okhttp3.internal.addIfAbsent
|
||||||
import okhttp3.internal.assertThreadDoesntHoldLock
|
import okhttp3.internal.assertThreadDoesntHoldLock
|
||||||
import okhttp3.internal.assertThreadHoldsLock
|
import okhttp3.internal.assertThreadHoldsLock
|
||||||
|
import okhttp3.internal.concurrent.TaskRunner.Companion.INSTANCE
|
||||||
import okhttp3.internal.notify
|
import okhttp3.internal.notify
|
||||||
import okhttp3.internal.threadFactory
|
import okhttp3.internal.threadFactory
|
||||||
import java.util.concurrent.SynchronousQueue
|
import java.util.concurrent.SynchronousQueue
|
||||||
@@ -28,9 +29,8 @@ import java.util.concurrent.TimeUnit
|
|||||||
/**
|
/**
|
||||||
* A set of worker threads that are shared among a set of task queues.
|
* A set of worker threads that are shared among a set of task queues.
|
||||||
*
|
*
|
||||||
* The task runner is responsible for managing non-daemon threads. It keeps the process alive while
|
* Use [INSTANCE] for a task runner that uses daemon threads. There is not currently a shared
|
||||||
* user-visible (ie. non-daemon) tasks are scheduled, and allows the process to exit when only
|
* instance for non-daemon threads.
|
||||||
* housekeeping (ie. daemon) tasks are scheduled.
|
|
||||||
*
|
*
|
||||||
* The task runner is also responsible for releasing held threads when the library is unloaded.
|
* The task runner is also responsible for releasing held threads when the library is unloaded.
|
||||||
* This is for the benefit of container environments that implement code unloading.
|
* This is for the benefit of container environments that implement code unloading.
|
||||||
@@ -289,6 +289,6 @@ class TaskRunner(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmField
|
@JvmField
|
||||||
val INSTANCE = TaskRunner(RealBackend(threadFactory("OkHttp TaskRunner", true)))
|
val INSTANCE = TaskRunner(RealBackend(threadFactory("OkHttp TaskRunner", daemon = true)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user