-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Create a new debugging page #4730
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
Open
daniCsorbaJB
wants to merge
4
commits into
master
Choose a base branch
from
debugging-doc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
066721f
feat: creating new coroutines debugging page
daniCsorbaJB b372d90
update: adding android limitations to the page
daniCsorbaJB 6854c0e
update: implementing review comments from Dima
daniCsorbaJB 9692cc7
chore: adding note to coroutines and channels tutorial
daniCsorbaJB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| The documentation has been moved to the [topics/debugging.md](topics/debugging.md). | ||
| The documentation has been moved to the [https://kotlinlang.org/docs/coroutines-debugging.html](https://kotlinlang.org/docs/coroutines-debugging.html) page. |
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,109 +1,3 @@ | ||
| **Table of contents** | ||
| The documentation has been moved to the [https://kotlinlang.org/docs/coroutines-debugging.html](https://kotlinlang.org/docs/coroutines-debugging.html) page. | ||
|
|
||
| <!--- TOC --> | ||
|
|
||
| * [Debugging coroutines](#debugging-coroutines) | ||
| * [Debug mode](#debug-mode) | ||
| * [Stacktrace recovery](#stacktrace-recovery) | ||
| * [Stacktrace recovery machinery](#stacktrace-recovery-machinery) | ||
| * [Debug agent](#debug-agent) | ||
| * [Android optimization](#android-optimization) | ||
|
|
||
| <!--- END --> | ||
|
|
||
| ## Debugging coroutines | ||
|
|
||
| Debugging asynchronous programs is challenging, because multiple concurrent coroutines are typically working at the same time. | ||
| To help with that, `kotlinx.coroutines` comes with additional features for debugging: debug mode, stacktrace recovery | ||
| and debug agent. | ||
| These debugging capabilities are available only on JVM platforms. | ||
|
|
||
| ## Debug mode | ||
|
|
||
| The first debugging feature of `kotlinx.coroutines` is debug mode. | ||
| It can be enabled either by setting system property [DEBUG_PROPERTY_NAME] or by running Java with enabled assertions (`-ea` flag). | ||
| The latter is helpful to have debug mode enabled by default in unit tests. | ||
|
|
||
| Debug mode attaches a unique [name][CoroutineName] to every launched coroutine. | ||
| Coroutine name can be seen in a regular Java debugger, | ||
| in a string representation of the coroutine or in the thread name executing named coroutine. | ||
| Overhead of this feature is negligible and it can be safely turned on by default to simplify logging and diagnostic. | ||
|
|
||
| ## Stacktrace recovery | ||
|
|
||
| Stacktrace recovery is another useful feature of debug mode. It is enabled by default in the debug mode, | ||
| but can be separately disabled by setting `kotlinx.coroutines.stacktrace.recovery` system property to `false`. | ||
|
|
||
| Stacktrace recovery tries to stitch asynchronous exception stacktrace with a stacktrace of the receiver by copying it, providing | ||
| not only information where an exception was thrown, but also where it was asynchronously rethrown or caught. | ||
|
|
||
| It is easy to demonstrate with actual stacktraces of the same program that awaits asynchronous operation in `main` function | ||
| (runnable code is [here](../../kotlinx-coroutines-debug/test/RecoveryExample.kt)): | ||
|
|
||
| | Without recovery | With recovery | | ||
| | - | - | | ||
| |  |  | | ||
|
|
||
| The only downside of this approach is losing referential transparency of the exception. | ||
|
|
||
| > Note that suppressed exceptions are not copied and are left intact in the cause | ||
| > in order to prevent cycles in the exceptions chain, obscure`[CIRCULAR REFERENCE]` messages | ||
| > and even [crashes](https://jira.qos.ch/browse/LOGBACK-1027) in some frameworks | ||
|
|
||
| ### Stacktrace recovery machinery | ||
|
|
||
| This section explains the inner mechanism of stacktrace recovery and can be skipped. | ||
|
|
||
| When an exception is rethrown between coroutines (e.g. through `withContext` or `Deferred.await` boundary), stacktrace recovery | ||
| machinery tries to create a copy of the original exception (with the original exception as the cause), then rewrite stacktrace | ||
| of the copy with coroutine-related stack frames (using [Throwable.setStackTrace](https://docs.oracle.com/javase/9/docs/api/java/lang/Throwable.html#setStackTrace-java.lang.StackTraceElement:A-)) | ||
| and then throws the resulting exception instead of the original one. | ||
|
|
||
| Exception copy logic is straightforward: | ||
| 1) If the exception class implements [CopyableThrowable], [CopyableThrowable.createCopy] is used. | ||
| `null` can be returned from `createCopy` to opt-out specific exception from being recovered. | ||
| 2) If the exception class has class-specific fields not inherited from Throwable, the exception is not copied. | ||
| 3) Otherwise, one of the public exception's constructor is invoked reflectively with an optional `initCause` call. | ||
| 4) If the reflective copy has a changed message (exception constructor passed a modified `message` parameter to the superclass), | ||
| the exception is not copied in order to preserve a human-readable message. [CopyableThrowable] does not have such a limitation | ||
| and allows the copy to have a `message` different from that of the original. | ||
|
|
||
| ## Debug agent | ||
|
|
||
| [kotlinx-coroutines-debug](../../kotlinx-coroutines-debug) module provides one of the most powerful debug capabilities in `kotlinx.coroutines`. | ||
|
|
||
| This is a separate module with a JVM agent that keeps track of all alive coroutines, introspects and dumps them similar to thread dump command, | ||
| additionally enhancing stacktraces with information where coroutine was created. | ||
|
|
||
| The full tutorial of how to use debug agent can be found in the corresponding [readme](../../kotlinx-coroutines-debug/README.md). | ||
|
|
||
| <!--- | ||
| Make an exception googlable | ||
| java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory; | ||
| at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm$ForLegacyVm.resolve(ByteBuddyAgent.java:1055) | ||
| at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm.resolve(ByteBuddyAgent.java:1038) | ||
| at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:374) | ||
| at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:342) | ||
| at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:328) | ||
| at kotlinx.coroutines.debug.internal.DebugProbesImpl.install(DebugProbesImpl.kt:39) | ||
| at kotlinx.coroutines.debug.DebugProbes.install(DebugProbes.kt:49) | ||
| --> | ||
|
|
||
| ## Android optimization | ||
|
|
||
| In optimized (release) builds with R8 version 1.6.0 or later both | ||
| [Debugging mode](debugging.md#debug-mode) and | ||
| [Stacktrace recovery](debugging.md#stacktrace-recovery) | ||
| are permanently turned off. | ||
| For more details see ["Optimization" section for Android](../../ui/kotlinx-coroutines-android/README.md#optimization). | ||
|
|
||
| <!--- MODULE kotlinx-coroutines-core --> | ||
| <!--- INDEX kotlinx.coroutines --> | ||
|
|
||
| [DEBUG_PROPERTY_NAME]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-d-e-b-u-g_-p-r-o-p-e-r-t-y_-n-a-m-e.html | ||
| [CoroutineName]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-name/index.html | ||
| [CopyableThrowable]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-copyable-throwable/index.html | ||
| [CopyableThrowable.createCopy]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-copyable-throwable/create-copy.html | ||
|
|
||
| <!--- MODULE kotlinx-coroutines-debug --> | ||
| <!--- END --> | ||
| To edit the documentation, open the [topics/coroutines-debugging.md](topics/coroutines-debugging.md) page. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.