Skip to content

Async functions from different modules treated as normal coroutines #418

Open
@csos95

Description

@csos95

Context

I've been slowly creating some modules to use in various projects (mostly wrapping rust libraries).
Some of them are async libraries and I want to use these modules with other lua binaries (such as love2d), so I created a simple scheduler module that just contains a tokio Runtime and LocalSet and functions to add coroutines to the LocalSet and run them all.

In one of my current web projects I wanted to use lua to prototype new pages, so I made a /lua endpoint and a websocket endpoint to connect to a fennel repl.
These endpoints pass requests from and to two lua coroutines in the same lua state.
They import modules defined in a separate .so file and use async functions from them.

Issue

I noticed that whenever I call an async function defined in a binary/library other than the one being used to run call_async, it uses 100% cpu.

After digging through the mlua docs and code, I believe I've tracked the issue down to the fact that the address of ASYNC_POLL_PENDING is used for the Lua::poll_pending value rather than a constant value.
This means every binary/library has a different value for poll_pending.
So when the Future impl of AsyncThread polls the coroutine, it doesn't recognize the return value as poll_pending.
Instead, it treats it like a normal coroutine that has yielded and immediately calls the waker to wake up the task before returning Poll::Pending.

Question

Is there any specific reason (such as possible security concerns) that the address of ASYNC_POLL_PENDING is used for poll_pending rather than a constant value?

If there is not, would it be possible to change it to use a constant value so that async functions defined in other binaries/libraries will be recognized and run correctly when using call_async?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions