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

NIOAsyncWriter: Fix suspending yield when writer is finished #3044

Conversation

orobio
Copy link
Contributor

@orobio orobio commented Jan 9, 2025

This fixes hitting the following preconditionFailure in NIOAsyncWriter: "This should have already been handled by yield()".

It doesn't expect a yield to be suspended when the state is .writerFinished, but this can definitely happen. This seemed to be the correct solution to me, but please check it carefully, because I'm unfamiliar with this code.

I'm not happy about the test for this. It's blocking the didYield call for a while, to make sure the correct state is reached. See also the 'FIXME' line. What would be a better way to do this?

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

This is a great catch, thanks so much!

Regarding your test, it might be useful for the test to use a new delegate that makes it easier to arrange the ordering to work out. NIO's ConditionLock can be used to set up a nice ordering chain, so that instead of using the usleeps we can just block on that lock being unlocked with the right value.

@orobio orobio force-pushed the NIOAsyncWriter-Fix-suspending-yield-when-writer-finished branch from bf18517 to f15a87b Compare January 14, 2025 08:05
@orobio
Copy link
Contributor Author

orobio commented Jan 14, 2025

@Lukasa : I updated it with ConditionLock. Is this what you had in mind?

This is still blocking a thread from the concurrency pool and then depending on another thread to unblock it. Will that not be a problem? The only way I see around that is to add an async hook to NIOAsyncWriter.

@Lukasa
Copy link
Contributor

Lukasa commented Jan 14, 2025

For testing purposes, that blocking will be safe. We will eventually resolve it, and we don't rely on Dispatch to make that happen (it requires the NIO thread to do it), so it'll be ok.

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jan 14, 2025
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

This is a really lovely patch, thanks so much @orobio!

@Lukasa Lukasa enabled auto-merge (squash) January 14, 2025 15:12
@Lukasa Lukasa merged commit 6fb31ea into apple:main Jan 14, 2025
34 of 35 checks passed
@orobio
Copy link
Contributor Author

orobio commented Jan 14, 2025

For testing purposes, that blocking will be safe. We will eventually resolve it, and we don't rely on Dispatch to make that happen (it requires the NIO thread to do it), so it'll be ok.

I'm not sure I fully understand what you're saying here, but I assume it's fine then.

I do still have that 'FIXME' line in there, which would probably be good to remove. Well..., this got merged while I was typing, so I'll open a new pull request for that.

Edit: Opened #3059

orobio added a commit to orobio/swift-nio that referenced this pull request Jan 14, 2025
This was left in when pull request apple#3044 was merged, but as discussed on
that pull request, this doesn't need to be fixed.
Lukasa pushed a commit that referenced this pull request Jan 14, 2025
This was left in when pull request #3044 was merged, but as discussed on
that pull request, this doesn't need to be fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants