Skip to content
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

Coroutines: make sure we do not crash when calling offer #2007

Conversation

martinbonnin
Copy link
Contributor

override fun onResponse(response: Response<T>) {
if (!channel.isClosedForSend) {
runCatching {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sav007 I feel the runCatching approach is safer since there could potentially be a race condition where the channel is closed just after the if before. Let me know if isClosedForSend was used to catch other cases.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid having to wrap the offer in a runCatching, you could create an extension on a Channel.

fun <E> SendChannel<E>.safeOffer(value: E) = runCatching {
        offer(value)
}

Copy link
Contributor Author

@martinbonnin martinbonnin Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msya, for 2 instances only, I'd rather not pollute the SendChannel namespace with an extension function.

@martinbonnin martinbonnin merged commit 9e2a24f into apollographql:master Feb 24, 2020
@martinbonnin martinbonnin deleted the catch-coroutines-offer-exceptions branch March 23, 2020 09:55
tasomaniac pushed a commit that referenced this pull request Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kotlinx.coroutines.JobCancellationException: Job was cancelled;
3 participants