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

fix: handle stream response exceptions properly in Ktor engine #589

Merged
merged 1 commit into from
Feb 15, 2022

Conversation

ianbotsf
Copy link
Contributor

Issue #

aws-sdk-kotlin#524

Description of changes

Fixes a bug with the Ktor engine's waiter. Due to a bad refactor, the implementation is unable to handle exceptions that occur before the waiter begins waiting (e.g., in the case an exception is thrown). Rather than restore the previous implementation based on channels, switched for a simpler and (hopefully) clearer implementation based on mutexes.

Added unit tests that verify waiter behavior. In particular, testSignalWhenNotWaiting failed with the prior implementation.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ianbotsf ianbotsf requested a review from a team as a code owner February 14, 2022 21:46
@ianbotsf ianbotsf requested a review from aajtodd February 14, 2022 21:46
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Contributor

@aajtodd aajtodd left a comment

Choose a reason for hiding this comment

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

I don't recall the behavior of kotlinx.coroutines.Mutex w.r.t same thread. This refactor could be susceptible to incorrect behavior if the mutex is re-entrant (i.e. same thread that locked it is allowed to acquire).

I don't think that the mutex is re-entrant (especially since there is an explicit re-entrant lock type in kotlinx.atomicfu package) but worth maybe double checking.

delay(500.milliseconds)
waiter.signal()
}
delay(1000.milliseconds)
Copy link
Contributor

Choose a reason for hiding this comment

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

we might consider changing these to something smaller, no need to wait a full second in the test I don't think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is in a runBlockingTest so time has no meaning.

@ianbotsf
Copy link
Contributor Author

I don't recall the behavior of kotlinx.coroutines.Mutex w.r.t same thread. This refactor could be susceptible to incorrect behavior if the mutex is re-entrant (i.e. same thread that locked it is allowed to acquire).

According to the Mutex documentation, it is non-reentrant.

@ianbotsf ianbotsf merged commit 158353e into main Feb 15, 2022
@ianbotsf ianbotsf deleted the fix-ktor-engine-waiter branch February 15, 2022 16:59
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.

2 participants