Repository navigation
feat(nav3): [Part 2] Add an argument source for encoding back stack entry properties via kotlinx.serialization #6751
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
0xadam-brown
wants to merge
1
commit into
feat/nav3-serial-name-mappers-8
Choose a base branch
from
feat/nav3-serial-name-mappers-argument-source
base: feat/nav3-serial-name-mappers-8
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
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
174 changes: 174 additions & 0 deletions
174
.../kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSource.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,174 @@ | ||
| package io.sentry.compose.navigation3.internal.backstack.kotlinx | ||
|
|
||
| import io.sentry.compose.navigation3.internal.common.ArgumentBudget | ||
| import io.sentry.compose.navigation3.internal.common.ArgumentSink | ||
| import io.sentry.compose.navigation3.internal.common.ArgumentSource | ||
| import io.sentry.compose.navigation3.internal.common.DropArgumentsException | ||
| import io.sentry.compose.navigation3.internal.common.HostArgumentSource | ||
| import io.sentry.compose.navigation3.internal.common.TreeArgumentSink | ||
| import io.sentry.compose.navigation3.internal.contract.ArgumentDropReason | ||
| import io.sentry.compose.navigation3.internal.contract.ArgumentDropReason.Companion.ARGUMENT_DROP_REASON_KEY | ||
| import io.sentry.util.ExceptionUtils | ||
| import kotlinx.serialization.KSerializer | ||
|
|
||
| /** | ||
| * The [ArgumentSource] for `BackStackEntryMapper.SerialNameAndProperties`. It's also the map of | ||
| * arguments it returns. | ||
| * | ||
| * Failures are reported to [onFailure]. The only exclusion is if an argument constraint is violated | ||
| * while writing to the `ArgumentSanitizer`'s sink; the sanitizer reports that instead, as those | ||
| * limits can depend on what fraction of the [ArgumentBudget] the update's other entries spent. | ||
| * | ||
| * **Lazy encoding** | ||
| * | ||
| * Encoding is lazy, so that arguments that are dropped or never read never run the entry's | ||
| * serializers. | ||
| * | ||
| * Once the arguments are read, [writeTo] writes the copy that read encoded, or drops the arguments | ||
| * for the reason its encoding failed, rather than encoding the entry again. Sentry then records | ||
| * what the host app read, even if its serializers would have encoded the entry differently on | ||
| * another call. | ||
| * | ||
| * To encode lazily, each instance holds references to [backStackEntry] and its [serializer] for as | ||
| * long as the instance is reachable. | ||
| */ | ||
| internal class KotlinxArgumentSource( | ||
| private val backStackEntry: Any, | ||
| private val serializer: KSerializer<Any>, | ||
| private val onFailure: (Throwable) -> Unit, | ||
| ) : AbstractMap<String, Any?>(), ArgumentSource { | ||
|
|
||
| private val copy: Lazy<EncodedCopy> = | ||
| // PUBLICATION so that no lock is held while encoding runs the host app's serializers, which | ||
| // could otherwise deadlock on a lock held by the app. Readers that race on the first read may | ||
| // each encode, but all of them see the copy published first. | ||
| lazy(LazyThreadSafetyMode.PUBLICATION) { encodeCopy() } | ||
|
|
||
| override val entries: Set<Map.Entry<String, Any?>> | ||
| get() = copy.value.arguments.entries | ||
|
|
||
| /** | ||
| * Writes the entry's arguments to [sink]: the cached copy if the arguments were read, or else | ||
| * encoding the entry's properties. | ||
| * | ||
| * Throws [DropArgumentsException] if the arguments can't be written, either with the reason for | ||
| * the constraint they violated, or with [ArgumentDropReason.ENCODING_FAILED] if encoding failed. | ||
| */ | ||
| override fun writeTo(sink: ArgumentSink) { | ||
| if (copy.isInitialized()) { | ||
| copy.value.writeTo(sink) | ||
| } else { | ||
| encode(sink) | ||
| } | ||
| } | ||
|
|
||
| @Suppress("ThrowsCount") | ||
| private fun encode(sink: ArgumentSink) { | ||
| try { | ||
| KotlinxPropertiesEncoder.encode(serializer, backStackEntry, sink) | ||
| } catch (e: DropArgumentsException) { | ||
| // Prefer the sink's first failure because it determines whether older entries keep their | ||
| // arguments. | ||
| throw sink.failure ?: e | ||
| } catch (e: LinkageError) { | ||
| // Preserve the entry's name and drop its properties. A compatibility check can't exercise | ||
| // every encoder path, and this LinkageError may be specific to the entry's serializers, so | ||
| // we keep the failure local to this type. | ||
| sink.failure?.let { throw it } | ||
| onFailure(e) | ||
| throw DropArgumentsException(ArgumentDropReason.ENCODING_FAILED, warning = null) | ||
| } catch (@Suppress("TooGenericExceptionCaught") t: Throwable) { | ||
| // App serializers may throw anything. Catch broadly to log once per type. | ||
| ExceptionUtils.rethrowIfFatal(t) | ||
|
|
||
| // A serializer may replace a constraint violation with its own exception. Prefer the sink's | ||
| // recorded failure because it determines whether older entries keep their arguments. | ||
| sink.failure?.let { throw it } | ||
| onFailure(t) | ||
| throw DropArgumentsException(ArgumentDropReason.ENCODING_FAILED, warning = null) | ||
| } | ||
| } | ||
|
|
||
| private fun encodeCopy(): EncodedCopy { | ||
| val sink = TreeArgumentSink(ArgumentBudget()) | ||
|
|
||
| return try { | ||
| encode(sink) | ||
| EncodedCopy(sink.argumentsOrThrow(), failure = null) | ||
| } catch (e: DropArgumentsException) { | ||
| // encode already reported an encoding failure. | ||
| if (e.reason != ArgumentDropReason.ENCODING_FAILED) { | ||
| onFailure(e) | ||
| } | ||
| EncodedCopy(emptyMap(), failure = e) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Arguments encoded from a single back stack entry's properties: [written] if encoding succeeded, | ||
| * or else the [failure] that stopped it. | ||
| */ | ||
| private class EncodedCopy( | ||
| private val written: Map<String, Any?>, | ||
| private val failure: DropArgumentsException?, | ||
| ) : ArgumentSource { | ||
|
|
||
| /** | ||
| * The arguments a read returns: [written], or, if encoding failed, only the drop reason under | ||
| * [ARGUMENT_DROP_REASON_KEY]. | ||
| * | ||
| * A host app mapper that copies or filters a dropped entry's arguments passes the reason on as | ||
| * an ordinary argument. Sentry then records it as it would for this mapper's own output, rather | ||
| * than recording no arguments and no reason. | ||
| */ | ||
| val arguments: Map<String, Any?> = | ||
| if (failure == null) { | ||
| written | ||
| } else { | ||
| mapOf(ARGUMENT_DROP_REASON_KEY to failure.reason.serializedValue) | ||
| } | ||
|
|
||
| /** | ||
| * Writes [written] to [sink], or, if encoding failed, throws [failure]'s reason without writing | ||
| * anything. | ||
| * | ||
| * A failed copy spends none of the sink's budget. Encoding the entry into the sink might have | ||
| * broken the sink's limits first, which would have dropped older entries' arguments too. A | ||
| * failed read keeps them instead, as replaying the copy can't account for everything encoding | ||
| * spent. | ||
| */ | ||
| override fun writeTo(sink: ArgumentSink) { | ||
| val failure = failure | ||
| if (failure == null) { | ||
| // The copy holds only plain maps, lists, and values the sink supports. | ||
| HostArgumentSource(written, onUnsupportedValue = {}).writeTo(sink) | ||
| return | ||
| } | ||
| // A broken limit has a warning and is recorded on the sink, as it would have been while | ||
| // encoding. An encoding failure was reported when it was read. | ||
| failure.warning?.let { sink.reject(failure.reason, it) } | ||
| throw DropArgumentsException(failure.reason, warning = null) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The arguments of a back stack entry whose properties can't be encoded because the app's | ||
| * kotlinx.serialization version doesn't support the properties encoder. | ||
| * | ||
| * Handles encoding failures like [KotlinxArgumentSource]: map reads return `{"dropped_by_sentry": | ||
| * "encoding_failed"}`, and writes drop the entry's arguments with | ||
| * [ArgumentDropReason.ENCODING_FAILED]. | ||
| */ | ||
| internal object KotlinxUnencodableArguments : AbstractMap<String, Any?>(), ArgumentSource { | ||
|
|
||
| private val dropReason = | ||
| mapOf(ARGUMENT_DROP_REASON_KEY to ArgumentDropReason.ENCODING_FAILED.serializedValue) | ||
|
|
||
| override val entries: Set<Map.Entry<String, Any?>> | ||
| get() = dropReason.entries | ||
|
|
||
| override fun writeTo(sink: ArgumentSink) { | ||
| throw DropArgumentsException(ArgumentDropReason.ENCODING_FAILED, warning = null) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
So my clanker tells me that
SentryBackStackEntry.equalsandhashCodedelegate to arguments. This means that comparing two entries or putting one in a HashSet now runs the app's serializers on whatever thread we're currently on.The issue is that the java spec says that
equalsshouldn't have side effects!My clanker thinks the code could be vastly simplified if we removed the laziness here.