-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Propagate exception to wasm-js and js in propagateExceptionFinalResort
#4472
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
murfel
wants to merge
9
commits into
develop
Choose a base branch
from
propagate-coroutine-exception-js-wasm
base: develop
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
9 commits
Select commit
Hold shift + click to select a range
51cde3a
add
murfel be4173a
add the last empty new line to each file
murfel 1f78da0
remove a comment
murfel 8d7c1ec
add comments, re-arrange code
murfel 4a7845d
simplify a function name
murfel 7dbce2c
rename test functions
murfel b21dd05
remove extra spaces in comments
murfel 485db2c
clean up handler in wasmJS code as well
murfel ed9d26b
add docs + fix articles nearby
murfel 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
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
9 changes: 4 additions & 5 deletions
9
kotlinx-coroutines-core/js/src/internal/CoroutineExceptionHandlerImpl.kt
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,8 +1,7 @@ | ||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlin.js.unsafeCast | ||
|
||
internal actual fun propagateExceptionFinalResort(exception: Throwable) { | ||
// log exception | ||
console.error(exception.toString()) | ||
} | ||
internal actual external interface JsAny | ||
|
||
internal actual fun Throwable.toJsException(): JsAny = this.unsafeCast<JsAny>() |
50 changes: 50 additions & 0 deletions
50
kotlinx-coroutines-core/js/test/PropagateExceptionFinalResortTest.kt
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package kotlinx.coroutines | ||
|
||
import kotlinx.coroutines.testing.* | ||
import kotlin.js.* | ||
import kotlin.test.* | ||
|
||
class PropagateExceptionFinalResortTest : TestBase() { | ||
@BeforeTest | ||
private fun removeListeners() { | ||
// Remove a Node.js's internal listener, which prints the exception to stdout. | ||
js(""" | ||
globalThis.originalListeners = process.listeners('uncaughtException'); | ||
process.removeAllListeners('uncaughtException'); | ||
""") | ||
} | ||
|
||
@AfterTest | ||
private fun restoreListeners() { | ||
js(""" | ||
if (globalThis.originalListeners) { | ||
process.removeAllListeners('uncaughtException'); | ||
globalThis.originalListeners.forEach(function(listener) { | ||
process.on('uncaughtException', listener); | ||
}); | ||
} | ||
""") | ||
} | ||
|
||
/* | ||
* Test that `propagateExceptionFinalResort` re-throws the exception on JS. | ||
* | ||
* It is checked by setting up an exception handler within JS. | ||
*/ | ||
@Test | ||
fun testPropagateExceptionFinalResortReThrowsOnNodeJS() = runTest { | ||
js(""" | ||
globalThis.exceptionCaught = false; | ||
process.on('uncaughtException', function(e) { | ||
globalThis.exceptionCaught = true; | ||
}); | ||
""") | ||
val job = GlobalScope.launch { | ||
throw IllegalStateException("My ISE") | ||
} | ||
job.join() | ||
delay(1) // Let the exception be re-thrown and handled. | ||
val exceptionCaught = js("globalThis.exceptionCaught") as Boolean | ||
assertTrue(exceptionCaught) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
kotlinx-coroutines-core/jsAndWasmJsShared/src/internal/CoroutineExceptionHandlerImpl.kt
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
|
||
internal expect interface JsAny | ||
|
||
internal expect fun Throwable.toJsException(): JsAny | ||
|
||
/* | ||
* Schedule an exception to be thrown inside JS or Wasm/JS event loop, | ||
* rather than in the current execution branch. | ||
*/ | ||
internal fun throwAsync(e: JsAny): Unit = js("setTimeout(function () { throw e }, 0)") | ||
dkhalanskyjb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
internal actual fun propagateExceptionFinalResort(exception: Throwable) { | ||
throwAsync(exception.toJsException()) | ||
} |
18 changes: 13 additions & 5 deletions
18
kotlinx-coroutines-core/wasmJs/src/internal/CoroutineExceptionHandlerImpl.kt
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,8 +1,16 @@ | ||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
internal actual typealias JsAny = kotlin.js.JsAny | ||
|
||
internal actual fun propagateExceptionFinalResort(exception: Throwable) { | ||
// log exception | ||
console.error(exception.toString()) | ||
} | ||
internal actual fun Throwable.toJsException(): JsAny = | ||
toJsError(message, this::class.simpleName, stackTraceToString()) | ||
|
||
internal fun toJsError(message: String?, className: String?, stack: String?): JsAny { | ||
js(""" | ||
const error = new Error(); | ||
error.message = message; | ||
error.name = className; | ||
error.stack = stack; | ||
return error; | ||
""") | ||
} |
49 changes: 49 additions & 0 deletions
49
kotlinx-coroutines-core/wasmJs/test/PropagateExceptionFinalResortTest.kt
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package kotlinx.coroutines | ||
|
||
import kotlinx.coroutines.testing.TestBase | ||
import kotlin.test.* | ||
|
||
class PropagateExceptionFinalResortTest : TestBase() { | ||
@BeforeTest | ||
private fun addUncaughtExceptionHandler() { | ||
addUncaughtExceptionHandlerHelper() | ||
} | ||
|
||
@AfterTest | ||
private fun removeHandler() { | ||
removeHandlerHelper() | ||
} | ||
|
||
/* | ||
* Test that `propagateExceptionFinalResort` re-throws the exception on Wasm/JS. | ||
* | ||
* It is checked by setting up an exception handler within Wasm/JS. | ||
*/ | ||
@Test | ||
fun testPropagateExceptionFinalResortReThrowsOnWasmJS() = runTest { | ||
val job = GlobalScope.launch { | ||
throw IllegalStateException("My ISE") | ||
} | ||
job.join() | ||
delay(1) // Let the exception be re-thrown and handled. | ||
assertTrue(exceptionCaught()) | ||
} | ||
} | ||
|
||
private fun addUncaughtExceptionHandlerHelper() { | ||
js(""" | ||
globalThis.exceptionCaught = false; | ||
globalThis.exceptionHandler = function(e) { | ||
globalThis.exceptionCaught = true; | ||
}; | ||
process.on('uncaughtException', globalThis.exceptionHandler); | ||
""") | ||
} | ||
|
||
private fun removeHandlerHelper() { | ||
js(""" | ||
process.removeListener('uncaughtException', globalThis.exceptionHandler); | ||
""") | ||
} | ||
|
||
private fun exceptionCaught(): Boolean = js("globalThis.exceptionCaught") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need some help with the wording, though. I'm unsure on how we spell Wasm/JS (because there's Wasm/WASI) (if Wasm/JS is ok, would the full spelling be Kotlin/Wasm/JS ? That doesn't look great)
Also, not sure saying "event loop" is a fair representation