Skip to content

AtomicWaker: take and wake #4151

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

bugadani
Copy link
Contributor

@bugadani bugadani commented May 1, 2025

While in embassy tasks are static, in other environments it's usually not the norm. There, keeping the Waker may force the task that registered it to remain allocated, potentially indefinitely. Given that AtomicWaker is used in peripheral drivers that aren't tied to a particular async framework, letting go of the Waker once it's not needed is probably better than keeping it around.

I expect this change to surface some issues around Futures that only register the waker once and expect it to stick around.

@bugadani bugadani force-pushed the take-and-wake branch 2 times, most recently from 02b503e to d6e19ce Compare May 1, 2025 09:22
@@ -156,8 +156,7 @@ fn waking_after_completion_does_not_poll() {
let (executor, trace) = setup();
executor.spawner().spawn(task1(trace.clone(), waker)).unwrap();

unsafe { executor.poll() };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what I was thinking here, the task is single shot and exits by the time this poll returns.

@Dirbaio Dirbaio added the trusted label May 1, 2025
@bugadani
Copy link
Contributor Author

bugadani commented May 1, 2025

Hmm does that trusted label mean I can ask for...?

bender run

@embassy-ci
Copy link

embassy-ci bot commented May 1, 2025

run: permission denied

@Dirbaio
Copy link
Member

Dirbaio commented May 1, 2025

bender run

@Dirbaio
Copy link
Member

Dirbaio commented May 1, 2025

I'm wondering, this alone doesn't fix the whole problem right? we also have to ensure all drivers deregister all their wakers on drop, otherwise they'll stay there forever keeping a reference to the task.

And the extra code to do that would be "waste" when running with embassy-executor.

@bugadani
Copy link
Contributor Author

bugadani commented May 1, 2025

No, not the whole problem, but with the follow-up PR the bits are there for HAL authors to do it if they want to, without their own AtomicWaker.

@Dirbaio
Copy link
Member

Dirbaio commented May 1, 2025

Is this something you're considering doing in esp-hal?

How're you planning of dealing with "the extra code to do that would be "waste" when running with embassy-executor"? just accept the bloat?

@bugadani
Copy link
Contributor Author

bugadani commented May 1, 2025

How're you planning of dealing with

We don't have targets with 32k flash, that's how 😅 This isn't exactly about what we consider doing, it's more about not limiting the HAL to statically allocated tasks.

@Dominaezzz
Copy link

I'm wondering, this alone doesn't fix the whole problem right? we also have to ensure all drivers deregister all their wakers on drop, otherwise they'll stay there forever keeping a reference to the task.

Once this (and the follow up PR lands), I want to make PRs to embassy-sync to fix more of the problem.
For example the Future returned by Signal::wait doesn't free the waker when it drops/cancels.
Of course, unlike this PR, everyone would be paying for the extra code to do this. Would you be against this? (I don't want waste the elbow grease)

@bugadani
Copy link
Contributor Author

bugadani commented May 2, 2025

So, this PR should either be neutral, or size/perf-positive because there's no need to write the Waker back into the Cell. However, the next PR will not be - take or deregister or whatever we end up with is a net loss for embassy. So what if we added a non-static-tasks feature to let other ecosystems work? The implication is that the next PR would have to be reverted from take to deregister so that the function could be no-op if the feature is not set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants