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 waiting on Windows to be sure I/O events can still come in. #4325

Merged
merged 1 commit into from
Feb 22, 2023

Conversation

jemc
Copy link
Member

@jemc jemc commented Feb 22, 2023

Prior to this commit we were using a mechanism on Windows called WaitForSingleObject when we wanted to put a thread to sleep until we activated a signal to wake it up later.

Now we use WaitForSingleObjectEx instead, so we can be sure that the thread is kept in an "alertable" state if there is an APC (Async Procedure Call) that needs to be run during the wait period.

We use APCs for socket I/O completion callbacks, and they can only arrive on the thread that initiated them, so it's important to allow such callbacks to run even if the scheduler thread is suspended.

Note that the callbacks we use for sockets will not run any Pony code - they will only dispatch a message via the ASIO subsystem, so running those APCs is safe even if the actor has been work-stealed to another thread and is running there. In such a situation, the ASIO event will just go into the actor's queue and they'll process the message later, as normal. We just need to make sure the APC actually will get run, hence we need to ensure even suspended threads will stay "alertable".

Note that this fixes a similar problem to one of the problems that was fixed in PR #3816, wherein some calls to Sleep were migrated to SleepEx for the same reason - to stay "alertable".

@jemc jemc self-assigned this Feb 22, 2023
@jemc jemc added the changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge label Feb 22, 2023
@ponylang-main ponylang-main added discuss during sync Should be discussed during an upcoming sync and removed changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge labels Feb 22, 2023
@ponylang-main
Copy link
Contributor

Hi @jemc,

The changelog - fixed label was added to this pull request; all PRs with a changelog label need to have release notes included as part of the PR. If you haven't added release notes already, please do.

Release notes are added by creating a uniquely named file in the .release-notes directory. We suggest you call the file 4325.md to match the number of this pull request.

The basic format of the release notes (using markdown) should be:

## Title

End user description of changes, why it's important,
problems it solves etc.

If a breaking change, make sure to include 1 or more
examples what code would look like prior to this change
and how to update it to work after this change.

Thanks.

Prior to this commit we were using a mechanism on Windows called
`WaitForSingleObject` when we wanted to put a thread to sleep
until we activated a signal to wake it up later.

Now we use `WaitForSingleObjectEx` instead, so we can be sure that
the thread is kept in an "alertable" state if there is an APC
(Async Procedure Call) that needs to be run during the wait period.

We use APCs for socket I/O completion callbacks, and they can only
arrive on the thread that initiated them, so it's important to
allow such callbacks to run even if the scheduler thread is suspended.

Note that the callbacks we use for sockets will not run any Pony
code - they will only dispatch a message via the ASIO subsystem,
so running those APCs is safe even if the actor has been
work-stealed to another thread and is running there.
In such a situation, the ASIO event will just go into the actor's
queue and they'll process the message later, as normal.
We just need to make sure the APC actually will get run,
hence we need to ensure even suspended threads will stay "alertable".

Note that this fixes a similar problem to one of the problems
that was fixed in PR ponylang#3816, wherein some calls to `Sleep`
were migrated to `SleepEx` for the same reason - to stay "alertable".
@jemc jemc force-pushed the fix/windows-wait-ex branch from ed95f9b to 9fc03f8 Compare February 22, 2023 05:48
@jemc jemc marked this pull request as ready for review February 22, 2023 15:02
@jemc jemc added the changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge label Feb 22, 2023
@jemc jemc requested review from chalcolith and a team February 22, 2023 15:03
@jemc
Copy link
Member Author

jemc commented Feb 22, 2023

This passed CI, and is ready for review.

@SeanTAllen SeanTAllen merged commit 9296ff0 into ponylang:main Feb 22, 2023
@ponylang-main ponylang-main removed the discuss during sync Should be discussed during an upcoming sync label Feb 22, 2023
github-actions bot pushed a commit that referenced this pull request Feb 22, 2023
github-actions bot pushed a commit that referenced this pull request Feb 22, 2023
@jemc jemc deleted the fix/windows-wait-ex branch February 23, 2023 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants