Skip to content

Commit

Permalink
Simplify JobNode.toString to reduce code and avoid potential StackOve…
Browse files Browse the repository at this point in the history
…rflow (#2377)

Fixes #2371
  • Loading branch information
elizarov committed Nov 14, 2020
1 parent addff4b commit 37b95a9
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 14 deletions.
13 changes: 1 addition & 12 deletions kotlinx-coroutines-core/common/src/JobSupport.kt
Expand Up @@ -1151,8 +1151,6 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
override fun invoke(cause: Throwable?) {
parent.continueCompleting(state, child, proposedUpdate)
}
override fun toString(): String =
"ChildCompletion[$child, $proposedUpdate]"
}

private class AwaitContinuation<T>(
Expand Down Expand Up @@ -1350,6 +1348,7 @@ internal abstract class JobNode<out J : Job>(
override val isActive: Boolean get() = true
override val list: NodeList? get() = null
override fun dispose() = (job as JobSupport).removeNode(this)
override fun toString() = "$classSimpleName@$hexAddress[job@${job.hexAddress}]"
}

internal class NodeList : LockFreeLinkedListHead(), Incomplete {
Expand Down Expand Up @@ -1384,15 +1383,13 @@ private class InvokeOnCompletion(
private val handler: CompletionHandler
) : JobNode<Job>(job) {
override fun invoke(cause: Throwable?) = handler.invoke(cause)
override fun toString() = "InvokeOnCompletion[$classSimpleName@$hexAddress]"
}

private class ResumeOnCompletion(
job: Job,
private val continuation: Continuation<Unit>
) : JobNode<Job>(job) {
override fun invoke(cause: Throwable?) = continuation.resume(Unit)
override fun toString() = "ResumeOnCompletion[$continuation]"
}

private class ResumeAwaitOnCompletion<T>(
Expand All @@ -1411,15 +1408,13 @@ private class ResumeAwaitOnCompletion<T>(
continuation.resume(state.unboxState() as T)
}
}
override fun toString() = "ResumeAwaitOnCompletion[$continuation]"
}

internal class DisposeOnCompletion(
job: Job,
private val handle: DisposableHandle
) : JobNode<Job>(job) {
override fun invoke(cause: Throwable?) = handle.dispose()
override fun toString(): String = "DisposeOnCompletion[$handle]"
}

private class SelectJoinOnCompletion<R>(
Expand All @@ -1431,7 +1426,6 @@ private class SelectJoinOnCompletion<R>(
if (select.trySelect())
block.startCoroutineCancellable(select.completion)
}
override fun toString(): String = "SelectJoinOnCompletion[$select]"
}

private class SelectAwaitOnCompletion<T, R>(
Expand All @@ -1443,7 +1437,6 @@ private class SelectAwaitOnCompletion<T, R>(
if (select.trySelect())
job.selectAwaitCompletion(select, block)
}
override fun toString(): String = "SelectAwaitOnCompletion[$select]"
}

// -------- invokeOnCancellation nodes
Expand All @@ -1463,7 +1456,6 @@ private class InvokeOnCancelling(
override fun invoke(cause: Throwable?) {
if (_invoked.compareAndSet(0, 1)) handler.invoke(cause)
}
override fun toString() = "InvokeOnCancelling[$classSimpleName@$hexAddress]"
}

internal class ChildHandleNode(
Expand All @@ -1472,7 +1464,6 @@ internal class ChildHandleNode(
) : JobCancellingNode<JobSupport>(parent), ChildHandle {
override fun invoke(cause: Throwable?) = childJob.parentCancelled(job)
override fun childCancelled(cause: Throwable): Boolean = job.childCancelled(cause)
override fun toString(): String = "ChildHandle[$childJob]"
}

// Same as ChildHandleNode, but for cancellable continuation
Expand All @@ -1483,7 +1474,5 @@ internal class ChildContinuation(
override fun invoke(cause: Throwable?) {
child.parentCancelled(child.getContinuationCancellationCause(job))
}
override fun toString(): String =
"ChildContinuation[$child]"
}

1 change: 0 additions & 1 deletion kotlinx-coroutines-core/common/src/selects/Select.kt
Expand Up @@ -339,7 +339,6 @@ internal class SelectBuilderImpl<in R>(
if (trySelect())
resumeSelectWithException(job.getCancellationException())
}
override fun toString(): String = "SelectOnCancelling[${this@SelectBuilderImpl}]"
}

@PublishedApi
Expand Down
1 change: 0 additions & 1 deletion kotlinx-coroutines-core/jvm/src/Future.kt
Expand Up @@ -41,7 +41,6 @@ private class CancelFutureOnCompletion(
// interruption flag and it will cause spurious failures elsewhere
future.cancel(false)
}
override fun toString() = "CancelFutureOnCompletion[$future]"
}

private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler() {
Expand Down

0 comments on commit 37b95a9

Please sign in to comment.