KTOR-7299 Fix close race condition causing descriptor to be closed while select is still busy #4638
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.
KTOR-7299 IOException: Fail to select descriptor for ACCEPT
Unfortunately it does not resolve my specific issue, but I noticed this race condition when investigating the issue.
I cannot consistently reproduce this in a test so I did not add it, but I was able to reproduce it sometimes throwing
kotlinx.io.IOException: Bad descriptor 5 for ACCEPT
. This means the descriptor was closed multiple times, which should never happen. With the changes in this PR (and after #4637 is merged which I discovered due to this issue) it will throwkotlinx.io.IOException: Selectable closed
instead. This is in line with JVM which throws ClosedChannelException (so IOException).Explanation of race condition before this PR:
ktor/ktor-network/nix/src/io/ktor/network/selector/SelectUtilsNix.kt
Line 65 in e4fd251
ktor/ktor-network/nix/src/io/ktor/network/selector/SelectUtilsNix.kt
Lines 77 to 83 in e4fd251
Fixed the above issue by moving
closeQueue.close()
to after the selection loop has finished so descriptors are not closed while select is still busy.