Skip to content

Get rid of Thread.name augmentation with CoroutineId when debug mode … #3678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions kotlinx-coroutines-core/jvm/src/CoroutineContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -284,27 +284,7 @@ private const val DEBUG_THREAD_NAME_SEPARATOR = " @"
@IgnoreJreRequirement // desugared hashcode implementation
internal data class CoroutineId(
val id: Long
) : ThreadContextElement<String>, AbstractCoroutineContextElement(CoroutineId) {
) : AbstractCoroutineContextElement(CoroutineId) {
companion object Key : CoroutineContext.Key<CoroutineId>
override fun toString(): String = "CoroutineId($id)"

override fun updateThreadContext(context: CoroutineContext): String {
val coroutineName = context[CoroutineName]?.name ?: "coroutine"
val currentThread = Thread.currentThread()
val oldName = currentThread.name
var lastIndex = oldName.lastIndexOf(DEBUG_THREAD_NAME_SEPARATOR)
if (lastIndex < 0) lastIndex = oldName.length
currentThread.name = buildString(lastIndex + coroutineName.length + 10) {
append(oldName.substring(0, lastIndex))
append(DEBUG_THREAD_NAME_SEPARATOR)
append(coroutineName)
append('#')
append(id)
}
return oldName
}

override fun restoreThreadContext(context: CoroutineContext, oldState: String) {
Thread.currentThread().name = oldState
}
}
7 changes: 5 additions & 2 deletions kotlinx-coroutines-core/jvm/src/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import kotlin.internal.InlineOnly
* ### Debugging facilities
*
* In debug mode every coroutine is assigned a unique consecutive identifier.
* Every thread that executes a coroutine has its name modified to include the name and identifier of
* the currently running coroutine.
*
* Enable debugging facilities with "`kotlinx.coroutines.debug`" ([DEBUG_PROPERTY_NAME]) system property,
* use the following values:
Expand All @@ -32,6 +30,11 @@ import kotlin.internal.InlineOnly
*
* Debugging facilities are implemented by [newCoroutineContext][CoroutineScope.newCoroutineContext] function that
* is used in all coroutine builders to create context of a new coroutine.
*
* ### Legacy note
*
* Previously, every thread that executed a coroutine had its name modified to include the name and identifier of the currently running coroutine.
* This behaviour was removed as part of #3677 and #2234.
*/
public const val DEBUG_PROPERTY_NAME: String = "kotlinx.coroutines.debug"

Expand Down