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

feat: support Atomics.waitAsync #687

Merged
merged 111 commits into from
Dec 4, 2024
Merged

feat: support Atomics.waitAsync #687

merged 111 commits into from
Dec 4, 2024

Conversation

metcoder95
Copy link
Member

@metcoder95 metcoder95 commented Oct 30, 2024

  • feat: Support Atomics.waitAsync

metcoder95 and others added 30 commits September 13, 2023 12:59
Co-authored-by: Robert Nagy <ronagy@icloud.com>
… 6.12.0 (#453)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
ronag
ronag previously approved these changes Nov 4, 2024
returning control to the main thread (avoid having open handles within a thread). If still want to have the possibility
of having open handles or handle asynchrnous tasks, you can set the environment variable `PISCINA_ENABLE_ASYNC_ATOMICS` to `1` or setting `options.atomics` to `async`.

> **Note**: The `async` mode comes with performance penalties and can lead to undesired behaviour if open handles are not tracked correctly.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain the open handle thing? Why is that an issue with async?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly as we are not doing event loop ticks, these can add some overhead if the workers start doing more than it should.

Even waiting for just the worker to wake up added some ticks that showed a difference of ±5% which is really negligible.

I've not strong opinion in this one; but if we want to make this the default behaviour we should document that it is important to not keep the workers doing too much background tasks as they can face some penalty (as well if using async, implementer needs to account for the teardown of the worker when idle, i.e. no more tasks scheduled)

Copy link
Member Author

Choose a reason for hiding this comment

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

If that sounds good, I can adjust the PR for that 👍

@ronag
Copy link
Collaborator

ronag commented Nov 4, 2024

I would recommend making async the default for a future semver major. IMHO it's the best balance between perf and expected behavior.

Base automatically changed from feat/custom_balancer to current November 5, 2024 07:49
@metcoder95 metcoder95 dismissed ronag’s stale review November 5, 2024 07:49

The base branch was changed.

@metcoder95 metcoder95 requested a review from ronag December 4, 2024 10:02
@metcoder95 metcoder95 linked an issue Dec 4, 2024 that may be closed by this pull request
@metcoder95
Copy link
Member Author

Merging now, let me know if there's further feedback so I can address it in further PRs

@metcoder95 metcoder95 merged commit 9c5a19e into current Dec 4, 2024
10 checks passed
@metcoder95 metcoder95 deleted the feat/async_atomics branch December 4, 2024 11:16
@jdmarshall
Copy link

So with this change will setInterval work properly? When this PR was first brought to my attention I was concerned in part about whether something like OpenTelemetry.js would function properly in a piscina worker thread.

A feature I'm thinking about creating a PR for would make piscina more attractive for beefier use cases, but the absence of ticks when no unanswered messages are in flight is problematic.

@metcoder95
Copy link
Member Author

Yeah; in a brief, with this change the event loop of the thread will continue ticking instead of being hold as it was before.
This can be adjusted through the atomics flag when instantiating Piscina or PISCINA_ENABLE_ASYNC_ATOMICS.

@jdmarshall
Copy link

jdmarshall commented Jan 8, 2025

Excellent.

As an aside, I'm wondering... A couple years ago I had a process that wouldn't exit. There's a debugging library that can tell you why the event loop isn't exiting in NodeJS. I think it hooks setInterval and setTimeout so it can dump the metadata about everything that is keeping things alive.

I'm wondering if it would be saner for piscina to do something similar, and keep the event loop in the workers running as long as there are outstanding intervals or timeouts. The bigger problem I'm trying to solve:

What if I try to push a task that's big enough to have OpenTelemetry or Prometheus running in it into a worker thread? Its Interval will never fire and the last block of telemetry after each message will either disappear or show up potentially minutes later.

Because without that, this other line of questioning (making 2 pools of workers talk to each other) is dead in the water. Because that sort of long-lived worker definitely needs to have monitoring. The flag is workable but it becomes an asterisk for things like this, and one or two are okay but beyond that I find you start to lose your audience.

@metcoder95
Copy link
Member Author

I believe that it should be totally doable with this new change; you can use the current alpha version for experimental testing and play with it to see how it goes.

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.

use Atomic.waitAsync instead of Atomics.wait
8 participants