Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rewind & Replay: Kotlin 2.2が変えるCoroutine デバッグ最前線

Avatar for Masayuki Suda Masayuki Suda
October 31, 2025
56

Rewind & Replay: Kotlin 2.2が変えるCoroutine デバッグ最前線

Kotlin Fest2025の登壇資料です。

Avatar for Masayuki Suda

Masayuki Suda

October 31, 2025
Tweet

Transcript

  1. ©2024 Theoria technologies All Rights Reserved. Rewind & Replay: Kotlin

    2.2 ͕ม͑ΔCoroutine σόοά࠷લઢ 2025 11/01
  2. ©2024 Theoria technologies All Rights Reserved. 2 ✓ Coroutineσόοά৽ػೳΛཧղ͢Δ ✓

    Before/AfterͰ՝୊ղܾΛମײ͢Δ ✓ ໌೔͔Β࢖͑Δ࣮ફతͳ஌ݟ ࠓ೔ͷΰʔϧ Section 1
  3. ©2024 Theoria technologies All Rights Reserved. 3 1. ΠϯτϩμΫγϣϯ 2.

    Coroutineσόοάͷʮҋʯ 3. ͳͥɺCoroutine͸Debug͕ࠔ೉ͳͷ͔ 4. Coroutineσόοάͷ5େվળ ໨࣍ Section 1
  4. ©2024 Theoria technologies All Rights Reserved. 4 ࣗݾ঺հ Section 1

    ςΦϦΞɾςΫϊϩδʔζגࣜձࣾ ɹɹMasayukiSuda ɹɹdaasuu
  5. ©2024 Theoria technologies All Rights Reserved. 5 ೝ஌঱ͱͷ޲͖߹͍ํΛɺ ςΫϊϩδʔͰม͍͑ͯ͘ɻ Our

    Mission ྫ͑͹ɺϦεΫʹૣظʹؾ͖ͮɺରԠͰ͖Δ࢓૊ΈΛͭ͘Δɻ ͨͱ͑ൃ঱ͯ͠΋ͦͷਓΒ͍͠฻Β͠Λࢧ͑ΔαʔϏεΛಧ͚Δɻ ͲͷΑ͏ͳεςʔδͰ΋ɺ͝ຊਓ΍͝Ո଒ɺؔΘΔ͢΂ͯͷਓɺ ͻͱΓͻͱΓ͕ࣗ෼ʹ߹ͬͨํ๏Ͱೝ஌঱ͱ޲͖߹͏͜ͱ͕Ͱ͖ΔΑ͏ʹɻ ͦͯ͠ɺ ਓੜͷ࠷ޙͷॠؒ·Ͱɺ ࣗ෼Β͘͠"ੜ͖͖Δ"͜ͱ͕Ͱ͖ΔࣾձΛɺ ࣮ݱ͢ΔͨΊʹɻ
  6. ©2024 Theoria technologies All Rights Reserved. 6 ೝ஌঱ϓϥοτϑΥʔϜͷίϯηϓτϝΠΩϯά͔ΒιϦϡʔγϣϯ։ൃɺ σϦόϦʔͷ֦େΛਪਐ͢Δɻ ITػೳձࣾͰ͸ͳ͘ɺࣄۀձࣾͱͯ͠ΤʔβΠͱڞ૑͢Δɻ

    Co-Creating with Eisai ҙࢥܾఆ ࣄۀਪਐ ࣗࣾ։ൃ ҩྍ࿈ܞ ઓུࡦఆ ࣗࣾ։ൃϓϩμΫτʹΑΔՁ஋ఏڙ ౤ࢿ ࣄۀ΁ͷ౤ࢿ ࣏ݧɾίϗʔτσʔλ΁ͷΞΫηε ωοτϫʔΫɾਓࡒఏڙ
  7. ©2024 Theoria technologies All Rights Reserved. 8 ͋ͳͨ΋ܦݧͨ͠͸ͣ Section 2

    ʮͳͥ͜ͷCoroutine͕ࢭ·ͬͯΔʁʯ ʮͲ͜Ͱsuspendͨ͠ʁʯ ʮStack Trace్͕੾ΕͯΔ…ʯ ʮม਺ͷ஋͕ݟ͑ͳ͍…ʯ
  8. ©2024 Theoria technologies All Rights Reserved. 9 ैདྷͷCoroutineσόοάͷ՝୊ Section 2

    ❌ Stack Traceͷ෼அ ❌ ϩʔΧϧม਺ͷফࣦ ❌ εςοϓ࣮ߦͷෆ҆ఆ͞ ❌ ଟ਺ͷCoroutineͷঢ়ଶݕࠪͷࠔ೉͞
  9. ©2024 Theoria technologies All Rights Reserved. 19 ՝୊3: εςοϓ࣮ߦͷෆ҆ఆ͞ Section

    2 ϒϨʔΫϙΠϯτΛઃఆ Step Over/Into Λ࣮ߦ 👉 ҙਤͤͣผͷCoroutineʹॲཧ͕ඈͿ 👉 Ͳ͜Λ࣮ߦ͍ͯ͠Δ͔෼͔Βͳ͘ͳΔ
  10. ©2024 Theoria technologies All Rights Reserved. 23 ՝୊4: ଟ਺ͷCoroutineͷঢ়ଶݕࠪͷࠔ೉͞ Section

    2 Coroutine͸ܰྔͳͨΊଟ਺ੜ੒͞Ε͕ͪ ͔͠͠ 👉 ଟ͘ͷCoroutineঢ়ଶݕ͕ࠪࠔ೉ 👉 ͦΕͧΕͷCoroutineͷ໾ׂͷ೺Ѳ͕ࠔ೉
  11. ©2024 Theoria technologies All Rights Reserved. 25 σόοάͷݱ࣮ Section 2

    ݁Ռ: • printlnσόοάʹཔΔ • ϩάΛେྔʹ࢓ࠐΉ • ࠶ݱʹ͕͔͔࣌ؒΔ • ຊ൪؀ڥͰͷτϥϒϧγϡʔτࠔ೉
  12. ©2024 Theoria technologies All Rights Reserved. 28 ίϯύΠϥͷ಺෦ॲཧ Section 3

    - suspend ͸“ෳ਺ճεϦʔϓ/΢ΣΠΫΞοϓՄೳ”Ͱ͋Δ͜ͱΛίϯύΠϥʹ఻͑Δम০ࢠ - ͢΂ͯͷ suspend ؔ਺ʹ͸ίϯύΠϥ͕Ӆ͠Ҿ਺ `Continuation` Λ෇༩ - Continuation ͸தஅ఺ͷ“ଓ͖”Λදݱ͠ɼresume() Ͱ࣮ߦΛ࠶։ - Continuation ͸“ϩʔΧϧม਺ͷอଘྖҬ”ͱͯ͠΋࢖ΘΕΔ
  13. ©2024 Theoria technologies All Rights Reserved. 29 Continuationͱ͸Կ͔ Section 3

    - தஅ͞Ε࣮ͨߦϑϩʔͷ“εφοϓγϣοτ” - resume() ݺͼग़͠Ͱͦͷଓ͖͔Β࠶։ - νΣʔϯԽ͞Εɺݺͼग़ؔ͠܎ͷ෮ݩʹ΋ؔ༩ - ϩʔΧϧม਺ͷ஋Λอ࣋͢ΔͨΊͷετϨʔδ
  14. ©2024 Theoria technologies All Rights Reserved. 30 ؔ਺͸εςʔτϚγϯʹม׵͞ΕΔ Section 3

    - suspend ؔ਺͸“Կ౓΋ى͖Δ”Մೳੑ͕͋ΔͨΊɺίϯύΠϥ͕εςʔτϚγϯ΁ม׵ - ؔ਺ͷ։࢝/֤αεϖϯγϣϯϙΠϯτޙʹϥϕϧʢstateʣΛ෇༩ - ϥϕϧʹԠͯ͡ॲཧϒϩοΫΛ੾ସ͑Δ࣮૷΁มܗ
  15. ©2024 Theoria technologies All Rights Reserved. 31 Code sample Section

    3 suspend fun describe(identifier: String): String { val description = fetchDescription(identifier) val enhancedDescription = enhanceDescription(description) return enhancedDescription } suspend fun fetchDescription(identifier: String): String {} suspend fun enhanceDescription(description: String): String {}
  16. ©2024 Theoria technologies All Rights Reserved. 32 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { }
  17. ©2024 Theoria technologies All Rights Reserved. 33 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { val description = fetchDescription(identifier) val enhancedDescription = enhanceDescription(description) return enhancedDescription }
  18. ©2024 Theoria technologies All Rights Reserved. 34 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { // label 0 -> first execution val description = fetchDescription(identifier) val enhancedDescription = enhanceDescription(description) return enhancedDescription }
  19. ©2024 Theoria technologies All Rights Reserved. 35 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { // label 0 -> first execution val description = fetchDescription(identifier) // label 1 -> resuming val enhancedDescription = enhanceDescription(description) return enhancedDescription }
  20. ©2024 Theoria technologies All Rights Reserved. 36 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { // label 0 -> first execution val description = fetchDescription(identifier) // label 1 -> resuming val enhancedDescription = enhanceDescription(description) // label 2 -> resuming and terminating return enhancedDescription }
  21. ©2024 Theoria technologies All Rights Reserved. 37 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { when (stateMachine.label) { 0 -> { // label 0 -> first execution val description = fetchDescription(identifier) return } 1 -> { // label 1 -> resuming val enhancedDescription = enhanceDescription(description) return } 2 -> { // label 2 -> resuming and terminating return enhancedDescription } } }
  22. ©2024 Theoria technologies All Rights Reserved. 38 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { when (stateMachine.label) { 0 -> { // label 0 -> first execution val description = fetchDescription(identifier) stateMachine.label = 1 return } 1 -> { // label 1 -> resuming val enhancedDescription = enhanceDescription(description) stateMachine.label = 2 return } 2 -> { // label 2 -> resuming and terminating return enhancedDescription } } }
  23. ©2024 Theoria technologies All Rights Reserved. 39 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { when (stateMachine.label) { 0 -> { // label 0 -> first execution val description = fetchDescription(identifier) stateMachine.label = 1 return } 1 -> { // label 1 -> resuming val enhancedDescription = enhanceDescription(description) stateMachine.label = 2 return } 2 -> { // label 2 -> resuming and terminating return enhancedDescription } } }
  24. ©2024 Theoria technologies All Rights Reserved. 40 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { when (stateMachine.label) { 0 -> { // label 0 -> first execution val description = fetchDescription(identifier) continuation.description = description continuation.identifier = null stateMachine.label = 1 return } 1 -> { // label 1 -> resuming val enhancedDescription = enhanceDescription(description) continuation.enhancedDescription = enhancedDescription continuation.description = null stateMachine.label = 2 return } 2 -> { // label 2 -> resuming and terminating return continuation.enhancedDescription } } }
  25. ©2024 Theoria technologies All Rights Reserved. 41 Code sample Section

    3 suspend fun describe(identifier: String, continuation: Continuation): String { when (stateMachine.label) { 0 -> { // label 0 -> first execution val description = fetchDescription(identifier) continuation.description = description continuation.identifier = null stateMachine.label = 1 return } 1 -> { // label 1 -> resuming val enhancedDescription = enhanceDescription(description) continuation.enhancedDescription = enhancedDescription continuation.description = null stateMachine.label = 2 return } 2 -> { // label 2 -> resuming and terminating return continuation.enhancedDescription } } }
  26. ©2024 Theoria technologies All Rights Reserved. 42 ͳͥϩʔΧϧม਺͕ফ͑Δͷ͔ʢLiveness Analysisʣ Section

    3 ίϯύΠϥ࠷దԽ -JWFOFTT"OBMZTJTʢੜଘੑ෼ੳʣͰlޙͷϒϩοΫͰ࢖ΘΕͳ͍zม਺Λݕग़ ࢖Θͳ͍ม਺͸$POUJOVBUJPO͔ΒψϧԽഉআʢσόοΨʹݟ͑ͳ͍ʣ ཧ༝ ϝϞϦϦʔΫ๷ࢭʢ௕࣌ؒεϦʔϓதʹڊେΦϒδΣΫτΛอ࣋͠ͳ͍ʣ ྫ5#ͷσʔλΛอ࣋ͨ͠ม਺͕εϦʔϓ͠ଓ͚ΔࣄଶΛආ͚Δ
  27. ©2024 Theoria technologies All Rights Reserved. 43 ࠷దԽΛ๷͙؆қࡦ: println Section

    3 ϙΠϯτ ม਺ΛQSJOUMO ʹ౉͢ͱl࢖༻͞ΕΔzͨΊɺ࠷దԽͷର৅͔Β֎Ε΍͍͢ σϝϦοτϩά͕૽͕͍͠ຊ൪ʹࠞೖ͠΍͍͢ύϑΥʔϚϯεӨڹ
  28. ©2024 Theoria technologies All Rights Reserved. 46 5େվળ Section 3

    ✅ ϩʔΧϧม਺อ࣋ ✅ Stack TraceҰ෦෮ݩ ✅ εςοϓ࣮ߦ ✅ Async Stack Trace ✅ Coroutine View ※Kotlin 2.2ɺIDEA 2025.1ͷ૊Έ߹ΘͤͰ࣮ݱ
  29. ©2024 Theoria technologies All Rights Reserved. 48 վળ2: Stack TraceҰ෦෮ݩ

    Section 4 Coroutine ScopeΛඈͼӽ͑ͨ৔߹ 👉 Ͳ͔͜Βݺ͹Ε͔ͨ௥੻Մೳ
  30. ©2024 Theoria technologies All Rights Reserved. 54 վળ2: Stack TraceҰ෦෮ݩ

    Section 4 Coroutine ScopeΛ௒͑ͳͯ͘΋෮ݩ͢Δํ๏͸ͳ͍ʁ
  31. ©2024 Theoria technologies All Rights Reserved. 56 վળ2: Stack Trace෮ݩ

    Section 4 Stack trace decoroutinator An open source library https://github.com/Anamorphosee/stacktrace-decoroutinator
  32. ©2024 Theoria technologies All Rights Reserved. 57 վળ2: Stack trace

    decoroutinator Section 4 - Coroutineͷ“Wake upʢ࠶։ʣ”࣮૷Λ Java AgentͰஔ׵ - ࣮ߦ࣌ʹCoroutineͷݺͼग़͠νΣʔϯʹରԠ͢Δϝιου܈Λੜ੒ - ૬ޓݺͼग़͠Ͱ“࣮ελοΫ”෩ͷ࿈ͳΓΛܗ੒ͯ͠ྫ֎ʹࠞࡏ
  33. ©2024 Theoria technologies All Rights Reserved. 58 վળ2: Stack trace

    decoroutinator Section 4 - Coroutine࠶։ͷλΠϛϯάͰ“decoroutination”༻ͷϑϨʔϜΛ஫ೖ - ੜ੒ϝιου͸“funA → funB → funC → ...”ͷΑ͏ʹ࿈࠯ - ྫ֎ൃੜ࣌ɺ௨ৗͷframeͱฒΜͰ“෮ݩ͞Εͨݺͼग़͠ݩ”͕stackʹදΕΔ
  34. ©2024 Theoria technologies All Rights Reserved. 65 վળ2: Stack trace

    decoroutinator Section 4 dependencies { implementation(“dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm:2.5.7") }
  35. ©2024 Theoria technologies All Rights Reserved. 68 վળ2: Stack trace

    decoroutinator Section 4 ஫ҙ఺: - ࣮ߦ࣌ͷΦʔόʔϔου͸খ͘͞ͳ͍ - ຊ൪ӡ༻Ͱ͸ඇਪ঑ɺσόοά༻్ʹݶఆ - ໰୊࠶ݱ΍ݪҼ௥੻ͷ“࠷ޙͷ੾Γࡳ”ͱͯ͠׆༻
  36. ©2024 Theoria technologies All Rights Reserved. 69 վળ3: Coroutine୯ҐͰͷεςοϓ࣮ߦ੍ޚ Section

    3 ಛఆͷCoroutine಺ͰσόοάΛܧଓ 👉 ผͷCoroutineʹඈ͹ͳ͍ 👉 suspendؔ਺΋εςοϓΦʔόʔՄೳ"
  37. ©2024 Theoria technologies All Rights Reserved. 71 վળ3: ରԠ͢Δσόοάૢ࡞ Section

    4 ͜ͷػೳ͕࣮૷͞Ε͍ͯΔૢ࡞: • Step Over • Step Out • Smart Step Into • Run-to-Cursor
  38. ©2024 Theoria technologies All Rights Reserved. 72 վળ4: Async Stack

    Trace Section 4 - IntelliJ IDEAʹ౥ࡌͷσόοΨػೳ - CoroutineΛؚΉ“ඇಉظλεΫ”ͷىݯʙݱࡏΛؔ࿈෇͚ͯදࣔ
  39. ©2024 Theoria technologies All Rights Reserved. 74 վળ5: Coroutine View

    Section 4 IntelliJ IDEA 2025.1ͷ৽͍͠πʔϧ΢Οϯυ΢ • શCoroutineͷঢ়ଶΛදࣔ • Suspend/Resumeཤྺ • ਌ࢠؔ܎ͷՄࢹԽ
  40. ©2024 Theoria technologies All Rights Reserved. 75 Coroutine ViewͷεΫϦʔϯγϣοτ Section

    4 σόοΨʔπʔϧ΢ΟϯυΡ಺ͷCoroutinesʹνΣοΫΛೖΕΔ
  41. ©2024 Theoria technologies All Rights Reserved. 83 Android ։ൃͷํ΁ Android

    StudioͰ͸😣😣😣 • Coroutine View • εςοϓ࣮ߦ • Async Stack Trace
  42. ©2024 Theoria technologies All Rights Reserved. 85 Special Thanks Debug

    Kotlin coroutines https://www.jetbrains.com/help/idea/debug-kotlin-coroutines.html Coroutine Debugging in IntelliJ IDEA | Alexey Merkulov https://www.youtube.com/watch?v=3vaAVtnrzAM Kotlin Coroutines - Debugging Tools Overview https://www.youtube.com/watch?v=InN1NzDNJrA Debugging the Future: Exploring Coroutine Debugger Tools | Nikita Nazarov https://www.youtube.com/watch?v=QomoVqZSXP0
  43. ©2024 Theoria technologies All Rights Reserved. 86 We are hiring

    ! ͋ͳͨͷٕज़Ͱೝ஌঱ʹؔΘΔਓʑͷະདྷΛม͑·͠ΐ͏